Skip to main content

Python use cases

Load the libray and data


import pandas as pd

from ChannelAttributionPro import *

import urllib.request

Data = pd.read_csv("/home/Projects/GIT/personal/channelattributionpro/data/italy_data.csv",sep=",")

password="yourpassword"

urllib.request.urlretrieve("https://channelattribution.io/csv/Data.csv", "Data.csv")

heuristic_models

#Perform transaction level attribution from a data.frame

path_attribution=heuristic_models(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", cha_sep=">",
password=password)
print(path_attribution)

#Return non converting paths in the output data.frame

path_attribution=heuristic_models(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", cha_sep=">",
flg_write_nulls=1, password=password)
print(path_attribution)

#Return paths in the output data.frame

path_attribution=heuristic_models(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", cha_sep=">",
flg_write_paths=1, password=password)
print(path_attribution)


#Perform transaction level attribution from a file and write output to file

res=heuristic_models(Data="Data.csv", var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", cha_sep=">", row_sep=";",
file_output="ouput.csv", password=password)
path_attribution = pd.read_csv("ouput.csv",sep=";")
print(res)

markov_model

#Perform transaction level attribution reading data from a data.frame

res=markov_model(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value",
var_null="total_null", cha_sep=">", password=password)
path_attribution=res["attribution"]
print(path_attribution)


#Return non converting paths in the output data.frame

res=markov_model(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value",
var_null="total_null", cha_sep=">", flg_write_nulls=1, password=password)
path_attribution=res["attribution"]
print(path_attribution)


#Return paths in the output data.frame

res=markov_model(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value",
var_null="total_null", cha_sep=">", flg_write_paths=1, password=password)
path_attribution=res["attribution"]
print(path_attribution)

#Perform transaction level attribution reading data from a file and write output to file

res=markov_model(Data="Data.csv", var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", var_null="total_null",
cha_sep=">", row_sep=";", file_output="ouput.csv", password=password)
print(res["attribution"])

res=markov_model(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", var_null="total_null", cha_sep=">",
row_sep=";", file_output="ouput.csv", password=password)
print(res["attribution"])

shapley

# Perform transaction level attribution reading data from a data.frame

res=shapley(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value",
var_null="total_null", cha_sep=">", password=password)
path_attribution=res["attribution"]
print(path_attribution)

#Perform transaction level attribution using the classic Shapley formula

res=shapley(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value",
var_null="total_null", cha_sep=">", flg_simplified=False, password=password)
path_attribution=res["attribution"]
print(path_attribution)

#Return non converting paths in the output data.frame

res=shapley(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value",
var_null="total_null", cha_sep=">", flg_write_nulls=1, password=password)
path_attribution=res["attribution"]
print(path_attribution)

#Return paths in the output data.frame

res=shapley(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", var_null="total_null",
cha_sep=">", flg_write_paths=1, password=password)
path_attribution=res["attribution"]
print(path_attribution)

#### Perform transaction level attribution reading data from a file and write output to file

res=shapley(Data="Data.csv", var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", var_null="total_null",
cha_sep=">", row_sep=";", file_output="ouput.csv", password=password)
print(res["attribution"])

new_paths_attribution

#make attribution on new paths after you have trained a Markov model

train=markov_model(Data=Data, var_path="path", var_conv="total_conversions",
var_value="total_conversion_value", cha_sep=">", password=password)
path_attribution=res$attribution

res=new_paths_attribution(Data=Data[1:5], var_path="path", var_conv="total_conversions",
Dparams=train$parameters, var_value="total_conversion_value",
row_sep=";", cha_sep=">", flg_write_nulls=0, flg_write_paths=1,
server="app.channelattribution.net", password=password)
print(res$attribution)


#Perform transaction level attribution reading data from a file and write output to file

res=new_paths_attribution(Data="Data.csv", var_path="path", var_conv="total_conversions",
Dparams=train$parameters, var_value="total_conversion_value",
row_sep=";", cha_sep=">", flg_write_nulls=0, flg_write_paths=1,
file_output="ouput.csv", server="app.channelattribution.net",
password=password)
print(res$attribution)

choose_order

#choose the best Markov model using AUC reading data from a data.frame

res=choose_order(Data, var_path="path", var_conv="total_conversions",
var_null="total_null", plot=0, cha_sep=">",password=password)
print(res['suggested_order'])

