-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path神经网络二分类.py
267 lines (250 loc) · 8.17 KB
/
神经网络二分类.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
import scipy
import pandas as pd
from scipy.io import arff
from sklearn.model_selection import train_test_split, KFold, StratifiedKFold
from sklearn.metrics import confusion_matrix, accuracy_score
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, BatchNormalization
def Aiming(y_hat, y):
'''
the “Aiming” rate (also called “Precision”) is to reflect the average ratio of the
correctly predicted labels over the predicted labels; to measure the percentage
of the predicted labels that hit the target of the real labels.
'''
n, m = y_hat.shape
sorce_k = 0
for v in range(n):
union = 0
intersection = 0
for h in range(m):
if y_hat[v, h] == 1 or y[v, h] == 1:
union += 1
if y_hat[v, h] == 1 and y[v, h] == 1:
intersection += 1
if intersection == 0:
continue
sorce_k += intersection / sum(y_hat[v])
return sorce_k / n
def Coverage(y_hat, y):
'''
The “Coverage” rate (also called “Recall”) is to reflect the average ratio of the
correctly predicted labels over the real labels; to measure the percentage of the
real labels that are covered by the hits of prediction.
'''
import numpy as np
n, m = y_hat.shape
sorce_k = 0
for v in range(n):
union = 0
intersection = 0
for h in range(m):
if y_hat[v, h] == 1 or y[v, h] == 1:
union += 1
if y_hat[v, h] == 1 and y[v, h] == 1:
intersection += 1
if intersection == 0:
continue
sorce_k += intersection / sum(y[v])
return sorce_k / n
def Accuracy(y_hat, y):
'''
The “Accuracy” rate is to reflect the average ratio of correctly predicted labels
over the total labels including correctly and incorrectly predicted labels as well
as those real labels but are missed in the prediction
'''
import numpy as np
n, m = y_hat.shape
sorce_k = 0
for v in range(n):
union = 0
intersection = 0
for h in range(m):
if y_hat[v, h] == 1 or y[v, h] == 1:
union += 1
if y_hat[v, h] == 1 and y[v, h] == 1:
intersection += 1
if intersection == 0:
continue
sorce_k += intersection / union
return sorce_k / n
def AbsoluteTrue(y_hat, y):
'''
错误一个即为零
'''
n, m = y_hat.shape
sorce_k = 0
for v in range(n):
if list(y_hat[v]) == list(y[v]):
sorce_k += 1
return sorce_k/n
def AbsoluteFalse(y_hat, y):
'''
hamming loss
'''
n, m = y_hat.shape
sorce_k = 0
union = 0
intersection = 0
for v in range(n):
if y_hat[v].all == y[v].all:
union += 1
for h in range(m):
if y_hat[v,h] == y[v,h]:
intersection += 1
break
print(n)
print(intersection)
return (n - intersection)/n
def recall(confusion_matrix):
'''recall = TP / (Tp + FN)'''
recall = confusion_matrix[0][0] / (confusion_matrix[0][0] + confusion_matrix[0][1])
return recall
def MCC(confusion_matrix):
'''MCC = (TP*TN - FP*FN) / ((TP + FP)(TP + FN)(TN + FP)(TN)) ** 0.5'''
TP = confusion_matrix[0][0]
FN = confusion_matrix[0][1]
FP = confusion_matrix[1][0]
TN = confusion_matrix[1][1]
MCC = (TP*TN - FP*FN) / ((TP + FP)*(TP + FN)*(TN + FP)*(TN+FN)) ** 0.5
return MCC
def specific(confusion_matrix):
'''sp = TN / (TN + FP)'''
TP = confusion_matrix[0][0]
FN = confusion_matrix[0][1]
FP = confusion_matrix[1][0]
TN = confusion_matrix[1][1]
sp = TN / (TN + FP)
return sp
data, meta = scipy.io.arff.loadarff("2932序列30特征 +元胞自动机.csv.arff")
df = pd.DataFrame(data)
X = df.iloc[:,0:30].values
Y = df.iloc[:,30:31].values
results = {}
confusion_matrixs = {}
kf = KFold(n_splits=5, shuffle=True)
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from keras import backend as K
K.clear_session()
tf.reset_default_graph()
counter = 1
for train,test in kf.split(X,Y):
location_confusion_matrixs = {}
# aims = []
# covs = []
# accs = []
# absts = []
# absfs = []
accs = []
mccs= []
sns = []
sps = []
X_train = X[train]
Y_train = Y[train]
X_test = X[test]
Y_test = Y[test]
Y_train = Y_train.astype(np.float64)
Y_test = Y_test.astype(np.float64).flatten()
# aim, cov, acc, abst, absf=train_deep(X_train,Y_train,X_test,Y_test)
feature_dim = X_train.shape[1]
label_dim = Y_train.shape[1]
model = Sequential()
print("create model. feature_dim ={}, label_dim ={}".format(feature_dim, label_dim))
model.add(Dense(128, activation='relu', input_dim=feature_dim))
# model.add(BatchNormalization(trainable=True))
# model.add(Dense(150, activation='relu'))
# model.add(BatchNormalization(trainable=True))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(8, activation='relu'))
# model.add(Dense(32, activation='relu'))
# model.add(BatchNormalization(trainable=True))
# model.add(Dense(50, activation='relu'))
# model.add(BatchNormalization(trainable=True))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='Adam', loss='binary_crossentropy', metrics=['accuracy'])
# model = deep_model(feature_dim,label_dim)
model.summary()
model.fit(X_train,Y_train,batch_size=8, epochs=128,validation_data=(X_test,Y_test))
prediction = model.predict(X_test)
prediction[prediction >= 0.5] = 1
prediction[prediction < 0.5] =0
# print(prediction)
cm = confusion_matrix(Y_test, prediction)
acc = accuracy_score(Y_test, prediction)
sp = specific(cm)
sn = recall(cm)
mcc = MCC(cm)
accs.append(acc)
sns.append(sn)
mccs.append(mcc)
sps.append(sp)
print("acc:\t", np.mean(accs))
print("s p:\t", np.mean(sps))
print("s n:\t", np.mean(sns))
print("mcc:\t", np.mean(mccs))
# y_hat=model.predict_proba(X_test)
# y_hat[y_hat >= 0.5] = 1
# y_hat[y_hat < 0.5] =0
# aim = Aiming(y_hat, Y_test)
# cov = Coverage(y_hat, Y_test)
# acc = Accuracy(y_hat, Y_test)
# abst = AbsoluteTrue(y_hat, Y_test)
# absf = AbsoluteFalse(y_hat, Y_test)
# aims.append(aim)
# covs.append(cov)
# accs.append(acc)
# absts.append(abst)
# absfs.append(absf)
# for location in range(Y.shape[1]):
# location_confusion_matrix = confusion_matrix(y_hat[:,location], Y_test[:,location])
# location_confusion_matrixs[str(location)] = location_confusion_matrix
# confusion_matrixs[str(counter)] = location_confusion_matrixs
# counter += 1
# print(aims)
# print(covs)
# print(accs)
# print(absts)
# print(absfs)·
# print("aim:",np.mean(aims))
# print("cov:",np.mean(covs))
# print("acc:",np.mean(accs))
# print("abst:",np.mean(absts))
# print("absf:",np.mean(absfs))
# np.save('matrixs_3.npy', confusion_matrixs)
# import numpy as np
# dic = np.load('results.npy',allow_pickle=True).item()
# print(dic)
# matrixs = np.load('matrixs_3.npy', allow_pickle=True).item()
# Acc = {}
# SN = {}
# SP = {}
# MCC = {}
# a = []
# count = 0
# for i in range(22):
# acc= 0
# sn = 0
# sp = 0
# mcc = 0
# for j in range(1, 6):
# confusion_matrix =matrixs[str(j)][str(i)]
# a.append(confusion_matrix.shape)
# TP = confusion_matrix[1, 1]
# FP = confusion_matrix[0, 1]
# FN = confusion_matrix[1, 0]
# TN = confusion_matrix[0, 0]
# acc += (TP + TN) / (TP + TN + FP + FN)
# sn += TP / (TP + FN)
# sp += TN / (TN + FP)
# mcc += ((TP * TN) - (FP * FN)) / (np.sqrt((TN + FN) * (TN + FP) * (TP + FN) * (TP + FP)))
# Acc[str(i)] = acc / 5
# SN[str(i)] = sn / 5
# SP[str(i)] = sp / 5
# MCC[str(i)] = mcc / 5
# print(Acc)
# print(SN)
# print(SP)
# print(MCC)