(原创)lightgbm 一些错误情况处理

1、做多分类问题时候(mutticlass),如果遇到

lightgbm.basic.LightGBMError: b'Number of classes should be specified and greater than 1 for multiclass training'

需要在params里面添加num_class参数项
import lightgbm as lgb
from sklearn import datasets
from sklearn.model_selection import train_test_split
iris=datasets.load_iris()
X_train,X_test,y_train,y_test=train_test_split(iris.data,iris.target,test_size=0.3)
import numpy as np
train_data=lgb.Dataset(X_train,label=y_train)
validation_data=lgb.Dataset(X_test,label=y_test)
params={
    'learning_rate':0.1,
    'lambda_l1':0.1,
    'lambda_l2':0.2,
    'max_depth':4,
    'objective':'multiclass',
    'num_class':3,  #lightgbm.basic.LightGBMError: b'Number of classes should be specified and greater than 1 for multiclass training'
}
clf=lgb.train(params,train_data,valid_sets=[validation_data])
from sklearn.metrics import roc_auc_score,accuracy_score
y_pred=clf.predict(X_test)
y_pred=[list(x).index(max(x)) for x in y_pred]
print(y_pred)
print(accuracy_score(y_test,y_pred))
posted @ 2018-04-28 11:43  tech-engin  阅读(9659)  评论(0编辑  收藏  举报