-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcnn_ADSNY.py
More file actions
191 lines (146 loc) · 6.25 KB
/
cnn_ADSNY.py
File metadata and controls
191 lines (146 loc) · 6.25 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
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
import pandas as pd
import numpy as np
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler, RobustScaler
from imblearn.over_sampling import SMOTE
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, StratifiedShuffleSplit, StratifiedKFold
from sklearn.model_selection import GridSearchCV, ShuffleSplit, learning_curve, cross_val_score
from sklearn.model_selection import KFold
from sklearn.metrics import recall_score, precision_score, f1_score, accuracy_score, roc_auc_score
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from lightgbm import LGBMClassifier
from sklearn.neighbors import KNeighborsClassifier
from tensorflow.keras.optimizers import Adam
from imblearn.over_sampling import SMOTE, ADASYN
train_df = pd.read_csv('creditcard.csv')
X = train_df.drop(columns={'Class'})
y = train_df['Class']
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=0)
X_train, X_test, y_train, y_test = train_test_split(X,y,stratify=y, test_size = 0.2)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)
y_test = y_test.ravel()
y_train = y_train.ravel()
X.info()
import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize=(20,14))
corr = X.corr()
sns.heatmap(corr)
y.value_counts()
y.describe()
fraud = train_df[train_df['Class'] == 1]
valid = train_df[train_df['Class'] == 0]
print("Fraud transaction statistics")
print(fraud["Amount"].describe())
print("\nNormal transaction statistics")
print(valid["Amount"].describe())
# describes info about train and test set
print("X_train dataset: ", X_train.shape)
print("y_train dataset: ", y_train.shape)
print("X_test dataset: ", X_test.shape)
print("y_test dataset: ", y_test.shape)
print("before applying smote:",format(sum(y_train == 1)))
print("before applying smote:",format(sum(y_train == 0)))
# import SMOTE module from imblearn library
# pip install imblearn if you don't have
from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state=2, sampling_strategy= 0.7)
adasyn = ADASYN(random_state = 2, sampling_strategy=0.7)
X_train, y_train = adasyn.fit_resample(X_train,y_train)
# X_train, y_train = sm.fit_resample(X_train, y_train)
print('After applying smote X_train: {}\n'.format(X_train.shape))
print('After applying smote y_train: {}\n'.format(y_train.shape))
print("After applying smote label '1': {}\n".format(sum(y_train == 1)))
print("After applying smote label '0': {}\n".format(sum(y_train == 0)))
X_train = X_train.reshape(X_train.shape[0] , X_train.shape[1],1)
X_test = X_test.reshape(X_test.shape[0] , X_test.shape[1],1)
X_train.shape , X_test.shape
import tensorflow as tf
from tensorflow.keras.optimizers import Adam
# Initialising the CNN
classifier = tf.keras.models.Sequential()
classifier.add(tf.keras.layers.Convolution1D(32 , 2 , activation='relu',input_shape=X_train[0].shape))
classifier.add(tf.keras.layers.BatchNormalization())
classifier.add(tf.keras.layers.Dropout(0.2))
classifier.add(tf.keras.layers.Convolution1D(64 , 2 , activation='relu'))
classifier.add(tf.keras.layers.BatchNormalization())
classifier.add(tf.keras.layers.Dropout(0.2))
classifier.add(tf.keras.layers.Convolution1D(128 , 2 , activation='relu'))
classifier.add(tf.keras.layers.BatchNormalization())
classifier.add(tf.keras.layers.Dropout(0.2))
classifier.add(tf.keras.layers.Flatten())
classifier.add(tf.keras.layers.Dense(units=256, activation='relu'))
classifier.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))
classifier.compile(optimizer=Adam(lr = 0.0001), loss='binary_crossentropy', metrics=['accuracy'])
classifier.summary()
metrics = [
'acc',
tf.keras.metrics.FalseNegatives(name="fn"),
tf.keras.metrics.FalsePositives(name="fp"),
tf.keras.metrics.TrueNegatives(name="tn"),
tf.keras.metrics.TruePositives(name="tp"),
tf.keras.metrics.Precision(name="precision"),
tf.keras.metrics.Recall(name="recall"),
]
classifier.compile(optimizer= Adam(learning_rate=0.001,),
loss='binary_crossentropy',
metrics=metrics,
)
ep = 43
bs = 512
p_list = list()
r_list = list()
f_list = list()
a_list = list()
for i in range (0,5) :
history = classifier.fit(X_train, y_train, epochs=ep, batch_size=bs, validation_data=(X_test, y_test),)
score = classifier.evaluate(X_test, y_test, verbose=0)
# x_range = range(1,101)
# plt.figure(figsize=(10,10))
# plt.plot(x_range, history.history['loss'], label='Training Loss')
# plt.plot(x_range, history.history['val_loss'], label='Validation Loss')
# plt.xlabel('Epochs')
# plt.ylabel('Error')
# # 그림에 선 표시
# plt.grid(True)
# # 범례 표시: best - 자동으로 최적의 위치에
# plt.legend(loc="best")
# plt.show()
# plt.savefig('CNN_ADSNY.png')
# history = classifier.fit(X_train, y_train, batch_size = 100, epochs = 10 , validation_data=(X_test,y_test),verbose=1)
# Predicting the Test set results
y_pred = classifier.predict(X_test).flatten().round()
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
import seaborn as sns
sns.heatmap(cm, annot=True)
#find accuracy
from sklearn.metrics import accuracy_score
print('CNN:',accuracy_score(y_test,y_pred))
# find classification report
from sklearn.metrics import f1_score , precision_score , recall_score , classification_report
print('classification_report:',classification_report(y_test,y_pred))
print('f1_score:',f1_score(y_test,y_pred))
print('precision_score:',precision_score(y_test,y_pred))
print('recall_score:',recall_score(y_test,y_pred))
p_list.append(precision_score(y_test,y_pred))
r_list.append(recall_score(y_test,y_pred))
f_list.append(f1_score(y_test,y_pred))
a_list.append(accuracy_score(y_test,y_pred))
print("precision " ,np.std(p_list))
print("precision " ,np.mean(p_list))
print("recall ", np.std(r_list))
print("recall ", np.mean(r_list))
print("f1 score ", np.std(f_list))
print("f1 score ", np.mean(f_list))
print("accuracy", np.std(a_list))
print("accuracy", np.mean(a_list))