質問編集履歴

1

修正後のコードになります。

2023/02/09 12:31

投稿

Muuuuki
Muuuuki

スコア26

test CHANGED
File without changes
test CHANGED
@@ -29,29 +29,32 @@
29
29
  from glob import glob
30
30
  import csv
31
31
  import os
32
+ from sklearn.preprocessing import StandardScaler
32
33
 
33
34
  def load_csv(file, y_value): # ファイル名fileのCSV読み込み,1データを複数に分割する
35
+ # pandas.read_csv()を使わないで書いてみる
34
36
  _x = list() # 1ファイルのデータすべて_xに格納する
35
37
  with open(file, "r") as f:
36
38
  reader = csv.reader(f)
37
- header = next(reader)
39
+ header = next(reader) # ヘッダは読み飛ばし
38
40
  for row in reader:
39
- _x.append([float(col) for col in row[:3]])
41
+ _x.append([float(col) for col in row[8:25]])
40
42
 
41
43
  # データ分割部
42
- _x = np.array(_x) # ここの _x は pd.read_csv(file).values[:, :3] と同義だが一応フルスクラッチで書いてる
44
+ _x = np.array(_x)
43
- roll = int(len(_x) * 0.8) # データ長の8割を1データにしてrollさせる.
45
+ roll = 215
44
46
  x, y = list(), list()
45
47
  for r in range(len(_x) - roll): # 2割のデータ長さだけデータが作られる.
46
- x.append(_x[r: r + roll])
48
+ x.append(StandardScaler().fit_transform(_x[r: r + roll].reshape(-1, 1)).reshape(_x[r: r + roll].shape))
47
49
  y.append([y_value])
48
50
  return np.array(x), np.array(y)
49
51
 
50
52
  def load_data(): # 全データ読み込み
51
53
  split = 20 # 最初の20ファイルは検証用データにする
52
54
  x_train, y_train, x_valid, y_valid = list(), list(), list(), list()
53
- for name, v in zip(["normal", "abnormal"], [0, 1]): # normalは0,abnormalは1にする
55
+ for name, v in zip(["mormal", "abnormal"], [0, 1]):
54
- for i, file in enumerate(glob(f"./data/{name}/*.csv")):
56
+ for i, file in enumerate(glob(f"/content/drive/MyDrive/data/{name}/*.csv")):
57
+ print(file)
55
58
  _x, _y = load_csv(file, v)
56
59
  if i < split:
57
60
  x_valid.extend(_x)
@@ -63,13 +66,15 @@
63
66
  return map(np.array, [x_train, y_train, x_valid, y_valid])
64
67
 
65
68
  x_train, y_train, x_valid, y_valid = load_data()
69
+ print(x_train.shape)
66
70
 
67
71
  import tensorflow as tf
68
72
  from tensorflow.python import keras
69
73
  from keras.models import Sequential
70
- from keras.layers import Conv1D, Input, Flatten, Dense
74
+ from keras.layers import Conv1D, Input, Flatten, Dense, AveragePooling1D, GlobalAveragePooling1D
71
75
  from keras.optimizers import Adam
72
76
 
77
+ from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
73
78
  from sklearn.metrics import confusion_matrix
74
79
  from sklearn.metrics import accuracy_score
75
80
  from sklearn.metrics import precision_score
@@ -79,10 +84,12 @@
79
84
  # 学習モデルを作成
80
85
  model = Sequential()
81
86
  model.add(Input(shape = x_train.shape[1:]))
82
- model.add(Conv1D(8, 4, strides = 2, activation = "tanh"))
83
- model.add(Conv1D(4, 4, strides = 2, activation = "tanh"))
87
+ model.add(Conv1D(64, 8, activation = "swish", kernel_initializer = "he_uniform"))
84
- model.add(Conv1D(2, 4, strides = 2, activation = "tanh"))
85
- model.add(Flatten())
88
+ model.add(AveragePooling1D())
89
+ model.add(Conv1D(128, 4, activation = "swish", kernel_initializer = "he_uniform"))
90
+ model.add(AveragePooling1D())
91
+ model.add(Conv1D(256, 3, activation = "swish", kernel_initializer = "he_uniform"))
92
+ model.add(GlobalAveragePooling1D())
86
93
  model.add(Dense(1, activation = "sigmoid"))
87
94
 
88
95
  model.compile(
@@ -96,6 +103,7 @@
96
103
  x_train, y_train,
97
104
  batch_size = 8,
98
105
  epochs = 50,
106
+ class_weight = {0: 4, 1: 1},
99
107
  validation_data = (x_valid, y_valid)
100
108
  )
101
109
 
@@ -103,7 +111,24 @@
103
111
  # 0から1の 範囲 で得られるのでroundで 四捨五入して0か1にする
104
112
  y_pred = np.round(model.predict(x_valid, batch_size = 8))
105
113
 
114
+ #混同行列
115
+ tn, fp, fn, tp = confusion_matrix(y_valid, y_pred).ravel() #混同行列のそれぞれの結果を取得
116
+ print("TN", tn)
117
+ print("FP", fp)
118
+ print("FN", fn)
119
+ print("TP", tp)
120
+
106
121
  #正解率(Accuracy)
107
122
  score = accuracy_score(y_valid, y_pred)
108
123
  print(f"正解率:{score * 100}%")
124
+
125
+ #適合率(Precision)
126
+ print('Precision:', precision_score(y_valid,y_pred))
127
+
128
+ #再現率(Recall)
129
+ print('Recall:', recall_score(y_valid,y_pred))
130
+
131
+ #F値(F-measure)
132
+ print('F1 score:', f1_score(y_valid,y_pred))
133
+ print("Classification report")
109
134
  ```