質問編集履歴
2
変数名の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -246,7 +246,7 @@
|
|
246
246
|
|
247
247
|
svc_clf = grid_search.best_estimator_
|
248
248
|
|
249
|
-
pred_
|
249
|
+
pred_svc = svc_clf.predict(X_test)
|
250
250
|
|
251
251
|
print('test accuracy:', svc_clf.score(X_test, y_test))
|
252
252
|
|
1
TSNEによる可視化、rbfカーネルSVCのコード追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -181,3 +181,79 @@
|
|
181
181
|
```
|
182
182
|
|
183
183
|
test accuracy: 0.9688888888888889
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
### 追記
|
188
|
+
|
189
|
+
t-SNE(多様体学習)により訓練データを2次元表現に変換し可視化。rbfカーネルのSVMによる認識精度を測った所、KNNに近い精度が確認できました。
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
#### t-SNEによる変換と可視化
|
194
|
+
|
195
|
+
```python
|
196
|
+
|
197
|
+
from sklearn.manifold import TSNE
|
198
|
+
|
199
|
+
import matplotlib.pyplot as plt
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
tsne = TSNE(random_state=42)
|
204
|
+
|
205
|
+
X_train_tsne = tsne.fit_transform(X_train)
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
plt.scatter(X_train_tsne[:, 0], X_train_tsne[:, 1], c=y_train)
|
210
|
+
|
211
|
+
```
|
212
|
+
|
213
|
+
![tsne](f7be61579d701ee3d645a81225bf437f.png)
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
#### SVC
|
218
|
+
|
219
|
+
```python
|
220
|
+
|
221
|
+
from sklearn.svm import SVC
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
param_grid = [{'kernel': ['rbf'],
|
226
|
+
|
227
|
+
'C': [0.001, 0.01, 0.1, 1, 10, 100],
|
228
|
+
|
229
|
+
'gamma': [0.001, 0.01, 0.1, 1, 10, 100]},
|
230
|
+
|
231
|
+
{'kernel': ['linear'],
|
232
|
+
|
233
|
+
'C': [0.001, 0.01, 0.1, 1, 10, 100]}]
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
grid_search = GridSearchCV(SVC(random_state=42), param_grid, cv=5)
|
238
|
+
|
239
|
+
grid_search.fit(X_train, y_train)
|
240
|
+
|
241
|
+
print(f"Best parameters: {grid_search.best_params_}")
|
242
|
+
|
243
|
+
print(f"Best validation score {grid_search.best_score_}")
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
svc_clf = grid_search.best_estimator_
|
248
|
+
|
249
|
+
pred_tree = svc_clf.predict(X_test)
|
250
|
+
|
251
|
+
print('test accuracy:', svc_clf.score(X_test, y_test))
|
252
|
+
|
253
|
+
```
|
254
|
+
|
255
|
+
Best parameters: {'C': 10, 'gamma': 0.001, 'kernel': 'rbf'}
|
256
|
+
|
257
|
+
Best validation score 0.9910913140311804
|
258
|
+
|
259
|
+
test accuracy: 0.9844444444444445
|