質問編集履歴

2

文法修正

2020/12/02 13:57

投稿

kusegasugoi0221
kusegasugoi0221

スコア11

test CHANGED
File without changes
test CHANGED
@@ -283,3 +283,39 @@
283
283
  main()
284
284
 
285
285
  ```
286
+
287
+
288
+
289
+ ```ここに言語を入力
290
+
291
+ Ypred [1 1 1 ... 6 6 6]
292
+
293
+
294
+
295
+ Ytest
296
+
297
+ 384 1
298
+
299
+ 385 1
300
+
301
+ 386 1
302
+
303
+ 387 1
304
+
305
+ 388 1
306
+
307
+ ..
308
+
309
+ 7675 8
310
+
311
+ 7676 8
312
+
313
+ 7677 8
314
+
315
+ 7678 8
316
+
317
+ 7679 8
318
+
319
+ Name: category, Length: 1536, dtype: in
320
+
321
+ ```

1

改善

2020/12/02 13:57

投稿

kusegasugoi0221
kusegasugoi0221

スコア11

test CHANGED
File without changes
test CHANGED
@@ -49,3 +49,237 @@
49
49
  SyntaxError: invalid character in identifier
50
50
 
51
51
  ```
52
+
53
+
54
+
55
+ コード本文です。
56
+
57
+ ```ここに言語を入力
58
+
59
+ # -*- coding: utf-8 -*-
60
+
61
+
62
+
63
+ import scipy as sp
64
+
65
+ import pandas as pd
66
+
67
+ from pandas import Series, DataFrame
68
+
69
+ import matplotlib.pyplot as plt
70
+
71
+ from sklearn.preprocessing import StandardScaler
72
+
73
+ from sklearn.ensemble import RandomForestClassifier #RandomForest
74
+
75
+ from sklearn.svm import SVC # SVM用
76
+
77
+ import lightgbm as lgb
78
+
79
+ from sklearn.metrics import confusion_matrix #混同行列
80
+
81
+ from sklearn.metrics import accuracy_score, precision_score #適合率
82
+
83
+ from sklearn.metrics import recall_score, f1_score #再現率,F1スコア
84
+
85
+ from sklearn.metrics import make_scorer
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+ def SVM(X_train_std, X_test_std, Y_train, Y_test):
102
+
103
+ model_SVM = SVC(random_state=0, kernel = "rbf", C = 1000 , gamma = 0.01 )
104
+
105
+ #学習モデル構築。引数に訓練データの特徴量と、それに対応したラベル
106
+
107
+ model_SVM.fit(X_train_std,Y_train)
108
+
109
+
110
+
111
+ #予測したクラスラベル
112
+
113
+ Y_pred = model_SVM.predict(X_test_std)
114
+
115
+
116
+
117
+ # .scoreで正解率を算出。
118
+
119
+ print("\nSVM")
120
+
121
+ print("train score:",model_SVM.score(X_train_std,Y_train))
122
+
123
+ print("test score:",model_SVM.score(X_test_std,Y_test))
124
+
125
+
126
+
127
+ print("accuracy score:",accuracy_score(Y_test, Y_pred,average='micro'))
128
+
129
+ print("precision score:",precision_score(Y_test, Y_pred,average='micro'))
130
+
131
+ print("recall score:",recall_score(Y_test, Y_pred,average='micro'))
132
+
133
+ print("f1 score:",f1_score(Y_test, Y_pred,average='micro'))
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+ def GBM(X_train, X_test, Y_train, Y_test):
142
+
143
+ model_GBM = lgb.LGBMClassifier(boosting_type='gbdt', num_leaves=58,
144
+
145
+ max_depth=14, learning_rate=0.1, n_estimators=940,
146
+
147
+ min_child_samples=40, importance_type="split", random_state=0)
148
+
149
+
150
+
151
+ #学習モデル構築。引数に訓練データの特徴量と、それに対応したラベル
152
+
153
+ model_GBM.fit(X_train, Y_train)
154
+
155
+
156
+
157
+ #予測したクラスラベル
158
+
159
+ Y_pred = model_GBM.predict(X_test)
160
+
161
+
162
+
163
+ # .scoreで正解率を算出。
164
+
165
+ print("\nGBM")
166
+
167
+ print("train score:",model_GBM.score(X_train,Y_train))
168
+
169
+ print("test score:",model_GBM.score(X_test,Y_test))
170
+
171
+
172
+
173
+ print("accuracy score:",accuracy_score(Y_test, Y_pred,average='micro'))
174
+
175
+ print("precision score:",precision_score(Y_test, Y_pred,average='micro'))
176
+
177
+ print("recall score:",recall_score(Y_test, Y_pred,average='micro'))
178
+
179
+ print("f1 score:",f1_score(Y_test, Y_pred,average='micro'))
180
+
181
+
182
+
183
+
184
+
185
+
186
+
187
+
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+ def main():
198
+
199
+ """
200
+
201
+ TODO case_nameに任意の名前を指定
202
+
203
+ フォルダを統一するために以降のscriptも名前を統一する
204
+
205
+ """
206
+
207
+ case_name = "case3"
208
+
209
+ # ---------------------------------------
210
+
211
+
212
+
213
+ case_dir = "./casestudy/{}/".format(case_name)
214
+
215
+ input_csv_name = "2_extracted_features_original.csv"
216
+
217
+ input_csv_path = case_dir + input_csv_name
218
+
219
+ input_df = pd.read_csv(input_csv_path, encoding="utf-8-sig")
220
+
221
+
222
+
223
+
224
+
225
+ #すべてのデータを対象に分類を行う場合
226
+
227
+ #-----------------------------------------------------------------------
228
+
229
+ task = "all"
230
+
231
+ train_df = input_df[input_df["train_test_flag"] == 0]
232
+
233
+ test_df = input_df[input_df["train_test_flag"] == 1]
234
+
235
+ # print(train_df)
236
+
237
+ # print(test_df)
238
+
239
+
240
+
241
+ X_train = train_df.loc[:, "contrast":"inverse_difference_m_norm"]
242
+
243
+ X_test = test_df.loc[:, "contrast":"inverse_difference_m_norm"]
244
+
245
+ # print(X_train)
246
+
247
+ # print(X_test)
248
+
249
+ Y_train = train_df["category"]
250
+
251
+ Y_test = test_df["category"]
252
+
253
+ # print(Y_train)
254
+
255
+ # print(Y_test)
256
+
257
+
258
+
259
+ sc = StandardScaler()
260
+
261
+ sc.fit(X_train)
262
+
263
+ X_train_std = sc.transform(X_train)
264
+
265
+ X_test_std = sc.transform(X_test)
266
+
267
+ # print(X_train_std)
268
+
269
+ # print(X_test_std)
270
+
271
+ # #------------------------------------------------------------------------
272
+
273
+ SVM(X_train_std, X_test_std, Y_train, Y_test)
274
+
275
+ GBM(X_train, X_test, Y_train, Y_test)
276
+
277
+
278
+
279
+
280
+
281
+ if __name__ == "__main__":
282
+
283
+ main()
284
+
285
+ ```