Last active
June 24, 2019 15:55
-
-
Save ikatsov/affa865415c8564a7d36c618906ba069 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 last_touch_attribution(df): | |
| # count the number of events for each campaign in df | |
| def count_by_campaign(df): | |
| counters = np.zeros(n_campaigns) | |
| for campaign_one_hot in df['campaigns'].values: | |
| campaign_id = np.argmax(campaign_one_hot) | |
| counters[campaign_id] = counters[campaign_id] + 1 | |
| return counters | |
| # count the number of impressions for each campaign | |
| campaign_impressions = count_by_campaign(df) | |
| # count the number of times the campaign is the last touch before the conversion | |
| dfc = df[df['conversion'] == 1] | |
| idx = dfc.groupby(['jid'])['timestamp_norm'].transform(max) == dfc['timestamp_norm'] | |
| campaign_conversions = count_by_campaign(dfc[idx]) | |
| return campaign_conversions / campaign_impressions | |
| lta = last_touch_attribution(df6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment