-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_models.py
More file actions
281 lines (212 loc) · 8.93 KB
/
final_models.py
File metadata and controls
281 lines (212 loc) · 8.93 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
# coding: utf-8
# ### Co-authored by Tanya, Bhavik, Mudit, Srihit and Jaykumar as a result of our ME781 Data Mining final project.
# Based on Shopping Data set from UCI's Machine Learning Repository
"""
1. **Predict.ai Website** <http://www.predictai.design/>
2. **GitHub Repo** <https://github.com/Tannybuoy/predictai>
3. **Demo Video** <https://www.youtube.com/watch?v=xFt4cl4daKM/>
"""
# Importing necessary libraries for running Machine Lerning models
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import RobustScaler, StandardScaler, MinMaxScaler, OneHotEncoder
from sklearn.model_selection import cross_val_score, train_test_split, cross_val_predict
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score
import xgboost as xgb
from xgboost import XGBClassifier
# === Google Drive Mount in Google Colab ===
from google.colab import drive
drive.mount('/content/drive')
# Go to directory containing the dataset
cd "/content/drive/My Drive/Colab Notebooks"
# Reading shopping data
X_train = pd.read_csv('ShoppingData.csv')
df = X_train.copy()
df.head()
# ## Producing dummy variables for categorical data and cleaning data
"""
A dummy dataframe is created to clean the dataset and preprocess
1. **Visitor** - Columns based on New, Returning or Other
2. **Month** - New columns for each month
3. **Class** - Column after changing data type to int
"""
dummiesdf = pd.get_dummies(df['VisitorType'])
df.drop('VisitorType', inplace = True, axis = 1)
df['New_Visitor'] = dummiesdf['New_Visitor']
df['Other'] = dummiesdf['Other']
df['Returning_Visitor'] = dummiesdf['Returning_Visitor']
dfmonth = pd.get_dummies(df['Month'])
df.drop('Month', inplace = True, axis = 1)
dfwithdummies = pd.concat([df, dfmonth], axis = 1, sort = False)
dfwithdummies['Class'] = df['Revenue'].astype(int)
dfwithdummies.drop('Revenue', axis = 1, inplace = True)
dfwithdummies['Weekend'] = df['Weekend'].astype(int)
dfwithdummies.drop('Returning_Visitor', axis = 1, inplace = True)
dfcleaned = dfwithdummies.copy()
X = dfcleaned.drop('Class', axis = 1)
Y = dfcleaned['Class'].copy()
# ## Checking for Collinearity Between Features and Creating Reducing Feature Size
"""
> The cor and heatmap help in visualising correlation between various
> features. Accordingly we do remove the columns/ pre-process.
"""
cor = X.corr()
sns.heatmap(cor, xticklabels=cor.columns,yticklabels=cor.columns)
def AvgMinutes(Count, Duration):
if Duration == 0:
output = 0
elif Duration != 0:
output = float(Duration)/float(Count)
return output
"""
> AvgMinutes function is used to calculate the average time
> spent by a customer on the given page. It is obtained by
> dividing the "Count" by "Duration"
"""
Columns = [['Administrative', 'Administrative_Duration'], ['Informational', 'Informational_Duration'], ['ProductRelated', 'ProductRelated_Duration']]
X['AvgAdministrative'] = X.apply(lambda x: AvgMinutes(Count = x['Administrative'], Duration = x['Administrative_Duration']), axis = 1)
X['AvgInformational'] = X.apply(lambda x: AvgMinutes(Count = x['Informational'], Duration = x['Informational_Duration']), axis = 1)
X['AvgProductRelated'] = X.apply(lambda x: AvgMinutes(Count = x['ProductRelated'], Duration = x['ProductRelated_Duration']), axis = 1)
X.drop(['Administrative', 'Administrative_Duration','Informational', 'Informational_Duration','ProductRelated', 'ProductRelated_Duration'], axis = 1, inplace = True)
"""
> Three new column features hence get added and six columns
> can now be dropped
"""
cor = X.corr()
sns.heatmap(cor, xticklabels=cor.columns,yticklabels=cor.columns)
"""
> Correlation matrix is plotted again using sns heatmap to check if the
> correlation between the above dropped six features has been dealt with
"""
# ## Quick overview of features
# Histogram of all features
for idx,column in enumerate(X.columns):
plt.figure(idx)
X.hist(column=column,grid=False)
# Checking for NA values
for i in X.columns:
print('Feature:',i)
print('# of N/A:',X[i].isna().sum())
# Visualising no of unique values and the unique values in each column of the training dataset
for i in X_train.columns:
print('####################')
print('COLUMN TITLE:',i)
print('# UNIQUE VALUES:',len(X_train[i].unique()))
print('UNIQUE VALUES:',X_train[i].unique())
print('####################')
print()
# Scaling to normalize data
X_copy = X.copy()
rc = RobustScaler()
X_rc=rc.fit_transform(X_copy)
X_rc=pd.DataFrame(X_rc,columns=X.columns)
# Plotting the histogram obtained post above processing functions
for idx,column in enumerate(X_rc.columns):
plt.figure(idx)
X_rc.hist(column=column,grid=False)
# ## Linear Model with All Features
from sklearn import linear_model
from sklearn import metrics
X_train, X_test, y_train, y_test = train_test_split(X_rc,Y,test_size=.2)
# Linear model
model = linear_model.SGDClassifier()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
# Accuracy score imported to calculate accuracy
from sklearn.metrics import accuracy_score
accuracy_score(y_test, y_pred)
# roc_auc_score imported to calculate accuracy
"""
> It illustrates in a binary classifier system the discrimination threshold
> created by plotting the true positive rate vs false positive rate
"""
from sklearn.metrics import roc_auc_score
roc_auc_score(y_test, y_pred)
# ## Random Forest with all Features
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(max_depth=17, random_state=0)
clf.fit(X_train, y_train)
y_pred1 = clf.predict(X_test)
accuracy_score(y_test, y_pred1)
roc_auc_score(y_test, y_pred1)
# ## Finding Important Features then Removing from Dataframe
from sklearn import svm
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
list_one =[]
#SelectKBest to obtain a list of importance of each feature column
feature_ranking = SelectKBest(chi2, k=5)
fit = feature_ranking.fit(X, Y)
fmt = '%-8s%-20s%s'
for i, (score, feature) in enumerate(zip(feature_ranking.scores_, X.columns)):
list_one.append((score, feature))
dfObj = pd.DataFrame(list_one)
dfObj.sort_values(by=[0], ascending = False)
#On seeing the list, we drop the ones which have a very low weightage and less importance
X_rc.drop(['Aug','TrafficType','OperatingSystems','Other','Jul'],axis=1,inplace=True)
X_train1, X_test1, y_train1, y_test1 = train_test_split(X_rc,Y,test_size=.2)
# ## Random Forest Classifier with Feature Selection Dataframe
# Now once again we run Random Forest Classifier, but after retaining only the important features as determined by SelectKBest
clf1 = RandomForestClassifier(n_estimators= 200, max_depth = 30 )
clf1.fit(X_train1, y_train1)
y_pred2 = clf1.predict(X_test1)
accuracy_score(y_test1, y_pred2)
roc_auc_score(y_test1, y_pred2)
# ## XGBoost Classifier with Feature Selection Dataframe
model = XGBClassifier(learning_rate = 0.1, n_estimators=150, min_child_weight=3, max_depth=13)
model.fit(X_train1, y_train1)
y_pred3 = model.predict(X_test1)
accuracy_score(y_test1, y_pred3)
roc_auc_score(y_test1, y_pred3)
# ## LogisticRegression with Feature Selection Dataframe
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
log_reg = LogisticRegression(solver='lbfgs',multi_class='multinomial',max_iter = 10000)
log_reg.fit(X_train1,y_train1)
y_pred4 = log_reg.predict(X_test1)
print(accuracy_score(y_pred4,y_test1))
print(roc_auc_score(y_test1, y_pred4))
# ## Gaussian Naive Bayes with Feature Selection Dataframe
from sklearn.naive_bayes import GaussianNB
GNB = GaussianNB()
GNB.fit(X_train1,y_train1)
y_pred5 = GNB.predict(X_test1)
print(accuracy_score(y_pred5,y_test1))
print(roc_auc_score(y_test1, y_pred5))
# ## KNN classifier with Feature Selection Dataframe
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors = 6)
knn.fit(X_train1,y_train1)
y_pred6 = knn.predict(X_test1)
print(accuracy_score(y_pred6,y_test1))
print(roc_auc_score(y_test1, y_pred6))
# ## SVM Classification with PCA feature reduction technique
from sklearn.decomposition import PCA
pca = PCA(n_components=15)
d=pca.fit_transform(X_train1)
e=pca.fit_transform(X_test1)
print(pca.explained_variance_ratio_.sum())
from sklearn.svm import SVC
svm = SVC()
svm.fit(d,y_train1)
y_pred7 = svm.predict(e)
print(accuracy_score(y_pred7,y_test1))
print(roc_auc_score(y_test1, y_pred7))
# ## SVM Classification with Feature Selection Dataframe
from sklearn.svm import SVC
svm = SVC()
svm.fit(X_train1,y_train1)
y_pred8 = svm.predict(X_test1)
print(accuracy_score(y_pred8,y_test1))
print(roc_auc_score(y_test1, y_pred8))
# ## Neural Network Classifier With Feature Selection Dataframe
from sklearn.neural_network import MLPClassifier
mlp = MLPClassifier(hidden_layer_sizes=(19,19,19), activation='relu', solver='adam', max_iter=500)
mlp.fit(X_train1,y_train1)
y_pred9= mlp.predict(X_test1)
print(accuracy_score(y_pred9,y_test1))
print(roc_auc_score(y_test1, y_pred9))