Created
July 28, 2019 01:52
-
-
Save karamanbk/fec6ba669e29b8d7cfe8fed5cc021bc8 to your computer and use it in GitHub Desktop.
This file contains 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
from datetime import datetime, timedelta,date | |
import pandas as pd | |
%matplotlib inline | |
from sklearn.metrics import classification_report,confusion_matrix | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import seaborn as sns | |
from __future__ import division | |
from sklearn.cluster import KMeans | |
import plotly.plotly as py | |
import plotly.offline as pyoff | |
import plotly.graph_objs as go | |
import sklearn | |
import xgboost as xgb | |
from sklearn.model_selection import KFold, cross_val_score, train_test_split | |
import warnings | |
warnings.filterwarnings("ignore") | |
#initiate plotly | |
pyoff.init_notebook_mode() | |
#function to order clusters | |
def order_cluster(cluster_field_name, target_field_name,df,ascending): | |
new_cluster_field_name = 'new_' + cluster_field_name | |
df_new = df.groupby(cluster_field_name)[target_field_name].mean().reset_index() | |
df_new = df_new.sort_values(by=target_field_name,ascending=ascending).reset_index(drop=True) | |
df_new['index'] = df_new.index | |
df_final = pd.merge(df,df_new[[cluster_field_name,'index']], on=cluster_field_name) | |
df_final = df_final.drop([cluster_field_name],axis=1) | |
df_final = df_final.rename(columns={"index":cluster_field_name}) | |
return df_final | |
#function for calculating the uplift | |
def calc_uplift(df): | |
avg_order_value = 25 | |
#calculate conversions for each offer type | |
base_conv = df[df.offer == 'No Offer']['conversion'].mean() | |
disc_conv = df[df.offer == 'Discount']['conversion'].mean() | |
bogo_conv = df[df.offer == 'Buy One Get One']['conversion'].mean() | |
#calculate conversion uplift for discount and bogo | |
disc_conv_uplift = disc_conv - base_conv | |
bogo_conv_uplift = bogo_conv - base_conv | |
#calculate order uplift | |
disc_order_uplift = disc_conv_uplift * len(df[df.offer == 'Discount']['conversion']) | |
bogo_order_uplift = bogo_conv_uplift * len(df[df.offer == 'Buy One Get One']['conversion']) | |
#calculate revenue uplift | |
disc_rev_uplift = disc_order_uplift * avg_order_value | |
bogo_rev_uplift = bogo_order_uplift * avg_order_value | |
print('Discount Conversion Uplift: {0}%'.format(np.round(disc_conv_uplift*100,2))) | |
print('Discount Order Uplift: {0}'.format(np.round(disc_order_uplift,2))) | |
print('Discount Revenue Uplift: ${0}\n'.format(np.round(disc_rev_uplift,2))) | |
if len(df[df.offer == 'Buy One Get One']['conversion']) > 0: | |
print('-------------- \n') | |
print('BOGO Conversion Uplift: {0}%'.format(np.round(bogo_conv_uplift*100,2))) | |
print('BOGO Order Uplift: {0}'.format(np.round(bogo_order_uplift,2))) | |
print('BOGO Revenue Uplift: ${0}'.format(np.round(bogo_rev_uplift,2))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i could excute uplift model from 41 line