-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCredit Card Fraud Detection.py
174 lines (110 loc) · 4.11 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
#!/usr/bin/env python
# coding: utf-8
# ### Import Libraries
# In[1]:
# Import basic libraries
import pandas as pd
from pandas.plotting import scatter_matrix
import numpy as np
import matplotlib.pyplot as plt
import os
from imblearn.over_sampling import ADASYN
from collections import Counter
import seaborn as sn
# scikit packages
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.naive_bayes import BernoulliNB
from sklearn import metrics
# settings
get_ipython().run_line_magic('matplotlib', 'inline')
sn.set_style("dark")
sn.set_palette("colorblind")
# plot functions
import plot_functions as pf
# In[2]:
# Load data
df = pd.read_csv("C:/Users/Prajwal/creditcard.csv")
# In[3]:
# View top 5 records
df.head()
# In[5]:
df.info()
# ### Explore label class
# In[6]:
print('Normal transactions count: ', df['Class'].value_counts().values[0])
print('Fraudulent transactions count: ', df['Class'].value_counts().values[1])
# ### Separate feature data (predictors) from labels
# In[7]:
# feature data (predictors)
X = df.iloc[:, :-1]
# label class
y = df['Class']
# ### Standardize data
# Scale the data to have zero mean and unit variance.
# In[8]:
scaler = StandardScaler()
scaled_X = scaler.fit_transform(X)
# ### Partition data into train and test sets
# In[9]:
# Partition data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(scaled_X, y, test_size=0.33, random_state=42)
# ### Train Models
# Three machine learning algorithms: Logistic Regression, Naive Baye, and RandomForest classifiers were trained using the processed feature data.
# In[11]:
X_train, y_train = X_res, y_res
# Train LogisticRegression Model
LGR_Classifier = LogisticRegression()
LGR_Classifier.fit(X_train, y_train);
# Train Decision Tree Model
RDF_Classifier = RandomForestClassifier(random_state=0)
RDF_Classifier.fit(X_train, y_train);
# Train Bernoulli Naive Baye Model
BNB_Classifier = BernoulliNB()
BNB_Classifier.fit(X_train, y_train);
# ### Evaluate Models
# In[12]:
# Evaluate models
modlist = [('RandomForest Classifier', RDF_Classifier),('LogisticRegression', LGR_Classifier),
('Naive Baiye Classifier', BNB_Classifier)]
models = [j for j in modlist]
print()
print('========================== Model Evaluation Results ========================' "\n")
for i, v in models:
scores = cross_val_score(v, X_train, y_train, cv=10)
accuracy = metrics.accuracy_score(y_train, v.predict(X_train))
confusion_matrix = metrics.confusion_matrix(y_train, v.predict(X_train))
classification = metrics.classification_report(y_train, v.predict(X_train))
print('===== {} ====='.format(i))
print()
print ("Cross Validation Mean Score: ", '{}%'.format(np.round(scores.mean(), 3) * 100))
print()
print ("Model Accuracy: ", '{}%'.format(np.round(accuracy, 3) * 100))
print()
print("Confusion Matrix:" "\n", confusion_matrix)
print()
print("Classification Report:" "\n", classification)
print()
# ### Test Models
# In[13]:
# Test models
classdict = {'normal':0, 'fraudulent':1}
print()
print('========================== Model Test Results ========================' "\n")
for i, v in models:
accuracy = metrics.accuracy_score(y_test, v.predict(X_test))
confusion_matrix = metrics.confusion_matrix(y_test, v.predict(X_test))
classification = metrics.classification_report(y_test, v.predict(X_test))
print('=== {} ==='.format(i))
print ("Model Accuracy: ", '{}%'.format(np.round(accuracy, 3) * 100))
print()
print("Confusion Matrix:" "\n", confusion_matrix)
print()
pf.plot_confusion_matrix(confusion_matrix, classes = list(classdict.keys()), title='Confusion Matrix Plot', cmap=plt.cm.summer)
print()
print("Classification Report:" "\n", classification)
print()
print('============================= ROC Curve ===============================' "\n")
pf.plot_roc_auc(arg1=models, arg2=X_test, arg3=y_test)