I want to calculate multi-touch attribution in e-commerce application. The pandas…
- User
I want to calculate multi-touch attribution in e-commerce application. The pandas dataframe is about 2 days, all rawdata events over all users behaviour. Data are almost 2.5M rows.
The column name 'eventName' is major. Attribution calculated next ways:
- If an user view product thumbnail, it is 1 event and it's 'eventName' is 'view product'.
- If an user click product thumbnail, it is 1 event and it's 'eventName' is 'click product'
- If an user bought product thumbnail, it is 1 event and it's 'eventName' is 'buy product'
- 'user_uid' column mean user unique identifier
- 'product_uid' column mean product unique identifier.
- We assign 'target event'. In this case 'eventName' == 'buy product'
- We assign 'action event'. In this case, 'eventName' == 'view product'
- For each target event, it has N(0 or more) 'action event's their time is before than target event's. In this situation, these 'action event' are called 'attributed to target event'
- 'attributed to target event' and their 'target event' must same condition. In this situation, it means "same user and same product" event
- For all 'attributed to target event' targets a 'target event', they divided 2 types of 'action event'. One is 'hit' and other one is 'miss'. If time difference between action event and target event are smaller than attribution window size, the action event is 'hit' and if larger than attribution window size, it is 'miss'. In this situation, it means "24h"
- Each 'hit' event get 1/len(all hit action for this target event) of score. In this case, score is 'price' of product.
- Of course, an action event can belong multiple target event's 'attributed to target event'. (e.g, A same user bought same product twice under 24h term)
- Each action event's total score is sum of all score that gain from this action belonged "attributed to target event"'s (If action event row A has belonged 5 group of 'attributed to target event' and score gained 5.13, 2.33, 0(missed), 3.22, 9.64, it's total score is 5.13+2.33+0+3.22+9.64=20.32)
- If you filter 'action' row for a standard, this filter's total score is "sum of filtered action event" / "count of filtered action event".
Using this rules, we can calculate multiple recommendation model's quality. But calculate this score naive code seems too long time to calculate. I can't imagine how to code it like use groupby-agg or apply or other dataframe tricks or map-reduce. Can you write to me pandas dataframe calculation code?
- Assistant
It seems like you're trying to do a complex event attribution analysis with multiple conditions. Pandas provides a robust set of tools to handle such tasks, but given the complexity and size of your data, it might still take some time to compute. Here's a simplified version of how you could approach this using pandas:
pythonimport pandas as pd import numpy as np # assuming df is your dataframe df['time_diff'] = df.groupby(['user_uid', 'product_uid'])['timestamp'].diff() df['is_hit'] = (df['time_diff'] < pd.Timedelta(hours=24)) & (df['eventName'] != 'buy product') df['score'] = np.where(df['is_hit'], df['price'], 0) df['total_score'] = df.groupby('action event')['score'].transform(sum) df['filtered_score'] = df['total_score'] / df.groupby('action event')['score'].transform('count') # final score final_score = df[df['eventName'] == 'buy product']['filtered_score'].mean()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.