1.直接调用LightGBM内嵌的cv寻找最佳的参数n_estimators(弱分类器数目)
Otto商品分类数据
导入必要模型import lightgbm as lgbm
import pandas as pd
import numpy as np
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import log_loss
from matplotlib import pyplot
import seaborn as sns
%matplotlib inline#读取数据
dpath = './logistic/'
train = pd.read_csv(dpath + "Otto_train_test.csv")
train.head()
Variable Identification
选择该数据集是因为该数据特征单一,我们可以在特征工程方面少做些工作,集中精力放在参数调优上;
Target分布,看看各类样本分布是否均衡sns.countplot(train.target)
pyplot.xlabel('target')
pyplot.ylabel('Number of occurrences')
每类样本分布不是很均匀
特征编码# 将类别字符串变成数字
y_train = train['target'] #形式为Class_x
y_train = y_train.map(lambda s: s[6:])
y_train = y_train.map(lambda s: int(s) - 1)#将类别的形式由Class_x变为0-8之间的整数
train = train.drop(["id" , "target"] , axis = 1)
X_train = np.array(train)
默认参数,此时学习率为0.1,比较大,观察弱分类数目的大致范围(采用默认参数配置,看看模型是过拟合还是欠拟合)#直接调用lightgbm内嵌的交叉验证(cv),可对连续的n_estimators参数进行快速交叉验证
#而GridSearchCV只能对有限个参数进行交叉验证,且速度相对较慢
import json
def modelfit(params , alg , X_train , y_train , early_stopping_rounds=10):
lgbm_params = params.copy()
lgbm_params['num_class'] = 9
lgbm_params.pop('silent');
lgbmtrain = lgbm.Dataset(X_train , y_train , silent=True)
#num_boost_round为弱分类器数目,下面的代码参数里因为已经设置了early_stopping_rounds
#即性能未提升的次数超过过早停止设置的数值,则停止训练
cv_result = lgbm.cv(lgbm_params , lgbmtrain , num_boost_round=10000 , nfold=5 , stratified=True , shuffle=True , metrics='multi_logloss' , early_stopping_rounds=early_stopping_rounds , show_stdv=True , seed=0 )
print('best n_estimators:' , len(cv_result['multi_logloss-mean']))
print('best cv score:' , cv_result['multi_logloss-mean'][-1])
#cv_result.to_csv('lgbm1_nestimators.csv' , index_label='n_estimators')
json.dump(cv_result , open('lgbm_1.json' , 'w'))
#采用交叉验证得到的最佳参数n_estimators,训练模型
alg.set_params(n_estimators=len(cv_result['multi_logloss-mean']))
alg.fit(X_train , y_train)
#Predict training set:
train_predprob = alg.predict_proba(X_train)
logloss = log_loss(y_train , train_predprob)
#Print model report:
print("logloss of train :")