-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathCustomer_NextTransaction_Prediction.py
170 lines (126 loc) · 6.47 KB
/
Customer_NextTransaction_Prediction.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# This code is used to score a deep learning model to predict the transaction amount and days of a customer.
# The model has been built on a lot of data and what you see here is just a sample of that data.
# Model : Deep Neural Network Regression
# Created on : 27-Aug-2018
# Importing all required libraries
import os
import sys
import time
import pyodbc
import pandas as pd
import numpy as np
from datetime import datetime
import json
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MinMaxScaler
from keras.models import model_from_json
sys.__stdout__ = sys.stdout
# Function for the CLV clustering model training
def clv_predict_amt(customer_id):
all_data = pd.read_csv('..\\..\\99_sample_data\\customerpreddata.csv')
all_data = all_data.fillna(0)
# Pull data for one customer (Temporary)
# all_data = all_data[all_data['AccountNo']==customer_id]
print('Cleaning and Transforming Data.')
# Convert all data to categorical
all_data = all_data.drop('lag_Datediff',1)
all_data['Gender'] = pd.Categorical(all_data.Gender).codes
all_data['ContactAvailability'] = pd.Categorical(all_data.ContactAvailability).codes
all_data['HouseType'] = pd.Categorical(all_data.HouseType).codes
all_data['HomeCountry'] = pd.Categorical(all_data.HomeCountry).codes
all_data['TransactionCountry'] = pd.Categorical(all_data.TransactionCountry).codes
all_data['TransactionCurrencyCode'] = pd.Categorical(all_data.TransactionCurrencyCode).codes
all_data['Week_Segment'] = pd.Categorical(all_data.Week_Segment).codes
all_data['Product'] = pd.Categorical(all_data.Product).codes
##################################################################
all_unique_acctno = all_data.AccountNo.unique()
temp_all_data = all_data
scalarX, scalarY = MinMaxScaler(), MinMaxScaler()
print('Attempting to read the built Keras Regression Deep Learning Model...')
############################# load json and create model
json_file = open('..\\..\\02_models\\clv_amt_dl_model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights('..\\..\\02_models\\clv_amt_dl_model.h5')
print("Loaded model from disk")
# evaluate loaded model on test data
loaded_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
#########################
for curr_act in all_unique_acctno:
all_data = temp_all_data[temp_all_data['AccountNo'] == curr_act]
# split into input (X) and output (Y) variables
X = all_data.iloc[0:len(all_data)-1,1:15] # dont consider account no
Y = all_data.iloc[0:len(all_data)-1,16] # lag transaction amount column
#print(Y.head())
loaded_model.fit(X, Y, epochs=100, verbose=0)
# Sample Testing
# split into input (Xnew) and output (Yold) variables
Xnew = all_data.tail(1) # dont consider account no
Xnew = Xnew.ix[:,1:15]
ynew = loaded_model.predict(Xnew)
print("Predicted Next Transaction (Amount) = {}".format(str(ynew)))
break # Remove this to do it for all customers. Breaking after 1 customer since this is a sample
def clv_predict_days(customer_id):
all_data = pd.read_csv('..\\..\\99_sample_data\\customerpreddata.csv')
all_data = all_data.fillna(0)
print('Cleaning and Transforming Data.')
# Convert all data to categorical
all_data = all_data.drop('lag_Txamt',1)
all_data['Gender'] = pd.Categorical(all_data.Gender).codes
all_data['ContactAvailability'] = pd.Categorical(all_data.ContactAvailability).codes
all_data['HouseType'] = pd.Categorical(all_data.HouseType).codes
all_data['HomeCountry'] = pd.Categorical(all_data.HomeCountry).codes
all_data['TransactionCountry'] = pd.Categorical(all_data.TransactionCountry).codes
all_data['TransactionCurrencyCode'] = pd.Categorical(all_data.TransactionCurrencyCode).codes
all_data['Week_Segment'] = pd.Categorical(all_data.Week_Segment).codes
all_data['Product'] = pd.Categorical(all_data.Product).codes
##################################################################
all_unique_acctno = all_data.AccountNo.unique()
temp_all_data = all_data
scalarX, scalarY = MinMaxScaler(), MinMaxScaler()
print('Attempting to read the built Keras Regression Deep Learning Model...')
############################# load json and create model
json_file = open('..\\..\\02_models\\clv_days_dl_model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights('..\\..\\02_models\\clv_days_dl_model.h5')
print("Loaded model from disk")
# evaluate loaded model on test data
loaded_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
#########################
for curr_act in all_unique_acctno:
all_data = temp_all_data[temp_all_data['AccountNo'] == curr_act]
# split into input (X) and output (Y) variables
X = all_data.iloc[0:len(all_data)-1,1:15] # dont consider account no
Y = all_data.iloc[0:len(all_data)-1,16] # lag transaction amount column
loaded_model.fit(X, Y, epochs=10, verbose=0)
# Sample Testing
# split into input (Xnew) and output (Yold) variables
Xnew = all_data.tail(1) # dont consider account no
Xnew = Xnew.ix[:,1:15]
ynew = loaded_model.predict(Xnew)
print("Customer ID = {}".format(str(curr_act)))
print("Predicted Next Transaction (Days) = {}".format(str(ynew)))
break # Remove this to do it for all customers. Breaking after 1 customer since this is a sample
print('Scoring Process Complete.')
def main():
# For testing only
customer_id = 1100722 # For training only
clv_predict_amt(customer_id)
clv_predict_days(customer_id)
if __name__ == "__main__":
main()