-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data-Driven.py
84 lines (58 loc) · 2.07 KB
/
Data-Driven.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
"""Case study Data-Driven
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1sBdA1czDkbksqjeTGy4-xbIaH8vAB5j3
"""
! pip install -q lifetimes
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.preprocessing import LabelEncoder
from lifetimes.datasets import load_cdnow_summary, load_cdnow_summary_data_with_monetary_value
from lifetimes import BetaGeoFitter
from lifetimes import GammaGammaFitter
from lifetimes.plotting import plot_frequency_recency_matrix, plot_probability_alive_matrix, plot_period_transactions
df = pd.read_csv('/content/churn.csv')
df.head()
print(df.info())
print (df.describe())
"""#Customer Churn"""
df = df.drop(columns=['RowNumber', 'CustomerId', 'Surname'])
le = LabelEncoder()
le.fit(df['Geography'])
df['Geography'] = le.transform(df['Geography'])
le.fit(df['Gender'])
df['Gender'] = le.transform(df['Gender'])
x = df.drop(columns=['Exited'])
y = df['Exited']
x.head()
y.head()
X_train, X_test, y_train, y_test = train_test_split(x, y)
rf_model = RandomForestClassifier()
rf_model.fit(X_train, y_train)
y_pred = rf_model.predict(X_test)
print(classification_report(y_test, y_pred))
result = X_test.copy()
result['Exited'] = y_test
result['Predicted'] = y_pred
result.head()
"""#Customer Lifetime Value (CLV)"""
df = load_cdnow_summary(index_col=[0])
df.head()
bgf = BetaGeoFitter()
bgf.fit(df['frequency'], df['recency'], df['T'])
plot_frequency_recency_matrix(bgf)
plot_probability_alive_matrix(bgf)
t = 1
df['predicted_purchases'] = bgf.conditional_expected_number_of_purchases_up_to_time(t, df['frequency'], df['recency'], df['T'])
df.head()
plot_period_transactions(bgf)
dfm = load_cdnow_summary_data_with_monetary_value()
dfm = dfm[dfm['monetary_value'] > 0]
dfm.corr()
print(dfm.columns)
ggf = GammaGammaFitter()
ggf.fit(dfm['frequency'], dfm['monetary_value'])
ggf.conditional_expected_average_profit(dfm['frequency'], dfm['monetary_value'])