/
Sumit minds.py
292 lines (204 loc) · 8.83 KB
/
Sumit minds.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
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
282
283
284
285
286
287
288
289
290
291
292
###########################################
############## Importing Packages #########
###########################################
import json
import pandas as pd
import numpy as np
import seaborn as sns
import seaborn as sb
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import ExtraTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.model_selection import cross_val_score
import catboost as ctb
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
from sklearn.metrics import recall_score
from sklearn.metrics import precision_score
from sklearn.utils import resample
##############################################################
######################## Importing Dataset ##################
##############################################################
cust=pd.read_csv("E:/ADM/customer.csv")
cust=cust.drop(["Unnamed: 0"],axis=1)
#Droping the variables which is not important for output variable.
cust=cust.drop(["customerDevice","customerEmail","customerIPAddress","customerPhone","customerBillingAddress","orderId","orderShippingAddress","paymentMethodId","transactionId"],axis=1)
cust.columns
#converting into binary
lb=LabelEncoder()
cust["fraudulent"]=lb.fit_transform(cust["fraudulent"])
cust["orderState"]=lb.fit_transform(cust["orderState"])
cust["transactionFailed"]=lb.fit_transform(cust["transactionFailed"])
cust["paymentMethodIssuer"]=lb.fit_transform(cust["paymentMethodIssuer"])
cust["paymentMethodProvider"]=lb.fit_transform(cust["paymentMethodProvider"])
cust["paymentMethodRegistrationFailure"]=lb.fit_transform(cust["paymentMethodRegistrationFailure"])
cust["paymentMethodType"]=lb.fit_transform(cust["paymentMethodType"])
########################################################
########################### EDA ########################
########################################################
#Identify duplicates records in the data
dupes=cust.duplicated()
sum(dupes)
#Removing Duplicates
cust1=cust.drop_duplicates() ######
cust1['fraudulent'].unique()
cust1.isnull().sum()
cust1.columns
cust1.shape
describe= cust1.describe() #mean,Std. dev, range
cust1.describe().T
cust1.median()
cust1.var()
max(cust1.orderAmount)-min(cust1.orderAmount)#range
cust1.skew()
#Finding outliers with index
def detect_outliers(x):
q1 = np.percentile(x,25)
q3 = np.percentile(x,75)
iqr = q3 - q1
lower = q1-(1.5*iqr)
upper = q3+(1.5*iqr)
outlier_indices = list(x.index[(x<lower) | (x>upper)])
outlier_value = list(x[outlier_indices])
return outlier_indices, outlier_value
indics, values = detect_outliers(cust1["orderAmount"])
print(indics)
print(values)
indics, values = detect_outliers(cust1["transactionAmount"])
print(indics)
print(values)
#histograms for each variable in df
hist =cust1.hist(bins=20,figsize =(14,14))
sns.countplot(data = cust1, x = 'fraudulent')
sns.countplot(data = cust1, x = 'paymentMethodIssuer')
sns.countplot(data = cust1, x = 'paymentMethodProvider')
sns.countplot(data = cust1, x = 'paymentMethodRegistrationFailure')
sns.countplot(data = cust1, x = 'paymentMethodType')
#create a boxplot for every column in df
boxplot = cust1.boxplot(grid=True, vert=True,fontsize=13)
#create the correlation matrix heat map
plt.figure(figsize=(14,12))
sns.heatmap(cust1.corr(),linewidths=.1,cmap="YlGnBu", annot=True)
plt.yticks(rotation=0)
#pair plots
g = sns.pairplot(cust1)
sb.factorplot(x='fraudulent' ,col='orderState' ,kind='count', data=cust1);
sb.factorplot(x='fraudulent' ,col='transactionFailed' ,kind='count', data=cust1);
sb.factorplot(x='fraudulent' ,col='paymentMethodProvider' ,kind='count', data=cust1);
sb.factorplot(x='fraudulent' ,col='paymentMethodRegistrationFailure' ,kind='count', data=cust1);
sb.factorplot(x='fraudulent' ,col='paymentMethodType' ,kind='count', data=cust1);
sns.lmplot(data= cust1, x='paymentMethodRegistrationFailure', y='paymentMethodType')
# Density Plot and Histogram of all arrival delays
sns.distplot(cust1['orderAmount'], hist=True, kde=True,
bins=int(180/5), color = 'darkblue',
hist_kws={'edgecolor':'black'},
kde_kws={'linewidth': 4})
sns.barplot(x='transactionAmount',y='transactionFailed',data=cust1)
################## Standardization ##############
def norm_func(i):
x = (i-i.mean())/(i.std())
return (x)
df_norm = norm_func(cust1.iloc[:,1:])
df_norm.describe()
### New Dataset
cust2= pd.concat([df_norm, cust1.iloc[:,0]], axis = 1)
#########################################
############## Balancing ############## : oversampling performing better than smote and undersampling
#########################################
# Separate input features and target
x = cust2.iloc[:,:8]
y = cust2.iloc[:,8]
# setting up testing and training sets
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.40, random_state=53)
# concatenate our training data back together
X = pd.concat([X_train, y_train], axis=1)
# separate minority and majority classes
not_fraud = X[X.fraudulent==0]
fraud = X[X.fraudulent==1]
# upsample minority
fraud_upsampled = resample(fraud,
replace=True, # sample with replacement
n_samples=len(not_fraud), # match number in majority class
random_state=53) # reproducible results
# combine majority and upsampled minority
upsampled = pd.concat([not_fraud, fraud_upsampled])
result = upsampled.reset_index()
######### Create New Dataframe
r2=result.drop(["index"],axis=1)
# check new class counts
r2.fraudulent.value_counts()
###################################################################
########################## Feature Selection ######################
###################################################################
#Feature Selection using Tree Classifier
a = r2.iloc[:,:8] #independent columns
b = r2.iloc[:,-1] #target column
model = ExtraTreeClassifier()
model.fit(a,b)
print(model.feature_importances_) #use inbuilt class feature_importances of tree based classifiers
#plot graph of feature importances for better visualization
feat_importances = pd.Series(model.feature_importances_, index=a.columns)
feat_importances.nlargest(8).plot(kind='barh')
#Almost all 8 variables are contributing towards output variable.
###############################################################
####################### Cross Validation ######################
###############################################################
colnames = list(r2.columns)
predictors = colnames[:8]
target = colnames[8]
Xx = cust2[predictors]
Yy = cust2[target]
#Random Forest Classifier
classifier = RandomForestClassifier(n_estimators=300, random_state=24)
all_accuracies = cross_val_score(classifier,Xx,Yy,cv=10)
print(all_accuracies)
print(all_accuracies.mean()) #70.28
#Catboost
modell = ctb.CatBoostClassifier()
all_accuraciess = cross_val_score(modell,Xx,Yy,cv=10)
print(all_accuraciess)
print(all_accuraciess.mean())#71.47
#Adaboost Classifier
abc = AdaBoostClassifier(n_estimators=50,learning_rate=0.1)
all_accuraciesss = cross_val_score(abc,Xx,Yy,cv=10)
print(all_accuraciesss)
print(all_accuraciesss.mean())#75.75
#XGBClassifier
model = XGBClassifier()
all_accuracy = cross_val_score(model,Xx,Yy,cv=10)
print(all_accuracy)
print(all_accuracy.mean()) #71.13
#Adaboost classifier is giving highest accuracy so we will build final model on this algorithm.
#############################################################################
############################## Final Model Bulding #########################
#############################################################################
#Adaboost classifier
X = r2[predictors]
Y = r2[target]
#Train Test split
train,test = train_test_split(r2,test_size = 0.4,stratify=r2.fraudulent,random_state=53)
###### Model building ######
rf =AdaBoostClassifier(n_estimators=50,learning_rate=0.1)
rf.fit(train[predictors],train[target])
rf.estimators_ #
rf.classes_ # class labels (output)
rf.predict(X)
pred_train=rf.predict(train[predictors])
pred_test = rf.predict(test[predictors])
pd.Series(pred_test).value_counts()
pd.crosstab(test[target],pred_test)
pd.crosstab(train[target],pred_train)
#f1 Score
f1_score(test[target], pred_test)#96.70
#recall
recall_score(test[target], pred_test, average='weighted') #96.51
#precision
precision_score(test[target], pred_test, average='weighted')#96.88
# Accuracy = train
np.mean(train.fraudulent == rf.predict(train[predictors]))#77.64
# Accuracy = Test
np.mean(pred_test==test.fraudulent) # 74.39
#just Good model.