回答編集履歴
3
d
test
CHANGED
@@ -88,12 +88,12 @@
|
|
88
88
|
|
89
89
|
|
90
90
|
|
91
|
-
acc_after_
|
91
|
+
acc_after_load = loaded_model.score(X, y) # 読み込み後にスコア計算
|
92
92
|
|
93
93
|
|
94
94
|
|
95
95
|
# 一致するかどうか
|
96
96
|
|
97
|
-
print(np.isclose(acc_before_save, acc_after_
|
97
|
+
print(np.isclose(acc_before_save, acc_after_load)) # True
|
98
98
|
|
99
99
|
```
|
2
d
test
CHANGED
@@ -19,3 +19,81 @@
|
|
19
19
|
|
20
20
|
|
21
21
|
[sklearn.model_selection.GridSearchCV — scikit-learn 0.21.3 documentation](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html)
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
## 追記
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
```python
|
30
|
+
|
31
|
+
import pickle
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
from sklearn import metrics
|
36
|
+
|
37
|
+
from sklearn.datasets import make_blobs
|
38
|
+
|
39
|
+
from sklearn.model_selection import GridSearchCV
|
40
|
+
|
41
|
+
from sklearn.svm import SVC
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
# データセットを作成する。
|
46
|
+
|
47
|
+
X, y = make_blobs(n_samples=1000, centers=2, random_state=0)
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
# グリッドサーチを行う。
|
52
|
+
|
53
|
+
param_grid = {
|
54
|
+
|
55
|
+
"C": [0.1, 1, 10, 100, 1000],
|
56
|
+
|
57
|
+
"gamma": [1, 0.1, 0.01, 0.001, 0.0001],
|
58
|
+
|
59
|
+
"kernel": ["rbf"],
|
60
|
+
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
clf = GridSearchCV(SVC(), param_grid, refit=True, verbose=0, cv=3)
|
66
|
+
|
67
|
+
clf.fit(X, y)
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
filename = "ERP(SVM).sav"
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
acc_before_save = clf.score(X, y) # 保存前にスコア計算
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
# モデルを保存する。
|
80
|
+
|
81
|
+
pickle.dump(clf.best_estimator_, open(filename, "wb"))
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
# モデルを読み込む。
|
86
|
+
|
87
|
+
loaded_model = pickle.load(open(filename, "rb"))
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
acc_after_save = loaded_model.score(X, y) # 読み込み後にスコア計算
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
# 一致するかどうか
|
96
|
+
|
97
|
+
print(np.isclose(acc_before_save, acc_after_save)) # True
|
98
|
+
|
99
|
+
```
|
1
d
test
CHANGED
File without changes
|