您的位置:首页 > 其它

sklearn-GridSearchCV,CV调节超参使用方法

2017-06-10 00:12 645 查看

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的不通用例子:

#-*- coding:utf-8 -*-
import numpy as np
import pandas as pd
import scipy as sp
import copy,os,sys,psutil
import lightgbm as lgb
from lightgbm.sklearn import LGBMRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import dump_svmlight_file
from svmutil import svm_read_problem

from sklearn import  metrics   #Additional scklearn functions
from sklearn.grid_search import GridSearchCV   #Perforing grid search

from featureProject.ly_features import make_train_set
from featureProject.my_import import split_data
# from featureProject.features import TencentReport
from featureProject.my_import import feature_importance2file

def print_best_score(gsearch,param_test):
# 输出best score
print("Best score: %0.3f" % gsearch.best_score_)
print("Best parameters set:")
# 输出最佳的分类器到底使用了怎样的参数
best_parameters = gsearch.best_estimator_.get_params()
for param_name in sorted(param_test.keys()):
print("\t%s: %r" % (param_name, best_parameters[param_name]))

def lightGBM_CV():
print ('获取内存占用率: '+(str)(psutil.virtual_memory().percent)+'%')
data, labels = make_train_set(24000000,25000000)
values = data.values;
param_test = {
'max_depth': range(5,15,2),
'num_leaves': range(10,40,5),
}
estimator = LGBMRegressor(
num_leaves = 50, # cv调节50是最优值
max_depth = 13,
learning_rate =0.1,
n_estimators = 1000,
objective = 'regression',
min_child_weight = 1,
subsample = 0.8,
colsample_bytree=0.8,
nthread = 7,
)
gsearch = GridSearchCV( estimator , param_grid = param_test, scoring='roc_auc', cv=5 )
gsearch.fit( values, labels )
gsearch.grid_scores_, gsearch.best_params_, gsearch.best_score_
print_best_score(gsearch,param_test)

if __name__ == '__main__':
lightGBM_CV()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: