回答編集履歴
1
補足を追加
test
CHANGED
@@ -23,3 +23,61 @@
|
|
23
23
|
random_state=7)
|
24
24
|
|
25
25
|
```
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
---
|
30
|
+
|
31
|
+
**【追記】**
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
> その後正解率と平均正答率を出力したいのですが
|
36
|
+
|
37
|
+
とりあえずは、こんな感じでしょうか
|
38
|
+
|
39
|
+
```Python
|
40
|
+
|
41
|
+
from sklearn.ensemble import RandomForestClassifier
|
42
|
+
|
43
|
+
from sklearn.model_selection import StratifiedKFold
|
44
|
+
|
45
|
+
from sklearn.metrics import accuracy_score
|
46
|
+
|
47
|
+
scores = []
|
48
|
+
|
49
|
+
kf = StratifiedKFold(n_splits=5, shuffle=True)
|
50
|
+
|
51
|
+
for train_index, test_index in kf.split(X, y):
|
52
|
+
|
53
|
+
train_X = X[train_index]
|
54
|
+
|
55
|
+
train_y = y[train_index]
|
56
|
+
|
57
|
+
test_X = X[test_index]
|
58
|
+
|
59
|
+
test_y = y[test_index]
|
60
|
+
|
61
|
+
lr = RandomForestClassifier(criterion='gini',
|
62
|
+
|
63
|
+
max_depth=6,
|
64
|
+
|
65
|
+
n_estimators=1000,
|
66
|
+
|
67
|
+
random_state=7)
|
68
|
+
|
69
|
+
lr.fit(train_X, train_y)
|
70
|
+
|
71
|
+
pred_y = lr.predict(test_X)
|
72
|
+
|
73
|
+
score = accuracy_score(test_y, pred_y)
|
74
|
+
|
75
|
+
scores.append(score)
|
76
|
+
|
77
|
+
print(f"SCORE : {score}")
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
print(f"Average Score : {sum(scores) / len(scores)}")
|
82
|
+
|
83
|
+
```
|