markov-model-feat
Python
#Here you can find two use cases:
#Use case 1: estimation of a Markov model with external features, loading data in memory
#Use case 2: estimation of a Markov model with external features, without loading data in memory (suited for high dimensional datasets)
###########
#Use case 1
###########
#estimation of a Markov model with external features, loading data in memory
#Load libraries and data
import pandas as pd
from ChannelAttributionPro import *
Data = pd.read_csv("https://app.channelattribution.io/data/data_paths_w_features.csv",sep=";")
#Set your token
token="yourtoken"
#Define the type of each feature: 'categorial' or 'numeric'
D_feat_types={'region':'categorial','segment':'categorial','seconds_to_last_touch':'numeric','position':'categorial'}
#Train Markov model with external features
res=markov_model_feat(Data,D_feat_types)
#Return transaction-level attribution
print(res["attribution"])
#Return estimated weigths for each feature at path level
print(res["weights_attr"])
#Return predictive performance for each feature
print(res["weights_feat"])
###########
#Use case 2
###########
#estimation of a Markov model with external features, without loading data in memory
#Load libraries
import requests
#Download data
response = requests.get('https://app.channelattribution.io/data/data_paths_w_features.csv')
with open('data.csv', 'wb') as f:
f.write(response.content)
#Define the path of the data
file_data="data.csv"
#Define and create the path where temporary files will be saved
dir_tmp="tmp_mmf/"
os.makedirs(dir_tmp, exist_ok=True)
#Define and create the path where output will be saved
dir_out="out_mmf/"
os.makedirs(dir_out, exist_ok=True)
#Define the type of each feature: 'categorial' or 'numeric'
D_feat_types={'region':'categorial','segment':'categorial','seconds_to_last_touch':'numeric','position':'categorial'}
#Train Markov model with external features
res=markov_model_feat(Data=file_data,D_feat_types=D_feat_types,dir_tmp=dir_tmp,dir_out=dir_out)
#Return the path where transaction-level attribution is saved
print(res["attribution"])
#Return the path where estimated weigths for each feature at path level are saved
print(res["weights_attr"])
#Return predictive performance for each feature
print(res["weights_feat"])