本文共 4007 字,大约阅读时间需要 13 分钟。
一)GridSearchCV简介
网格搜索(GridSearch)用于选取模型的最优超参数。获取最优超参数的方式可以绘制验证曲线,但是验证曲线只能每次获取一个最优超参数。如果多个超参数有很多排列组合的话,就可以使用网格搜索寻求最优超参数的组合。网格搜索针对超参数组合列表中的每一个组合,实例化给定的模型,做cv次交叉验证,将平均得分最高的超参数组合作为最佳的选择,返回模型对象。
二)sklearn.model_selection.GridSearchCV参数详解
sklearn.model_selection.GridSearchCV(estimator, param_grid, scoring=None, n_jobs=None, iid=’warn’, refit=True, cv=’warn’, verbose=0, pre_dispatch=‘2*n_jobs’, error_score=’raise-deprecating’, return_train_score=False)
(1) estimator
选择使用的分类器,并且传入除需要确定最佳的参数之外的其他参数。
(2) param_grid需要最优化的参数的取值,值为字典或者列表。
(3) scoring=None模型评价标准,默认None。
根据所选模型不同,评价准则不同。比如scoring=”accuracy”。 如果是None,则使用estimator的误差估计函数。 (4) n_jobs=1 进程个数,默认为1。 若值为 -1,则用所有的CPU进行运算。 若值为1,则不进行并行运算,这样的话方便调试。(5) iid=True
默认True,为True时,默认为各个样本fold概率分布一致,误差估计为所有样本之和,而非各个fold的平均。(6) refit=True
默认为True,程序将会以交叉验证训练集得到的最佳参数,重新对所有可用的训练集与开发集进行,作为最终用于性能评估的最佳模型参数。即在搜索参数结束后,用最佳参数结果再次fit一遍全部数据集。
(7) cv=None
交叉验证参数,默认None,使用三折交叉验证。
(8) verbose=0,
verbose:日志冗长度 0:不输出训练过程, 1:偶尔输出,>1:对每个子模型都输出。(9) pre_dispatch=‘2*n_jobs’
指定总共分发的并行任务数。当n_jobs大于1时,数据将在每个运行点进行复制,这可能导致OOM,而设置pre_dispatch参数,则可以预先划分总共的job数量,使数据最多被复制pre_dispatch次
三)以鸢尾花数据集为例,基于网格搜索得到最优模型
import numpy as npimport sklearn.model_selection as msimport sklearn.svm as svm #导入svm函数from sklearn.datasets import load_iris #导入鸢尾花数据iris = load_iris()x = iris.datay = iris.target# 可以看到样本大概分为三类print(x[:5])print(y)
out:
[[5.1 3.5 1.4 0.2] [4.9 3. 1.4 0.2] [4.7 3.2 1.3 0.2] [4.6 3.1 1.5 0.2] [5. 3.6 1.4 0.2]][0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]
# 基于svm 实现分类model = svm.SVC(probability=True)# 基于网格搜索获取最优模型params = [ { 'kernel':['linear'],'C':[1,10,100,1000]}, { 'kernel':['poly'],'C':[1,10],'degree':[2,3]}, { 'kernel':['rbf'],'C':[1,10,100,1000], 'gamma':[1,0.1, 0.01, 0.001]}]model = ms.GridSearchCV(estimator=model, param_grid=params, cv=5) model.fit(x, y)
# 网格搜索训练后的副产品print("模型的最优参数:",model.best_params_)print("最优模型分数:",model.best_score_)print("最优模型对象:",model.best_estimator_)
模型的最优参数: { 'C': 1, 'kernel': 'linear'}最优模型分数: 0.98最优模型对象: SVC(C=1, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma='auto_deprecated', kernel='linear', max_iter=-1, probability=True, random_state=None, shrinking=True, tol=0.001, verbose=False)
# 输出网格搜索每组超参数的cv数据for p, s in zip(model.cv_results_['params'], model.cv_results_['mean_test_score']): print(p, s)
out:
{ 'C': 1, 'kernel': 'linear'} 0.98{ 'C': 10, 'kernel': 'linear'} 0.9733333333333334{ 'C': 100, 'kernel': 'linear'} 0.9666666666666667{ 'C': 1000, 'kernel': 'linear'} 0.9666666666666667{ 'C': 1, 'degree': 2, 'kernel': 'poly'} 0.9733333333333334{ 'C': 1, 'degree': 3, 'kernel': 'poly'} 0.9666666666666667{ 'C': 10, 'degree': 2, 'kernel': 'poly'} 0.9666666666666667{ 'C': 10, 'degree': 3, 'kernel': 'poly'} 0.9666666666666667{ 'C': 1, 'gamma': 1, 'kernel': 'rbf'} 0.9666666666666667{ 'C': 1, 'gamma': 0.1, 'kernel': 'rbf'} 0.98{ 'C': 1, 'gamma': 0.01, 'kernel': 'rbf'} 0.9333333333333333{ 'C': 1, 'gamma': 0.001, 'kernel': 'rbf'} 0.9133333333333333{ 'C': 10, 'gamma': 1, 'kernel': 'rbf'} 0.9533333333333334{ 'C': 10, 'gamma': 0.1, 'kernel': 'rbf'} 0.98{ 'C': 10, 'gamma': 0.01, 'kernel': 'rbf'} 0.98{ 'C': 10, 'gamma': 0.001, 'kernel': 'rbf'} 0.9333333333333333{ 'C': 100, 'gamma': 1, 'kernel': 'rbf'} 0.94{ 'C': 100, 'gamma': 0.1, 'kernel': 'rbf'} 0.9666666666666667{ 'C': 100, 'gamma': 0.01, 'kernel': 'rbf'} 0.98{ 'C': 100, 'gamma': 0.001, 'kernel': 'rbf'} 0.98{ 'C': 1000, 'gamma': 1, 'kernel': 'rbf'} 0.9333333333333333{ 'C': 1000, 'gamma': 0.1, 'kernel': 'rbf'} 0.9533333333333334{ 'C': 1000, 'gamma': 0.01, 'kernel': 'rbf'} 0.9666666666666667{ 'C': 1000, 'gamma': 0.001, 'kernel': 'rbf'} 0.98
转载地址:http://oxqbb.baihongyu.com/