-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredit_card_fraud_detection .py
173 lines (106 loc) · 4.87 KB
/
credit_card_fraud_detection .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
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 31 16:58:30 2020
@author: shiva dumnawar
"""
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
df= pd.read_csv('creditcard.csv')
df.head()
df.info()
df.isnull().sum()
# no null values
des= df.describe()
df['Class'].value_counts()
print('valid transactions :',round(df['Class'].value_counts()[0]/len(df)*100,2),'%')
print('fraud transactions :',round(df['Class'].value_counts()[1]/len(df)*100,2),'%')
# dataset is highly imbalanced
sns.countplot(x='Class', data= df, palette='Set1')
sns.displot(x= 'Amount', data= df)
sns.displot(x= 'Time', data=df)
# scaling Amount and Time
from sklearn.preprocessing import StandardScaler
ss= StandardScaler()
df['Amount']= ss.fit_transform(df['Amount'].values.reshape(-1,1))
df['Time']= ss.fit_transform(df['Time'].values.reshape(-1,1))
df.head(10)
# Using the given imbalanced dataset causes overfitting.
# To avoid overfitting, new sub-sample is created with 492 'fraud' and 492 'no fraud' transactions.
# shuffle the dataframe
df= df.sample(frac=1)
fraud_df= df.loc[df['Class']==1]
no_fraud_df= df.loc[df['Class']==0][:492]
norm_distributed_df= pd.concat([fraud_df, no_fraud_df])
new_df= norm_distributed_df.sample(frac=1)
sns.countplot(x='Class', data= new_df, palette='Set1')
# data is normally distributed
X= new_df.iloc[:, :-1]
y= new_df.iloc[:, -1].values
#
from sklearn.ensemble import ExtraTreesClassifier
model= ExtraTreesClassifier()
model.fit(X,y)
model.feature_importances_
feat_importances= pd.Series(model.feature_importances_, index= X.columns)
plt.figure(figsize=(12,8))
feat_importances.nlargest(25).plot(kind='barh')
# correlation
c= new_df.corr()
plt.figure(figsize=(12,8))
sns.heatmap(c, cmap= 'coolwarm', annot=False )
# V4, V14, V12, V11, V17, V16 these featues are highly correlated to the class feature
# Negative correlation - V14, V12, V17, V16
# Positive correlation - V4, V11
f, axes= plt.subplots(nrows=2, ncols=3, figsize=(16,12))
sns.boxplot(x= 'Class', y= 'V4', data= new_df, palette= 'Set1', ax= axes[0][0])
axes[0][0].set_title('V4 vs Class correlation')
sns.boxplot(x= 'Class', y= 'V14', data= new_df, palette= 'Set1', ax= axes[0][1])
axes[0][1].set_title('V14 vs Class correlation')
sns.boxplot(x= 'Class', y= 'V12', data= new_df, palette= 'Set1', ax= axes[0][2])
axes[0][2].set_title('V12 vs Class correlation')
sns.boxplot(x= 'Class', y= 'V11', data= new_df, palette= 'Set1', ax= axes[1][0])
axes[1][0].set_title('V11 vs Class correlation')
sns.boxplot(x= 'Class', y= 'V17', data= new_df, palette= 'Set1', ax= axes[1][1])
axes[1][1].set_title('V17 vs Class correlation')
sns.boxplot(x= 'Class', y= 'V16', data= new_df, palette= 'Set1', ax= axes[1][2])
axes[1][2].set_title('V16 vs Class correlation')
# Using Clip() method, outliers are removed
new_df['V4']= new_df['V4'].clip(lower=new_df['V4'].quantile(.2), upper= new_df['V4'].quantile(.75))
new_df['V14']= new_df['V14'].clip(lower=new_df['V14'].quantile(.2), upper= new_df['V14'].quantile(.75))
new_df['V12']= new_df['V12'].clip(lower=new_df['V12'].quantile(.2), upper= new_df['V12'].quantile(.75))
new_df['V11']= new_df['V11'].clip(lower=new_df['V11'].quantile(.2), upper= new_df['V11'].quantile(.75))
new_df['V17']= new_df['V17'].clip(lower=new_df['V17'].quantile(.2), upper= new_df['V17'].quantile(.75))
new_df['V16']= new_df['V16'].clip(lower=new_df['V16'].quantile(.2), upper= new_df['V16'].quantile(.75))
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test= train_test_split(X, y, test_size=0.2, random_state=15)
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
# logistic regression
log_reg= LogisticRegression()
log_reg.fit(X_train, y_train)
y_pred= log_reg.predict(X_test)
from sklearn.metrics import confusion_matrix, accuracy_score
print(confusion_matrix(y_test, y_pred))
print('accuracy score : ' , accuracy_score(y_test, y_pred))
# KNN
clf= KNeighborsClassifier()
clf.fit(X_train, y_train)
y_pred_k= clf.predict(X_test)
print(confusion_matrix(y_test, y_pred_k))
print('accuracy score : ' , accuracy_score(y_test, y_pred_k))
# Support vector classifier
svm_clf= SVC().fit(X_train, y_train)
y_pred_s= svm_clf.predict(X_test)
print(confusion_matrix(y_test, y_pred_s))
print('accuracy score : ' , accuracy_score(y_test, y_pred_s))
# Decision Tree Classifier
tree= DecisionTreeClassifier().fit(X_train, y_train)
y_pred_d= tree.predict(X_test)
print(confusion_matrix(y_test, y_pred_d))
print('accuracy score : ' , accuracy_score(y_test, y_pred_d))
# logistic regression achieved higher accuracy compared to
# other three algorithms.