teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

補足を追加

2019/10/10 10:32

投稿

magichan
magichan

スコア15898

answer CHANGED
@@ -10,4 +10,33 @@
10
10
  max_depth=6,
11
11
  n_estimators=1000,
12
12
  random_state=7)
13
+ ```
14
+
15
+ ---
16
+ **【追記】**
17
+
18
+ > その後正解率と平均正答率を出力したいのですが
19
+ とりあえずは、こんな感じでしょうか
20
+ ```Python
21
+ from sklearn.ensemble import RandomForestClassifier
22
+ from sklearn.model_selection import StratifiedKFold
23
+ from sklearn.metrics import accuracy_score
24
+ scores = []
25
+ kf = StratifiedKFold(n_splits=5, shuffle=True)
26
+ for train_index, test_index in kf.split(X, y):
27
+ train_X = X[train_index]
28
+ train_y = y[train_index]
29
+ test_X = X[test_index]
30
+ test_y = y[test_index]
31
+ lr = RandomForestClassifier(criterion='gini',
32
+ max_depth=6,
33
+ n_estimators=1000,
34
+ random_state=7)
35
+ lr.fit(train_X, train_y)
36
+ pred_y = lr.predict(test_X)
37
+ score = accuracy_score(test_y, pred_y)
38
+ scores.append(score)
39
+ print(f"SCORE : {score}")
40
+
41
+ print(f"Average Score : {sum(scores) / len(scores)}")
13
42
  ```