超参数调节

雨点打透心脏的1/2处 2022-05-19 05:26 259阅读 0赞

1.网格搜索参数GridSearchCV 类

  1. class sklearn.model_selection.GridSearchCV(estimator, param_grid, scoring=None, fit_params=None, n_jobs=1,
  2. iid=True, refit=True, cv=None, verbose=0, pre_dispatch=‘2*n_jobs’, error_score=’raise’, return_train_score=’warn’)
  1. estimator : 分类器
  2. param_grid : 需要调参的参数。格式为:param_grid = {‘criterion’: [‘gini’, ‘entropy’],
    ‘max_depth’: [2,3,4,5,6],
    ‘min_samples_split’:[2,3,4,5,6],
    ‘min_samples_leaf’:[2,3,4,5,6]
    }
  3. scoring: 模型评分标准:如下
  4. refit : 默认为True ,在搜索参数结束后,用最佳参数结果再次fit一遍全部数据集
  5. cv: 交叉验证参数,默认为3折
  6. verbose:日志冗长度,int:冗长度,0:不输出训练过程,1:偶尔输出,>1:对每个子模型都输出
  7. pre_dispatch=‘2\n_jobs’:* 指定总共分发的并行任务数。

scoring参数:分类、聚类和回归的评估方法都有。

  1. ['accuracy', 'adjusted_mutual_info_score', 'adjusted_rand_score', 'average_precision', 'completeness_score',
  2. 'explained_variance', 'f1', 'f1_macro', 'f1_micro', 'f1_samples', 'f1_weighted', 'fowlkes_mallows_score',
  3. 'homogeneity_score', 'mutual_info_score', 'neg_log_loss', 'neg_mean_absolute_error', 'neg_mean_squared_error',
  4. 'neg_mean_squared_log_error', 'neg_median_absolute_error', 'normalized_mutual_info_score', 'precision', 'precision_macro',
  5. 'precision_micro', 'precision_samples', 'precision_weighted', 'r2', 'recall', 'recall_macro', 'recall_micro',
  6. 'recall_samples', 'recall_weighted', 'roc_auc', 'v_measure_score']

具体含义可以参考:http://sklearn.apachecn.org/cn/0.19.0/modules/model_evaluation.html#scoring-parameter

2.使用网格搜索:

需要我们人工手动输入的参数称为超参数, 进行超参数的选择的过程叫做调参。

  1. #coding=gbk
  2. #调整估计器的超参数,进行超参数的选择的过程叫做调参
  3. #1,网格搜索方法 GridSearchCV
  4. from sklearn import datasets
  5. from sklearn.model_selection import train_test_split
  6. from sklearn.svm import SVC
  7. from sklearn.metrics import classification_report
  8. from sklearn.model_selection import GridSearchCV #导入网格搜索方法的包
  9. # Each datapoint is a 8x8 image of a digit. 每一个数据点都是 8x8 的像素点
  10. digits = datasets.load_digits()
  11. print(digits.data.shape) # (1797, 64)
  12. print(digits.data[:5,:])
  13. # import matplotlib.pyplot as plt
  14. # plt.gray()
  15. # plt.matshow(digits.images[3])
  16. # plt.show()
  17. n_samples = len(digits.images)
  18. print(n_samples) # 1797
  19. X = digits.data
  20. y = digits.target
  21. print(y[:5]) #[0 1 2 3 4] 对应的数字
  22. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)
  23. param_grid = [{
  24. 'kernel':['rbf'],
  25. 'gamma':[1e-3, 1e-4],
  26. 'C':[1, 10,100,1000]
  27. },{
  28. 'kernel':['linear'],
  29. 'C':[1, 10,100,1000]
  30. }]
  31. scores =['precision', 'recall']
  32. for score in scores :
  33. print('score %s' %score)
  34. print('------')
  35. clf = GridSearchCV(SVC(), param_grid, cv=5, scoring='%s_macro'%score)
  36. clf.fit(X_train, y_train)
  37. print('best params is :')
  38. print(clf.best_params_)
  39. print('grid score')
  40. print()
  41. means = clf.cv_results_['mean_test_score']
  42. stds = clf.cv_results_['std_test_score']
  43. for mean, std, params in zip(means, stds, clf.cv_results_['params']):
  44. print('%.3f (+-/%0.03f) for %r'%(mean, std*2, params))
  45. print('-----')
  46. print('classification report')
  47. y_true, y_pred = y_test, clf.predict(X_test)
  48. report = classification_report(y_true, y_pred)
  49. print(report)

