-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (101 loc) · 3.89 KB
/
main.py
File metadata and controls
134 lines (101 loc) · 3.89 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
# import the necessary packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import gridspec
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, accuracy_score
from sklearn.metrics import precision_score, recall_score
from sklearn.metrics import f1_score, matthews_corrcoef
from sklearn.metrics import confusion_matrix
data = pd.read_csv("creditcard.csv")
data.head()
print(data.shape)
print(data.describe())
# shows the no. of fraud cases in dataset
fraud = data[data['Class'] == 1]
valid = data[data['Class'] == 0]
outlierFraction = len(fraud)/float(len(valid))
print(outlierFraction)
print('Fraud Cases: {}'.format(len(data[data['Class'] == 1])))
print('Valid Transactions: {}'.format(len(data[data['Class'] == 0])))
print("Amount details of the fraudulent transaction")
fraud.Amount.describe()
print("details of valid transaction")
valid.Amount.describe()
# Correlation matrix
corrmat = data.corr()
fig = plt.figure(figsize = (12, 9))
sns.heatmap(corrmat, vmax = .8, square = True)
plt.show()
# divides the X,Y from the dataset
X = data.drop(['Class'], axis = 1)
Y = data["Class"]
print(X.shape)
print(Y.shape)
# get just the values for processing
# (its a numpy array with no columns)
xData = X.values
yData = Y.values
# Split the data into training and testing sets (by using scikit-learn)
xTrain, xTest, yTrain, yTest = train_test_split(
xData, yData, test_size = 0.2, random_state = 42)
print("Number of fraud cases in the test set: {}".format(np.sum(yTest == 1)))
# Building the Random Forest Classifier (RANDOM FOREST)
# random forest model creation
rfc = RandomForestClassifier()
rfc.fit(xTrain, yTrain)
# predictions
yPred = rfc.predict(xTest)
# Evaluating the classifier
# printing every score of the classifier
# scoring in anything
# n_outliers = len(fraud)
# n_errors = (yPred != yTest).sum()
# print("The model used is Random Forest classifier")
# acc = accuracy_score(yTest, yPred)
# print("The accuracy is {}".format(acc))
# prec = precision_score(yTest, yPred)
# print("The precision is {}".format(prec))
# rec = recall_score(yTest, yPred)
# print("The recall is {}".format(rec))
# f1 = f1_score(yTest, yPred)
# print("The F1-Score is {}".format(f1))
# MCC = matthews_corrcoef(yTest, yPred)
# print("The Matthews correlation coefficient is{}".format(MCC))
# # printing the confusion matrix
# LABELS = ['Normal', 'Fraud']
# conf_matrix = confusion_matrix(yTest, yPred)
# plt.figure(figsize =(12, 12))
# sns.heatmap(conf_matrix, xticklabels = LABELS,
# yticklabels = LABELS, annot = True, fmt ="d");
# plt.title("Confusion matrix")
# plt.ylabel('True class')
# plt.xlabel('Predicted class')
# plt.show()
n_outliers = len(fraud)
n_errors = (yPred != yTest).sum()
print("Number of outliers (fraud): {}".format(n_outliers))
print("Number of errors: {}".format(n_errors))
print("The model used is Random Forest classifier")
acc = accuracy_score(yTest, yPred)
print("The accuracy is {}".format(acc))
prec = precision_score(yTest, yPred)
print("The precision is {}".format(prec))
rec = recall_score(yTest, yPred)
print("The recall is {}".format(rec))
f1 = f1_score(yTest, yPred)
print("The F1-Score is {}".format(f1))
MCC = matthews_corrcoef(yTest, yPred)
print("The Matthews correlation coefficient is {}".format(MCC))
# printing the confusion matrix
LABELS = ['Normal', 'Fraud']
conf_matrix = confusion_matrix(yTest, yPred)
plt.figure(figsize=(12, 12))
sns.heatmap(conf_matrix, xticklabels=LABELS, yticklabels=LABELS, annot=True, fmt="d");
plt.title("Confusion matrix")
plt.ylabel('True class')
plt.xlabel('Predicted class')
plt.show()