本文部分查看于:https://www.cnblogs.com/techengin/p/8966735.html

使用ligthgbm做多分类问题时候(mutticlass),如果遇到

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

1、需要在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))

2、另外像

> 参数 'metric':{'l2','auc'} 这些评估函数是不能放入的;

> 还有sklearn中的模型评估准确率(可以用)其它精确率,召回率,f1都不能使用,个人觉得是因为他是多分类不能用,具体可了解这三个的原理;

 

感谢阅读,望指出理解不当之处!

 

posted on 2020-07-28 16:28  whiteooo  阅读(1746)  评论(0编辑  收藏  举报