机器学习之网格搜索(GridSearch)及参数说明,实例演示

机器学习之网格搜索(GridSearch)及参数说明,实例演示一 GridSearchCV 简介网格搜索 GridSearch 用于选取模型的最优超参数 获取最优超参数的方式可以绘制验证曲线 但是验证曲线只能每次获取一个最优超参数 如果多个超参数有很多排列组合的话 就可以使用网格搜索寻求最优超参数的组合 网格搜索针对超参数组合列表中的每一个组合 实例化给定的模型 做 cv 次交叉验证 将平均得分最高的超参数组合作为最佳的选择 返回模型对象 GridSearc

二)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

Scoring Function Comment
Classification
‘accuracy’ metrics.accuracy_score
‘average_precision’ metrics.average_precision_score
‘f1’ metrics.f1_score for binary targets
‘f1_micro’ metrics.f1_score micro-averaged
‘f1_macro’ metrics.f1_score macro-averaged
‘f1_weighted’ metrics.f1_score weighted average
‘f1_samples’ metrics.f1_score by multilabel sample
‘neg_log_loss’ metrics.log_loss requires predict_proba support
‘precision’ etc. metrics.precision_score suffixes apply as with ‘f1’
‘roc_auc’ metrics.roc_auc_score
‘recall’ etc. metrics.recall_score suffixes apply as with ‘f1’
Clustering
‘adjusted_rand_score’ metrics.adjusted_rand_score
Regression
‘neg_mean_absolute_error’ metrics.mean_absolute_error
‘neg_mean_squared_error’ metrics.mean_squared_error
‘neg_median_absolute_error’ metrics.median_absolute_error
‘r2’ metrics.r2_score

(4) n_jobs=1 进程个数,默认为1。 若值为 -1,则用所有的CPU进行运算。 若值为1,则不进行并行运算,这样的话方便调试。 (5) iid=True

默认True,为True时,默认为各个样本fold概率分布一致,误差估计为所有样本之和,而非各个fold的平均。

(6) refit=True

默认为True,程序将会以交叉验证训练集得到的最佳参数,重新对所有可用的训练集与开发集进行,作为最终用于性能评估的最佳模型参数。即在搜索参数结束后,用最佳参数结果再次fit一遍全部数据集。

(7) cv=None

交叉验证参数,默认None,使用三折交叉验证。

(9) pre_dispatch=‘2*n_jobs’

指定总共分发的并行任务数。当n_jobs大于1时,数据将在每个运行点进行复制,这可能导致OOM,而设置pre_dispatch参数,则可以预先划分总共的job数量,使数据最多被复制pre_dispatch次

三)以鸢尾花数据集为例,基于网格搜索得到最优模型

import numpy as np import sklearn.model_selection as ms import sklearn.svm as svm #导入svm函数 from sklearn.datasets import load_iris #导入鸢尾花数据 iris = load_iris() x = iris.data y = 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_) 

out:

模型的最优参数: {'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.33334 {'C': 100, 'kernel': 'linear'} 0.66667 {'C': 1000, 'kernel': 'linear'} 0.66667 {'C': 1, 'degree': 2, 'kernel': 'poly'} 0.33334 {'C': 1, 'degree': 3, 'kernel': 'poly'} 0.66667 {'C': 10, 'degree': 2, 'kernel': 'poly'} 0.66667 {'C': 10, 'degree': 3, 'kernel': 'poly'} 0.66667 {'C': 1, 'gamma': 1, 'kernel': 'rbf'} 0.66667 {'C': 1, 'gamma': 0.1, 'kernel': 'rbf'} 0.98 {'C': 1, 'gamma': 0.01, 'kernel': 'rbf'} 0.33333 {'C': 1, 'gamma': 0.001, 'kernel': 'rbf'} 0.33333 {'C': 10, 'gamma': 1, 'kernel': 'rbf'} 0.33334 {'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.33333 {'C': 100, 'gamma': 1, 'kernel': 'rbf'} 0.94 {'C': 100, 'gamma': 0.1, 'kernel': 'rbf'} 0.66667 {'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.33333 {'C': 1000, 'gamma': 0.1, 'kernel': 'rbf'} 0.33334 {'C': 1000, 'gamma': 0.01, 'kernel': 'rbf'} 0.66667 {'C': 1000, 'gamma': 0.001, 'kernel': 'rbf'} 0.98 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/176890.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午8:47
下一篇 2026年3月26日 下午8:47


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号