clf.grid_score_が表示されない
下記のような2値分類のSVMを組んだのですが,グリッドサーチの結果を格納してあるclf.grid_score_
でエラーが出ます。
エラーメッセージ
AttributeError: 'GridSearchCV' object has no attribute 'grid_scores_'
プログラム
# -*- coding: utf-8 -*-
import numpy as np
import numpy.random as nr
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.svm import SVC
## データの読み込み
Injury = np.loadtxt("./Injury_All.csv",delimiter=',',skiprows=1)
nr.shuffle(Injury)
#怪我の有無
labels = Injury[:,0:1]
#標準化
sd = StandardScaler()
feature = sd.fit_transform(Injury[:,1:])
#2割をテストデータへ
X_train, X_test = train_test_split(feature,test_size=0.2, random_state=None)
y_train, y_test = train_test_split(labels,test_size=0.2, random_state=None)
## チューニングパラメータ
tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-1, 1e-2, 1e-3, 1e-4],
'C': [1, 10, 100, 1000]},
]
scores = ['accuracy', 'precision', 'recall']
for score in scores:
print ('\n' + '='*50)
print (score)
print ('='*50)
clf = GridSearchCV(SVC(C=1), tuned_parameters, cv=5, scoring=score, n_jobs=-1)
clf.fit(X_train, y_train)
print ("\n+ ベストパラメータ:\n")
print (clf.best_estimator_)
print("\n+ トレーニングデータでCVした時の平均スコア:\n")
for params, mean_score, all_scores in clf.grid_scores_:
print ("{:.3f} (+/- {:.3f}) for {}".format(mean_score, all_scores.std() / 2, params))
print ("\n+ テストデータでの識別結果:\n")
y_true, y_pred = y_test, clf.predict(X_test)
print (classification_report(y_true, y_pred))