回答編集履歴
4
edit
test
CHANGED
@@ -225,3 +225,9 @@
|
|
225
225
|
局在したガウシアンの場合(gamma=10000.)
|
226
226
|
|
227
227
|

|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
CVしたガウシアンの場合
|
232
|
+
|
233
|
+

|
3
edit
test
CHANGED
@@ -222,6 +222,6 @@
|
|
222
222
|
|
223
223
|
|
224
224
|
|
225
|
-
局在したガウシアン(gamma=10000.)
|
225
|
+
局在したガウシアンの場合(gamma=10000.)
|
226
226
|
|
227
227
|

|
2
edit
test
CHANGED
@@ -201,3 +201,27 @@
|
|
201
201
|
CV(get_OneHot, *load(fonehot=True))
|
202
202
|
|
203
203
|
```
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
---
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
追記
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
IrisのXに2変数を使って平面上に射影したときの境界線。
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
広がったガウシアンの場合(gamma=0.000001)
|
220
|
+
|
221
|
+

|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
局在したガウシアン(gamma=10000.)
|
226
|
+
|
227
|
+

|
1
edit
test
CHANGED
@@ -7,3 +7,197 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
sklearnのSVCはOne-vs-Oneで実装されていませんか?
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
---
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
簡単なIrisに対してのコード。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
SVCをもうすでに使っているのならマルチクラスを分類できない理由は特にないように思いましたが…
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
```python
|
26
|
+
|
27
|
+
import numpy as np
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
from sklearn.datasets import load_iris
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
from sklearn.preprocessing import OneHotEncoder
|
36
|
+
|
37
|
+
from sklearn.metrics import accuracy_score
|
38
|
+
|
39
|
+
from sklearn.model_selection import StratifiedKFold
|
40
|
+
|
41
|
+
from sklearn.model_selection import RandomizedSearchCV
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
from sklearn.svm import SVC
|
46
|
+
|
47
|
+
from sklearn.multiclass import OneVsRestClassifier, OneVsOneClassifier
|
48
|
+
|
49
|
+
from sklearn.multioutput import MultiOutputClassifier
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
def load(fonehot=False):
|
54
|
+
|
55
|
+
data = load_iris()
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
x = data['data']
|
60
|
+
|
61
|
+
y = data['target']
|
62
|
+
|
63
|
+
hs = y
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
if fonehot:
|
68
|
+
|
69
|
+
en = OneHotEncoder()
|
70
|
+
|
71
|
+
y = en.fit_transform(y.reshape(-1, 1)).toarray()
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
return x, y, hs
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
def get_SVC():
|
80
|
+
|
81
|
+
clf = SVC()
|
82
|
+
|
83
|
+
param_grid = {'kernel': ['rbf', 'linear'],
|
84
|
+
|
85
|
+
'C': np.logspace(-10, 1, 1000),
|
86
|
+
|
87
|
+
'gamma': np.logspace(-10, 1, 1000)}
|
88
|
+
|
89
|
+
clf = RandomizedSearchCV(clf, param_grid, cv=5, n_iter=100, random_state=2018)
|
90
|
+
|
91
|
+
return clf
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
def get_OvR():
|
96
|
+
|
97
|
+
clf = SVC()
|
98
|
+
|
99
|
+
clf = OneVsRestClassifier(clf)
|
100
|
+
|
101
|
+
param_grid = {'estimator__kernel': ['rbf', 'linear'],
|
102
|
+
|
103
|
+
'estimator__C': np.logspace(-10, 1, 1000),
|
104
|
+
|
105
|
+
'estimator__gamma': np.logspace(-10, 1, 1000)}
|
106
|
+
|
107
|
+
clf = RandomizedSearchCV(clf, param_grid, cv=5, n_iter=100, random_state=2018)
|
108
|
+
|
109
|
+
return clf
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
def get_OvO():
|
114
|
+
|
115
|
+
clf = SVC()
|
116
|
+
|
117
|
+
clf = OneVsOneClassifier(clf)
|
118
|
+
|
119
|
+
param_grid = {'estimator__kernel': ['rbf', 'linear'],
|
120
|
+
|
121
|
+
'estimator__C': np.logspace(-10, 1, 1000),
|
122
|
+
|
123
|
+
'estimator__gamma': np.logspace(-10, 1, 1000)}
|
124
|
+
|
125
|
+
clf = RandomizedSearchCV(clf, param_grid, cv=5, n_iter=100, random_state=2018)
|
126
|
+
|
127
|
+
return clf
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
def get_OneHot():
|
132
|
+
|
133
|
+
clf = SVC()
|
134
|
+
|
135
|
+
clf = MultiOutputClassifier(clf)
|
136
|
+
|
137
|
+
param_grid = {'estimator__kernel': ['rbf', 'linear'],
|
138
|
+
|
139
|
+
'estimator__C': np.logspace(-10, 1, 1000),
|
140
|
+
|
141
|
+
'estimator__gamma': np.logspace(-10, 1, 1000)}
|
142
|
+
|
143
|
+
clf = RandomizedSearchCV(clf, param_grid, cv=5, n_iter=100, random_state=2018)
|
144
|
+
|
145
|
+
return clf
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
def CV(get_clf, x, y, hs, n_splits=3):
|
150
|
+
|
151
|
+
kf = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=2018)
|
152
|
+
|
153
|
+
s_s = []
|
154
|
+
|
155
|
+
pss = []
|
156
|
+
|
157
|
+
for tr, ts in kf.split(x, hs):
|
158
|
+
|
159
|
+
x_ = x[tr]
|
160
|
+
|
161
|
+
y_ = y[tr]
|
162
|
+
|
163
|
+
px = x[ts]
|
164
|
+
|
165
|
+
py = y[ts]
|
166
|
+
|
167
|
+
clf = get_clf()
|
168
|
+
|
169
|
+
clf.fit(x_, y_)
|
170
|
+
|
171
|
+
s_ = accuracy_score(y_, clf.predict(x_))
|
172
|
+
|
173
|
+
ps = accuracy_score(py, clf.predict(px))
|
174
|
+
|
175
|
+
s_s.append(s_)
|
176
|
+
|
177
|
+
pss.append(ps)
|
178
|
+
|
179
|
+
print('train: {0:7.4f} {1:7.4f}'.format(np.mean(s_s), np.std(s_s)))
|
180
|
+
|
181
|
+
print('test: {0:7.4f} {1:7.4f}'.format(np.mean(pss), np.std(pss)))
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
if __name__ == '__main__':
|
186
|
+
|
187
|
+
print('SVC(Implemented with One-vs-one)')
|
188
|
+
|
189
|
+
CV(get_SVC, *load())
|
190
|
+
|
191
|
+
print('OneVsRest')
|
192
|
+
|
193
|
+
CV(get_OvR, *load())
|
194
|
+
|
195
|
+
print('OneVsOne')
|
196
|
+
|
197
|
+
CV(get_OvO, *load())
|
198
|
+
|
199
|
+
print('OneHot')
|
200
|
+
|
201
|
+
CV(get_OneHot, *load(fonehot=True))
|
202
|
+
|
203
|
+
```
|