sklearn 使用GridSearchCV实现自动调参,选出最优参数

from sklearn.grid_search import GridSearchCV
from sklearn.svm import SVC
import tflearn
import tflearn.datasets.mnist as mnist
#加载数据
x_train, y_train, x_test, y_test = mnist.load_data(one_hot=False)
#构建模型,选择核函数并训练
clf = SVC()
clf.set_params(kernel='rbf', probability=True).fit(x_train[:1000,:], y_train[:1000])
preds1 = clf.predict(x_test[:1000,:])
print("基准测试集验证得分:"+str(np.mean(preds1 == y_test[:1000])))
#设置即将要变换的参数
param_grid = {'C': [0.0001, 0.001, 0.010.1, 1, 10, 100, 1000], 'gamma': [0.001, 0.0001]}    
#构建自动调参容器,n_jobs参数支持同时多个进程运行并行测试
grid_search = GridSearchCV(clf, param_grid, n_jobs = 1, verbose=10)    
grid_search.fit(x_train[:1000,:], y_train[:1000])
#选出最优参数    
best_parameters = grid_search.best_estimator_.get_params()    
for para, val in list(best_parameters.items()):    
    print(para, val)
#使用最优参数进行训练
clf = SVC(kernel='rbf', C=best_parameters['C'], gamma=best_parameters['gamma'], probability=True).fit(x_train[:1000,:], y_train[:1000])
preds1 = clf.predict(x_test[:1000,:])
print("最优测试集验证得分:"+str(np.mean(preds1 == y_test[:1000])))

猜你喜欢

转载自blog.csdn.net/u011311291/article/details/81035122