GridSearchCV用法

# encoding:utf-8
from sklearn.model_selection import train_test_split
from sklearn import datasets, svm
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
iris = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=0)
# 设置参数调整的范围及配置
param_grid = [
  {'C': [1, 10, 100, 1000], 'kernel': ['linear']},
  {'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']},
]

svm_model = svm.SVC()

# 将超参数配置及模型放入GridSearch中进行自动搜索
clf = GridSearchCV(svm_model, param_grid, cv=5)
clf.fit(X_train, y_train)

# 获取选择的最优模型
best_model = clf.best_estimator_
# 查看选择的最优超参数配置
print(clf.best_params_)
# 预测
y_pred = best_model.predict(X_test)
print('accuracy', accuracy_score(y_test, y_pred))
 
  
结果为
{'kernel': 'linear', 'C': 10}
('accuracy', 1.0)
 
 

你可能感兴趣的:(机器学习)