【算法_调参】sklearn_GridSearchCV,CV调节超参使用方法

GridSearchCV 简介:

GridSearchCV,它存在的意义就是自动调参,只要把参数输进去,就能给出最优化的结果和参数。但是这个方法适合于小数据集,一旦数据的量级上去了,很难得出结果。这个时候就是需要动脑筋了。数据量比较大的时候可以使用一个快速调优的方法——坐标下降。它其实是一种贪心算法:拿当前对模型影响最大的参数调优,直到最优化;再拿下一个影响最大的参数调优,如此下去,直到所有的参数调整完毕。这个方法的缺点就是可能会调到局部最优而不是全局最优,但是省时间省力,巨大的优势面前,还是试一试吧,后续可以再拿bagging再优化。回到sklearn里面的GridSearchCV,GridSearchCV用于系统地遍历多种参数组合,通过交叉验证确定最佳效果参数。

GridSearchCV官方网址:http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html

常用参数解读:

estimator :所使用的分类器,如estimator=RandomForestClassifier(min_samples_split=100,min_samples_leaf=20,max_depth=8,max_features='sqrt',random_state=10), 并且传入除需要确定最佳的参数之外的其他参数。每一个分类器都需要一个scoring参数,或者score方法。
param_grid :值为字典或者列表,即需要最优化的参数的取值,param_grid =param_test1,param_test1 = {'n_estimators':range(10,71,10)}。
scoring : 准确度评价标准,默认None,这时需要使用score函数;或者如scoring='roc_auc',根据所选模型不同,评价准则不同。字符串(函数名),或是可调用对象,需要其函数签名形如:scorer(estimator, X, y);如果是None,则使用estimator的误差估计函数。scoring参数选择如下:

参考地址:http://scikit-learn.org/stable/modules/model_evaluation.html


cv :交叉验证参数,默认None,使用三折交叉验证。指定fold数量,默认为3,也可以是yield训练/测试数据的生成器。
refit :默认为True,程序将会以交叉验证训练集得到的最佳参数,重新对所有可用的训练集与开发集进行,作为最终用于性能评估的最佳模型参数。即在搜索参数结束后,用最佳参数结果再次fit一遍全部数据集。
iid:默认True,为True时,默认为各个样本fold概率分布一致,误差估计为所有样本之和,而非各个fold的平均。
verbose:日志冗长度,int:冗长度,0:不输出训练过程,1:偶尔输出,>1:对每个子模型都输出。
n_jobs: 并行数,int:个数,-1:跟CPU核数一致, 1:默认值。
pre_dispatch:指定总共分发的并行任务数。当n_jobs大于1时,数据将在每个运行点进行复制,这可能导致OOM,而设置pre_dispatch参数,则可以预先划分总共的job数量,使数据最多被复制pre_dispatch次

常用方法:

grid.fit():运行网格搜索
grid_scores_:给出不同参数情况下的评价结果
best_params_:描述了已取得最佳结果的参数的组合
best_score_:成员提供优化过程期间观察到的最好的评分

使用gbm的不通用例子:

[python]  view plain  copy
  1. #-*- coding:utf-8 -*-  
  2. import numpy as np  
  3. import pandas as pd  
  4. import scipy as sp  
  5. import copy,os,sys,psutil  
  6. import lightgbm as lgb  
  7. from lightgbm.sklearn import LGBMRegressor  
  8. from sklearn.model_selection import GridSearchCV  
  9. from sklearn.datasets import dump_svmlight_file  
  10. from svmutil import svm_read_problem  
  11.   
  12. from sklearn import  metrics   #Additional scklearn functions  
  13. from sklearn.grid_search import GridSearchCV   #Perforing grid search  
  14.   
  15. from featureProject.ly_features import make_train_set  
  16. from featureProject.my_import import split_data  
  17. # from featureProject.features import TencentReport  
  18. from featureProject.my_import import feature_importance2file  
  19.   
  20.   
  21. def print_best_score(gsearch,param_test):  
  22.      # 输出best score  
  23.     print("Best score: %0.3f" % gsearch.best_score_)  
  24.     print("Best parameters set:")  
  25.     # 输出最佳的分类器到底使用了怎样的参数  
  26.     best_parameters = gsearch.best_estimator_.get_params()  
  27.     for param_name in sorted(param_test.keys()):  
  28.         print("\t%s: %r" % (param_name, best_parameters[param_name]))  
  29.   
  30. def lightGBM_CV():  
  31.     print ('获取内存占用率: '+(str)(psutil.virtual_memory().percent)+'%')  
  32.     data, labels = make_train_set(24000000,25000000)  
  33.     values = data.values;  
  34.     param_test = {  
  35.         'max_depth': range(5,15,2),  
  36.         'num_leaves': range(10,40,5),  
  37.     }  
  38.     estimator = LGBMRegressor(  
  39.         num_leaves = 50# cv调节50是最优值  
  40.         max_depth = 13,  
  41.         learning_rate =0.1,   
  42.         n_estimators = 1000,   
  43.         objective = 'regression',   
  44.         min_child_weight = 1,   
  45.         subsample = 0.8,  
  46.         colsample_bytree=0.8,  
  47.         nthread = 7,  
  48.     )  
  49.     gsearch = GridSearchCV( estimator , param_grid = param_test, scoring='roc_auc', cv=5 )  
  50.     gsearch.fit( values, labels )  
  51.     gsearch.grid_scores_, gsearch.best_params_, gsearch.best_score_  
  52.     print_best_score(gsearch,param_test)  
  53.   
  54.   
  55. if __name__ == '__main__':  
  56.     lightGBM_CV()  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值