#choose the best Markov model using AUC reading data from a file

res=choose_order(Data="Data.csv", var_path="path", var_conv="total_conversions",
var_null="total_null", cha_sep=">", row_sep=";", plot=0, password=password)
print(res['suggested_order'])

#choose the best Markov model using Area under Precision-Recall Curve

res=choose_order(Data="Data.csv", var_path="path", var_conv="total_conversions",
var_null="total_null", cha_sep=">", row_sep=";", type='auc-prerec',
plot=0, password=password)
print(res['suggested_order'])

combine_combine_mta_mmm

#perform multi-touch attribution

res=markov_model(Data, var_path="path", var_conv="total_conversions",
var_null="total_null", flg_write_paths=1,password=password)
mta_path_attribution=res['attribution']
mta_path_attribution=mta_path_attribution[["path_id","path","channel","total_conversions"]]
mta_path_attribution.columns=["path_id","path","channel","attribution"]
print(mta_path_attribution)

#load media mix model attribution

mmm_attribution=pd.DataFrame(
{"channel":["alpha","eta","iota","beta","theta","lambda","epsilon","omega"],
"attribution":[4826.18,2757.64,2574.60,1454.74,913.53,418.50,407.34,800.00]})
print(mmm_attribution)

#combine MTA and MMM without prior weights

final_path_attribution=combine_mta_mmm(mta_path_attribution, mmm_attribution, password=password)
print(final_path_attribution)

#combine MTA and MMM with a prior weight on channel alpha

prior_weights_mta=pd.DataFrame({"channel":["alpha"],"mta_weight":[0.2]})
#prior_weights_mta is a data.frame of subjective prior weights.
#Each weight is a real number between 0 and 1 which indicates, for the channel considered,
#how much weight will receive MTA in the final attribution calculation.
#Hence (1-MTA weight) is the weight of MMM for the channel considered.
#Since we know that channel alpha had some tracking problems than we decided
#to give it an MTA weight equal to $0.2$
#which implicates that MMM will receive a weight of 0.8 and thus MMM will be more
#important for that channel.
#The channels with no prior weight specified will receive a default of $0.5$ which means that
#MTA and MMM will be equally weighted in the attribution process.

final_path_attribution=combine_mta_mmm(mta_path_attribution, mmm_attribution,
prior_weights_mta=prior_weights_mta, password=password)
print(final_path_attribution)

hta_model

#load data
Data = pd.read_csv("https://app.channelattribution.net/data/Data_ata.csv")

#define list of channels you are interested in performing attribution
channels=["facebook_impressions","tv_impressions","google_adv_clicks",
"direct_searches","youtube_impressions","linkedin_impressions"]

#define the global conversion rate for the use case
glob_conv_rate=None

#define the conversion rates, one for each channel if known, or NULL otherwise
L_conv_rate=None

#define dictionary of weights (between 0 and 1), one for each channel
#(suggestion: use weight=1 for clicks)
L_weight=dict()
L_weight["facebook_impressions"]=0.01
L_weight["tv_impressions"]=0.01
L_weight["google_adv_clicks"]=1
L_weight["direct_searches"]=1
L_weight["youtube_impressions"]=0.01
L_weight["linkedin_impressions"]=0.01

#define the how many units of time are long the customer journeys
#on average for the use case considered
mean_cj_length=1

#define the adstock rates (between 0 and 1), one for each channels
L_ads_rate=dict()
L_ads_rate["facebook_impressions"]=0.00
L_ads_rate["tv_impressions"]=0.40
L_ads_rate["google_adv_clicks"]=0.10
L_ads_rate["direct_searches"]=0.10
L_ads_rate["youtube_impressions"]=0.00
L_ads_rate["linkedin_impressions"]=0.00

#run the model
#we set nsim=1 for speed reasons but at least nsim=10 is suggested
res=hta_model(Data, var_time="week", var_conv="conversions",
channels=channels, var_value=None, glob_conv_rate=glob_conv_rate,
L_conv_rate=L_conv_rate, L_weight=L_weight, mean_cj_length=mean_cj_length,
L_ads_rate=L_ads_rate, nsim=1,
verbose=True, server="app.channelattribution.net",password=password)

print(res)