Created
November 19, 2019 02:20
-
-
Save MortenHegewald/fb1d8051cd818c25283cbcbc4b587e5c to your computer and use it in GitHub Desktop.
MA - Removal Effects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def removal_effects(df, conversion_rate): | |
removal_effects_dict = {} | |
channels = [channel for channel in df.columns if channel not in ['Start', | |
'Null', | |
'Conversion']] | |
for channel in channels: | |
removal_df = df.drop(channel, axis=1).drop(channel, axis=0) | |
for column in removal_df.columns: | |
row_sum = np.sum(list(removal_df.loc[column])) | |
null_pct = float(1) - row_sum | |
if null_pct != 0: | |
removal_df.loc[column]['Null'] = null_pct | |
removal_df.loc['Null']['Null'] = 1.0 | |
removal_to_conv = removal_df[ | |
['Null', 'Conversion']].drop(['Null', 'Conversion'], axis=0) | |
removal_to_non_conv = removal_df.drop( | |
['Null', 'Conversion'], axis=1).drop(['Null', 'Conversion'], axis=0) | |
removal_inv_diff = np.linalg.inv( | |
np.identity( | |
len(removal_to_non_conv.columns)) - np.asarray(removal_to_non_conv)) | |
removal_dot_prod = np.dot(removal_inv_diff, np.asarray(removal_to_conv)) | |
removal_cvr = pd.DataFrame(removal_dot_prod, | |
index=removal_to_conv.index)[[1]].loc['Start'].values[0] | |
removal_effect = 1 - removal_cvr / conversion_rate | |
removal_effects_dict[channel] = removal_effect | |
return removal_effects_dict | |
removal_effects_dict = removal_effects(trans_matrix, base_conversion_rate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/MortenHegewald/fb1d8051cd818c25283cbcbc4b587e5c
KeyError: 'Null'
hey i am getting this error. can you help me out with it