質問編集履歴
3
微修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -334,6 +334,14 @@
|
|
334
334
|
|
335
335
|
|
336
336
|
|
337
|
+
# print(macro_precision)
|
338
|
+
|
339
|
+
# print(macro_recall)
|
340
|
+
|
341
|
+
# print(macro_f_measure)
|
342
|
+
|
343
|
+
|
344
|
+
|
337
345
|
# 学習,評価結果を保存
|
338
346
|
|
339
347
|
with open("result.csv", "a") as f:
|
2
プログラムの全文を記載(ファイル名のみ修正あり)
test
CHANGED
File without changes
|
test
CHANGED
@@ -36,6 +36,30 @@
|
|
36
36
|
|
37
37
|
```Python3
|
38
38
|
|
39
|
+
# -*- coding: utf-8 -*-
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
import keras.backend as K
|
44
|
+
|
45
|
+
import os
|
46
|
+
|
47
|
+
import pandas as pd
|
48
|
+
|
49
|
+
from sklearn.model_selection import train_test_split
|
50
|
+
|
51
|
+
from keras.models import Sequential
|
52
|
+
|
53
|
+
from keras.layers.core import Dense, Activation
|
54
|
+
|
55
|
+
from keras.utils import np_utils
|
56
|
+
|
57
|
+
from sklearn import preprocessing
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
39
63
|
# precision, recall, f-measureを定義する
|
40
64
|
|
41
65
|
def normalize_y_pred(y_pred):
|
@@ -170,25 +194,113 @@
|
|
170
194
|
|
171
195
|
|
172
196
|
|
197
|
+
def build_multilayer_perceptron():
|
198
|
+
|
199
|
+
"""多層パーセプトロンモデルを構築"""
|
200
|
+
|
201
|
+
model = Sequential()
|
202
|
+
|
203
|
+
model.add(Dense(16, input_shape=(12, )))
|
204
|
+
|
205
|
+
model.add(Activation('relu'))
|
206
|
+
|
207
|
+
model.add(Dense(2))
|
208
|
+
|
209
|
+
model.add(Activation('softmax'))
|
210
|
+
|
211
|
+
return model
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
|
216
|
+
|
173
217
|
if __name__ == "__main__":
|
174
218
|
|
219
|
+
with open("result.csv", "w") as f:
|
220
|
+
|
221
|
+
print("date,accuracy,precision,recall,f_measure", file=f)
|
222
|
+
|
223
|
+
|
224
|
+
|
175
|
-
for i in range(1,
|
225
|
+
for i in range(1, 32):
|
226
|
+
|
176
|
-
|
227
|
+
# ファイルが存在すれば読み込み,以下を実行する
|
228
|
+
|
177
|
-
if os.path.exists(
|
229
|
+
if (os.path.exists(
|
178
230
|
|
179
231
|
os.path.join("data", "hoge", "20XX", "01",
|
180
232
|
|
181
|
-
"20XX010" + str(i) + ".txt"))
|
233
|
+
"20XX010" + str(i) + ".txt"))
|
234
|
+
|
182
|
-
|
235
|
+
or os.path.exists(
|
236
|
+
|
237
|
+
os.path.join("data", "hoge", "20XX", "01",
|
238
|
+
|
239
|
+
"20XX01" + str(i) + ".txt"))):
|
240
|
+
|
241
|
+
if i < 10:
|
242
|
+
|
183
|
-
dataset = pd.read_csv(
|
243
|
+
dataset = pd.read_csv(
|
184
|
-
|
244
|
+
|
185
|
-
os.path.join("data", "hoge", "20XX", "01",
|
245
|
+
os.path.join("data", "hoge", "20XX", "01",
|
186
|
-
|
246
|
+
|
187
|
-
"20XX010" + str(i) + ".txt"),
|
247
|
+
"20XX010" + str(i) + ".txt"),
|
188
|
-
|
248
|
+
|
189
|
-
delim_whitespace=True)
|
249
|
+
delim_whitespace=True)
|
250
|
+
|
190
|
-
|
251
|
+
else:
|
252
|
+
|
191
|
-
|
253
|
+
dataset = pd.read_csv(
|
254
|
+
|
255
|
+
os.path.join("data", "hoge", "20XX", "01",
|
256
|
+
|
257
|
+
"20XX01" + str(i) + ".txt"),
|
258
|
+
|
259
|
+
delim_whitespace=True)
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
# 説明変数,ターゲット変数を定義
|
264
|
+
|
265
|
+
X = dataset.iloc[:, [0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]
|
266
|
+
|
267
|
+
Y = dataset.iloc[:, 17]
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
# データの標準化
|
272
|
+
|
273
|
+
X = preprocessing.scale(X)
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
# ラベルをone-hot-encoding形式に変換
|
278
|
+
|
279
|
+
# 0 => [1, 0, 0]
|
280
|
+
|
281
|
+
# 1 => [0, 1, 0]
|
282
|
+
|
283
|
+
# 2 => [0, 0, 1]
|
284
|
+
|
285
|
+
Y = np_utils.to_categorical(Y)
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
# 訓練データとテストデータに分割
|
290
|
+
|
291
|
+
train_X, test_X, train_Y, test_Y = train_test_split(
|
292
|
+
|
293
|
+
X, Y, train_size=0.8, test_size=0.2)
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
# print(macro_precision)
|
298
|
+
|
299
|
+
# print(macro_recall)
|
300
|
+
|
301
|
+
# print(macro_f_measure)
|
302
|
+
|
303
|
+
|
192
304
|
|
193
305
|
# モデル構築
|
194
306
|
|
@@ -202,9 +314,7 @@
|
|
202
314
|
|
203
315
|
metrics=[
|
204
316
|
|
205
|
-
'accuracy', macro_precision, macro_recall,
|
317
|
+
'accuracy', macro_precision, macro_recall, macro_f_measure
|
206
|
-
|
207
|
-
macro_f_measure
|
208
318
|
|
209
319
|
])
|
210
320
|
|
@@ -212,9 +322,31 @@
|
|
212
322
|
|
213
323
|
# モデル訓練
|
214
324
|
|
215
|
-
model.fit(
|
216
|
-
|
217
|
-
|
325
|
+
model.fit(train_X, train_Y, epochs=1, batch_size=100, verbose=1)
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
# モデル評価
|
330
|
+
|
331
|
+
loss, accuracy, macro_precision, macro_recall, macro_f_measure = model.evaluate(
|
332
|
+
|
333
|
+
test_X, test_Y, verbose=0)
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
# 学習,評価結果を保存
|
338
|
+
|
339
|
+
with open("result.csv", "a") as f:
|
340
|
+
|
341
|
+
print(
|
342
|
+
|
343
|
+
"{0},{1},{2},{3},{4}".format(
|
344
|
+
|
345
|
+
str(i), accuracy, macro_precision, macro_recall,
|
346
|
+
|
347
|
+
macro_f_measure),
|
348
|
+
|
349
|
+
file=f)
|
218
350
|
|
219
351
|
```
|
220
352
|
|
1
# モデル訓練の箇所を追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -208,6 +208,14 @@
|
|
208
208
|
|
209
209
|
])
|
210
210
|
|
211
|
+
|
212
|
+
|
213
|
+
# モデル訓練
|
214
|
+
|
215
|
+
model.fit(
|
216
|
+
|
217
|
+
train_X, train_Y, epochs=1, batch_size=100, verbose=1)
|
218
|
+
|
211
219
|
```
|
212
220
|
|
213
221
|
|