-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fraudulent Transactions Prediction.py
691 lines (340 loc) · 14 KB
/
Fraudulent Transactions 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
import warnings
warnings.filterwarnings('ignore')
from sklearn.model_selection import train_test_split, GridSearchCV, RandomizedSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from xgboost import XGBClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score, roc_auc_score, f1_score
from sklearn.preprocessing import MinMaxScaler
from scipy import stats
import imblearn
from imblearn.under_sampling import NearMiss
# In[2]:
plt.rcParams['figure.figsize'] = (12,8)
# In[3]:
pd.set_option('display.float_format', lambda x: '%.3f' % x)
# In[4]:
df = pd.read_csv('Fraud.csv')
df.head()
# In[5]:
df.shape
# In[6]:
df.info()
# In[7]:
df.describe()
# In[8]:
df.isnull().sum()
# In[9]:
df[df.duplicated()]
# Data is already clean as far as null and duplicate values are concerned so no data cleaning process is required over here.
# In[10]:
df.corr()
# In[11]:
df.drop(columns=['nameOrig','nameDest'],inplace=True)
# ### Eliminating outliers
# In[12]:
sns.boxenplot(df['amount'])
plt.ylabel('distribution')
plt.show()
# In[13]:
sns.boxenplot(df['oldbalanceOrg'])
plt.ylabel('distribution')
# In[14]:
sns.boxenplot(df['newbalanceOrig'])
plt.ylabel('distribution')
# In[15]:
sns.boxenplot(df['oldbalanceDest'])
plt.ylabel('distribution')
# In[16]:
sns.boxenplot(df['newbalanceDest'])
plt.ylabel('distribution')
# In[17]:
def remove_outliers(df,col):
lower_quantile = df[col].quantile(0.25)
upper_quantile = df[col].quantile(0.75)
IQR = upper_quantile - lower_quantile
lower_whisker = lower_quantile - 1.5 * IQR
upper_whisker = upper_quantile + 1.5 * IQR
temp = df.loc[(df[col]>lower_whisker)&(df[col]<upper_whisker)]
return temp[col]
# In[18]:
df['amount'] = remove_outliers(df,'amount')
df['oldbalanceOrg'] = remove_outliers(df,'oldbalanceOrg')
df['newbalanceOrig'] = remove_outliers(df,'newbalanceOrig')
df['oldbalanceDest'] = remove_outliers(df,'oldbalanceDest')
df['newbalanceDest'] = remove_outliers(df,'newbalanceDest')
# ## Exploratory Data Analysis
# In[19]:
sns.heatmap(df.corr(),annot=True,cmap='plasma')
# The only noteworthy thing from the heatmap is that the transaction recipient's old and new balances are strongly positively correlated with each other.
# In[20]:
df.groupby('isFraud').describe().T
# In[21]:
values = df['type'].value_counts().values
labels = df['type'].value_counts().keys()
explode = (0.1,0,0,0,0)
plt.pie(values,labels=labels,explode=explode,shadow=True,autopct='%1.1f%%')
plt.show()
# A vast majority of the transactions are of the type CASH_OUT having an overall proportion of a little more than one-third, closely followed by Payment mode having a share of nearly 34%. The proportion of CASH_IN transaction stood at just over one-fifth, even though, the percentage of debit and normal transfer transactions had a minimal share of less than one-tenth.
# In[22]:
values = df['isFraud'].value_counts().values
labels = ['Not Fraud','Fraud']
explode = (0.1,0)
plt.pie(values,labels=labels,explode=explode,shadow=True,autopct='%1.1f%%')
plt.show()
# Just a tiny fraction of the total transactions in the given dataset are fraud which indicates that this is a highly imbalanced dataset.
# Let's find out the maximum transferred amount by type.
# In[23]:
max_amount_type = df.groupby('type')['amount'].max().sort_values(ascending=False).reset_index()[:10]
max_amount_type
# In[24]:
sns.barplot(x='type',y='amount',data=max_amount_type,palette='magma')
# The highest amount was transferred through normal transfer mode while the least money was transferred by payment.
# In[25]:
sns.countplot(df['isFraud'])
# This is an imbalanced dataset as almost all the samples provided in the dataset belong to the majority class label 'Not Fraud'.
# In[26]:
sns.distplot(df['amount'],bins=50)
# All the balances, either old or new, of both the sender as well as the receiver have a positively right skewed distribution so let's perform normalization of each of these variables.
# In[27]:
positive_fraud_case = df[df['isFraud']==1]
sns.distplot(positive_fraud_case['amount'],bins=50)
# In[28]:
non_fraud_case = df[df['isFraud']==0]
sns.distplot(non_fraud_case['amount'],bins=50)
# In[29]:
sns.regplot(x='oldbalanceDest',y='newbalanceDest',data=df.sample(100000))
# ## Performing Min Max Normalization of the features
# In[30]:
df['amount'].fillna(df['amount'].mean(),inplace=True)
df['oldbalanceOrg'].fillna(df['oldbalanceOrg'].mean(),inplace=True)
df['newbalanceOrig'].fillna(df['newbalanceOrig'].mean(),inplace=True)
df['oldbalanceDest'].fillna(df['oldbalanceDest'].mean(),inplace=True)
df['newbalanceDest'].fillna(df['newbalanceDest'].mean(),inplace=True)
# In[31]:
payment_types = pd.get_dummies(df['type'],prefix='type',drop_first=True)
df = pd.concat([df,payment_types],axis=1)
df.head()
# In[32]:
df.drop('type',axis=1,inplace=True)
# In[33]:
df['type_CASH_OUT'] = df['type_CASH_OUT'].astype(np.int64)
df['type_DEBIT'] = df['type_DEBIT'].astype(np.int64)
df['type_PAYMENT'] = df['type_PAYMENT'].astype(np.int64)
df['type_TRANSFER'] = df['type_TRANSFER'].astype(np.int64)
# Warning: The target variable of our machine learning models is predominantly imbalanced which may hamper the predictive accuracy of the models as the predictions may be solely made on the basis of the 'majority class', thereby completely neglecting the 'minority class' as a consequence.
# In[34]:
x = df.drop('isFraud',axis=1)
y = df['isFraud']
# In[35]:
nm = NearMiss()
x_nm, y_nm = nm.fit_resample(x,y)
# # Model Training and Evaluation
# In[36]:
X = x_nm
y = y_nm
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.35,stratify=y,random_state=2022)
# In[37]:
X_train = MinMaxScaler().fit_transform(X_train)
X_test = MinMaxScaler().fit_transform(X_test)
# In[38]:
lr = LogisticRegression()
lr.fit(X_train,y_train)
lr_pred = lr.predict(X_test)
print('ROC AUC Score:',roc_auc_score(y_test,lr_pred))
print('F1 Score:',f1_score(y_test,lr_pred))
print('Confusion Matrix:\n',confusion_matrix(y_test,lr_pred))
print('Classification Report:\n',classification_report(y_test,lr_pred))
print('Accuracy Score:',accuracy_score(y_test,lr_pred))
# In[39]:
rfc = RandomForestClassifier()
rfc.fit(X_train,y_train)
# In[40]:
rfc_pred = rfc.predict(X_test)
rfc_pred
# In[41]:
print("Confusion Matrix:\n",confusion_matrix(y_test,rfc_pred))
print("Classification Report:\n",classification_report(y_test,rfc_pred))
print("ROC AUC Score:",roc_auc_score(y_test,rfc_pred))
print("F1 Score:",f1_score(y_test,rfc_pred))
print('Accuracy Score:',accuracy_score(y_test,rfc_pred))
# In[42]:
dtree = DecisionTreeClassifier()
dtree.fit(X_train,y_train)
# In[43]:
dtree_pred = dtree.predict(X_test)
dtree_pred
# In[44]:
print("ROC AUC Score:",roc_auc_score(y_test,dtree_pred))
print("Confusion Matrix:\n",confusion_matrix(y_test,dtree_pred))
print("F1 Score:",f1_score(y_test,dtree_pred))
print("Classification Report:\n",classification_report(y_test,dtree_pred))
print("Accuracy Score:",accuracy_score(y_test,dtree_pred))
# In[45]:
gnb = GaussianNB()
gnb.fit(X_train, y_train)
# In[46]:
gnb_pred = gnb.predict(X_test)
gnb_pred
# In[47]:
print("ROC AUC Score:",roc_auc_score(y_test,gnb_pred))
print("Confusion Matrix:\n",confusion_matrix(y_test,gnb_pred))
print("F1 Score:",f1_score(y_test,gnb_pred))
print("Classification Report:\n",classification_report(y_test,gnb_pred))
print("Accuracy Score:",accuracy_score(y_test,gnb_pred))
# In[48]:
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train,y_train)
# In[49]:
knn_pred = knn.predict(X_test)
knn_pred
# In[50]:
print("ROC AUC Score:",roc_auc_score(y_test,knn_pred))
print("Confusion Matrix:\n",confusion_matrix(y_test,knn_pred))
print("F1 Score:",f1_score(y_test,knn_pred))
print("Classification Report:\n",classification_report(y_test,knn_pred))
print("Accuracy Score:",accuracy_score(y_test,knn_pred))
# In[51]:
svm = SVC()
svm.fit(X_train,y_train)
# In[52]:
svm_pred = svm.predict(X_test)
svm_pred
# In[53]:
print("ROC AUC Score:",roc_auc_score(y_test,svm_pred))
print("Confusion Matrix:\n",confusion_matrix(y_test,svm_pred))
print("F1 Score:",f1_score(y_test,svm_pred))
print("Classification Report:\n",classification_report(y_test,svm_pred))
print("Accuracy Score:",accuracy_score(y_test,svm_pred))
# In[54]:
xgb = XGBClassifier()
xgb.fit(X_train,y_train)
# In[55]:
xgb_pred = xgb.predict(X_test)
xgb_pred
# In[56]:
print("ROC AUC Score:",roc_auc_score(y_test,xgb_pred))
print("Confusion Matrix:\n",confusion_matrix(y_test,xgb_pred))
print("F1 Score:",f1_score(y_test,xgb_pred))
print("Classification Report:\n",classification_report(y_test,xgb_pred))
print("Accuracy Score:",accuracy_score(y_test,xgb_pred))
# ## Hyperparameter Tuning using GridSearchCV and RandomizedSearchCV
# In[57]:
param_grid = {'C': [1,10,100,1000,10000], 'gamma': [1,0.1,0.01,0.001,0.0001], 'kernel': ['rbf']}
# In[58]:
grid_search_svm = GridSearchCV(SVC(),param_grid,refit=True,verbose=3)
grid_search_svm.fit(X_train,y_train)
# In[59]:
grid_search_svm.best_estimator_
# In[60]:
svm = SVC(C=10000,gamma=1)
svm.fit(X_train,y_train)
# In[61]:
svm_pred = svm.predict(X_test)
svm_pred
# In[62]:
print("ROC AUC Score:",roc_auc_score(y_test,svm_pred))
print("Confusion Matrix:\n",confusion_matrix(y_test,svm_pred))
print("F1 Score:",f1_score(y_test,svm_pred))
print("Classification Report:\n",classification_report(y_test,svm_pred))
print("Accuracy Score:",accuracy_score(y_test,svm_pred))
# In[63]:
param_grid = {'n_estimators': [100,200,300,400,500],
'criterion': ['gini','entropy'],
'class_weight': ['balanced','balanced_subsample']
}
# In[64]:
random_search_rfc = RandomizedSearchCV(RandomForestClassifier(),param_grid,refit=True,verbose=3)
random_search_rfc.fit(X_train,y_train)
# In[65]:
rfc = RandomForestClassifier(n_estimators=300,criterion='entropy',class_weight='balanced')
rfc.fit(X_train,y_train)
# In[66]:
rfc_pred = rfc.predict(X_test)
rfc_pred
# In[67]:
print("ROC AUC Score:",roc_auc_score(y_test,rfc_pred))
print("Confusion Matrix:\n",confusion_matrix(y_test,rfc_pred))
print("F1 Score:",f1_score(y_test,rfc_pred))
print("Classification Report:\n",classification_report(y_test,rfc_pred))
print("Accuracy Score:",accuracy_score(y_test,rfc_pred))
# In[68]:
param_grid = {'C': [1.0,2.0,3.0,4.0,5.0],
'solver': ['liblinear','sag','saga'],
'class_weight': ['balanced']}
# In[69]:
random_search_lr = RandomizedSearchCV(LogisticRegression(),param_grid,refit=True,verbose=3)
random_search_lr.fit(X_train,y_train)
# In[70]:
lr = LogisticRegression(C=5.0,solver='saga',class_weight='balanced')
lr.fit(X_train,y_train)
# In[71]:
lr_pred = lr.predict(X_test)
lr_pred
# In[72]:
print("ROC AUC Score:",roc_auc_score(y_test,lr_pred))
print("Confusion Matrix:\n",confusion_matrix(y_test,lr_pred))
print("F1 Score:",f1_score(y_test,lr_pred))
print("Classification Report:\n",classification_report(y_test,lr_pred))
print("Accuracy Score:",accuracy_score(y_test,lr_pred))
# In[73]:
param_grid = {'criterion': ['gini','entropy'], 'splitter': ['best','random']}
# In[74]:
random_search_dtree = RandomizedSearchCV(DecisionTreeClassifier(),param_grid,refit=True,verbose=3)
random_search_dtree.fit(X_train,y_train)
# In[75]:
dtree = DecisionTreeClassifier(criterion='gini',splitter='random')
dtree.fit(X_train,y_train)
# In[76]:
dtree_pred = dtree.predict(X_test)
dtree_pred
# In[77]:
print("ROC AUC Score:",roc_auc_score(y_test,dtree_pred))
print("Confusion Matrix:\n",confusion_matrix(y_test,dtree_pred))
print("F1 Score:",f1_score(y_test,dtree_pred))
print("Classification Report:\n",classification_report(y_test,dtree_pred))
print("Accuracy Score:",accuracy_score(y_test,dtree_pred))
# In[78]:
param_grid = {'n_neighbors': [1,2,3,4,5],
'weights': ['uniform','distance'],
'algorithm': ['auto','ball_tree','kd_tree','brute'],
'p': [1,2]}
# In[79]:
random_search_knn = RandomizedSearchCV(KNeighborsClassifier(),param_grid,refit=True,verbose=3)
random_search_knn.fit(X_train,y_train)
# In[80]:
knn = KNeighborsClassifier(n_neighbors=2,algorithm='auto',p=2,weights='distance')
knn.fit(X_train,y_train)
# In[86]:
knn_pred = knn.predict(X_test)
knn_pred
# In[87]:
print("ROC AUC Score:",roc_auc_score(y_test,knn_pred))
print("Confusion Matrix:\n",confusion_matrix(y_test,knn_pred))
print("F1 Score:",f1_score(y_test,knn_pred))
print("Classification Report:\n",classification_report(y_test,knn_pred))
print("Accuracy Score:",accuracy_score(y_test,knn_pred))
# ## Conclusion
# In[83]:
print("Performance of ML Models:")
print('Predictive Accuracy of Logistic Regression:',str(np.round(accuracy_score(y_test,lr_pred)*100,2)) + '%')
print('Predictive Accuracy of K Neighbors Classifier:',str(np.round(accuracy_score(y_test,knn_pred)*100,2)) + '%')
print('Predictive Accuracy of Support Vector Classifier:',str(np.round(accuracy_score(y_test,svm_pred)*100,2)) + '%')
print('Predictive Accuracy of Decision Tree Classifier:',str(np.round(accuracy_score(y_test,dtree_pred)*100,2)) + '%')
print('Predictive Accuracy of Random Forest Classifier:',str(np.round(accuracy_score(y_test,rfc_pred)*100,2)) + '%')
print('Predictive Accuracy of Gaussian Naive Bayes:',str(np.round(accuracy_score(y_test,gnb_pred)*100,2)) + '%')
print('Predictive Accuracy of XGBoost Classifier:',str(np.round(accuracy_score(y_test,xgb_pred)*100,2)) + '%')
# ### K Nearest Neighbors Classifier is the best performing model with a prediction accuracy of approximately 99%.
# ### Gaussian Naive Bayes is the worst performing model having the least prediction accuracy of just 85.81%.