Skip to main content
Version: 3.12

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 password

password="yourpassword"

#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,server="http://app.channelattribution.io",password=password)

#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 pandas as pd

from ChannelAttributionPro import *

import requests

import os

#Set your password

password="yourpassword"

#Download data

url = 'https://app.channelattribution.io/data/data_paths_w_features.csv'
response = requests.get(url)

# Save the file
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,server="http://app.channelattribution.io",dir_tmp=dir_tmp,dir_out=dir_out,password=password)

#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"])