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