输出结果:

  1. score precision
  2. ------
  3. best params is :
  4. {'kernel': 'rbf', 'gamma': 0.001, 'C': 10}
  5. grid score
  6. 0.986 (+-/0.016) for {'kernel': 'rbf', 'gamma': 0.001, 'C': 1}
  7. 0.959 (+-/0.029) for {'kernel': 'rbf', 'gamma': 0.0001, 'C': 1}
  8. 0.988 (+-/0.017) for {'kernel': 'rbf', 'gamma': 0.001, 'C': 10}
  9. 0.982 (+-/0.026) for {'kernel': 'rbf', 'gamma': 0.0001, 'C': 10}
  10. 0.988 (+-/0.017) for {'kernel': 'rbf', 'gamma': 0.001, 'C': 100}
  11. 0.982 (+-/0.025) for {'kernel': 'rbf', 'gamma': 0.0001, 'C': 100}
  12. 0.988 (+-/0.017) for {'kernel': 'rbf', 'gamma': 0.001, 'C': 1000}
  13. 0.982 (+-/0.025) for {'kernel': 'rbf', 'gamma': 0.0001, 'C': 1000}
  14. 0.975 (+-/0.014) for {'kernel': 'linear', 'C': 1}
  15. 0.975 (+-/0.014) for {'kernel': 'linear', 'C': 10}
  16. 0.975 (+-/0.014) for {'kernel': 'linear', 'C': 100}
  17. 0.975 (+-/0.014) for {'kernel': 'linear', 'C': 1000}
  18. -----
  19. classification report
  20. precision recall f1-score support
  21. 0 1.00 1.00 1.00 89
  22. 1 0.97 1.00 0.98 90
  23. 2 0.99 0.98 0.98 92
  24. 3 1.00 0.99 0.99 93
  25. 4 1.00 1.00 1.00 76
  26. 5 0.99 0.98 0.99 108
  27. 6 0.99 1.00 0.99 89
  28. 7 0.99 1.00 0.99 78
  29. 8 1.00 0.98 0.99 92
  30. 9 0.99 0.99 0.99 92
  31. avg / total 0.99 0.99 0.99 899
  32. score recall
  33. ------
  34. best params is :
  35. {'kernel': 'rbf', 'gamma': 0.001, 'C': 10}
  36. grid score
  37. 0.986 (+-/0.019) for {'kernel': 'rbf', 'gamma': 0.001, 'C': 1}
  38. 0.957 (+-/0.029) for {'kernel': 'rbf', 'gamma': 0.0001, 'C': 1}
  39. 0.987 (+-/0.019) for {'kernel': 'rbf', 'gamma': 0.001, 'C': 10}
  40. 0.981 (+-/0.028) for {'kernel': 'rbf', 'gamma': 0.0001, 'C': 10}
  41. 0.987 (+-/0.019) for {'kernel': 'rbf', 'gamma': 0.001, 'C': 100}
  42. 0.981 (+-/0.026) for {'kernel': 'rbf', 'gamma': 0.0001, 'C': 100}
  43. 0.987 (+-/0.019) for {'kernel': 'rbf', 'gamma': 0.001, 'C': 1000}
  44. 0.981 (+-/0.026) for {'kernel': 'rbf', 'gamma': 0.0001, 'C': 1000}
  45. 0.972 (+-/0.012) for {'kernel': 'linear', 'C': 1}
  46. 0.972 (+-/0.012) for {'kernel': 'linear', 'C': 10}
  47. 0.972 (+-/0.012) for {'kernel': 'linear', 'C': 100}
  48. 0.972 (+-/0.012) for {'kernel': 'linear', 'C': 1000}
  49. -----
  50. classification report
  51. precision recall f1-score support
  52. 0 1.00 1.00 1.00 89
  53. 1 0.97 1.00 0.98 90
  54. 2 0.99 0.98 0.98 92
  55. 3 1.00 0.99 0.99 93
  56. 4 1.00 1.00 1.00 76
  57. 5 0.99 0.98 0.99 108
  58. 6 0.99 1.00 0.99 89
  59. 7 0.99 1.00 0.99 78
  60. 8 1.00 0.98 0.99 92
  61. 9 0.99 0.99 0.99 92
  62. avg / total 0.99 0.99 0.99 899

实际当中有用的参数,以clf表示我们的GridSearchCV对象

clf.best_params_ 返回最好的参数

clf.best_score_ 返回最好的测试分数,它的值和 clf.cv_results_[‘mean_test_score’][dt_grid.best_index_] 是相同的。

clf.best_index_ 返回列表中分数最好的下表

clf.best_estimator_ 返回最好的模型

grid_scores_ 在sklearn 0.18中已经不赞成使用了,用下面的cv_results_来代替

clf.cv_results_ 返回使用交叉验证进行搜索的结果,它本身又是一个字典,里面又有很多内容,我们来看一下上面的clf.cv_results_.keys()里面有什么:

  1. dict_keys(
  2. ['mean_fit_time', 'std_fit_time', 'mean_score_time', 'std_score_time',
  3. 'param_C', 'param_gamma', 'param_kernel', 'params',
  4. 'split0_test_score', 'split1_test_score', 'split2_test_score', 'split3_test_score', 'split4_test_score',
  5. 'mean_test_score', 'std_test_score', 'rank_test_score',
  6. 'split0_train_score', 'split1_train_score', 'split2_train_score', 'split3_train_score', 'split4_train_score',
  7. 'mean_train_score', 'std_train_score'] )

可以分为上面几类: 第一类是时间, 第二类是参数, 第三类是测试分数,其中又分为每次交叉验证的参数和统计的参数,第四类是训练分数,其中也分为每次交叉验证的参数和统计的参数。

参考:https://www.cnblogs.com/jiaxin359/p/8641976.html#_labelTop

http://sklearn.apachecn.org/cn/0.19.0/modules/grid_search.html#grid-search-tips

发表评论

表情:
评论列表 (有 0 条评论,259人围观)

还没有评论,来说两句吧...

相关阅读

    相关 常用的参数

    一.概念     通俗的理解是神经网络中的参数指的是那些通过训练可以进行调节的参数.比如权重和偏置量.超参数指的是在训练之前需要手工设定的,不断试错调整的参数. 二.

    相关 调节心情?

    今天同事们大多来上班了,和前2天的冷清相比,热闹许多,呵呵,但其实我比较喜欢安静。一个同事早上和我打招呼,还垂头丧气的,说有3件事情很失落,我告诉他,年刚过完阿,新的一年开始了