赞
踩
https://blog.csdn.net/u012969412/article/details/72232524
首先需要介绍的工具是sklearn的模型选择API(GridSearchCV)
官网链接:http://scikit-learn.org/stable/modules/generated/sklearn.grid_search.GridSearchCV.html
第一节:GridSearchCV函数的用法
sklearn.grid_search.GridSearchCV(
estimator, # 是你要训练的模型booster
param_grid, # 字典类型的params 需要选择的超参
scoring=None, # 评判性能好坏的标准 如auc: scoring=‘roc_auc’
fit_params=None,
n_jobs=1, # 并行运行的作业数
iid=True,
refit=True,
cv=None, # 训练集与验证集的比值,相当于nfold=5
verbose=0,
pre_dispatch='2n_jobs’,
error_score=‘raise’
)
一个简单的例子如下:
#-- coding:utf-8 -*-
import pandas as pd
from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
iris = datasets.load_iris()
parameters = {
‘kernel’?‘linear’, ‘rbf’),
‘C’:[1, 2, 4],
‘gamma’:[0.125, 0.25, 0.5 ,1, 2, 4]
}
svr = svm.SVC() # 模型
clf = GridSearchCV( svr, parameters, n_jobs=4 )
clf.fit(iris.data, iris.target) #你和模型
cv_result = pd.DataFrame.from_dict( clf.cv_results_ )
with open(’./data/cv_result.csv’,‘wb’) as f: cv_result.to_csv(f)
print 'The parameters of the best model are: ’
print clf.best_params_ # 打印出最合适的模型参数
y_pred_array = clf.predict(iris.data) # 预测结果
print classification_report( y_true=iris.target, y_pred=y_pred_array )
其中 print clf.best_params_ 会打印如下信息,这是最优参数
{‘kernel’: ‘linear’, ‘C’: 2, ‘gamma’: 0.125}
print classification_report( y_true=iris.target, y_pred=y_pred_array ) 会打印如下结果
precision recall f1-score support
0 1.00 1.00 1.00 50
1 1.00 0.94 0.97 50
2 0.94 1.00 0.97 50
avg / total 0.98 0.98 0.98 150
第二节:lightGBM使用GridSearchCV调参
LGBMRegressor可以调用的参数配置如下
lightgbm.sklearn.LGBMRegressor(
boosting_type=‘gbdt’,
num_leaves=31,
max_depth=-1,
learning_rate=0.1,
n_estimators=10,
max_bin=255,
subsample_for_bin=50000,
objective=‘regression’,
min_split_gain=0,
min_child_weight=5,
min_child_samples=10,
subsample=1,
subsample_freq=1,
colsample_bytree=1,
reg_alpha=0,
reg_lambda=0,
seed=0,
nthread=-1,
silent=True,
huber_delta=1.0,
gaussian_eta=1.0,
fair_c=1.0,
poisson_max_delta_step=0.7,
drop_rate=0.1,
skip_drop=0.5,
max_drop=50,
uniform_drop=False,
xgboost_dart_mode=False
)
作者:·清尘·
来源:CSDN
原文:https://blog.csdn.net/u012969412/article/details/72232524
版权声明:本文为博主原创文章,转载请附上博文链接!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。