我在一个二分类问题上训练了一个xgboost分类器。它产生了70%准确的预测。然而,对数损失非常大,达到了9.13。我怀疑这可能是因为一些预测与目标偏差很大,但我不知道为什么会这样发生 – 其他人使用xgboost在相同数据上报告的对数损失要好得多(0.55 – 0.6)。
from readCsv import x_train, y_trainfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import accuracy_score, log_lossfrom xgboost import XGBClassifierseed=7test_size=0.09X_train, X_test, y_train, y_test = train_test_split( x_train, y_train, test_size=test_size, random_state=seed)# fit model no training datamodel = XGBClassifier(max_depth=5, learning_rate=0.02, objective= 'binary:logistic', n_estimators = 5000)model.fit(X_train, y_train)# make predictions for test datay_pred = model.predict(X_test)predictions = [round(value) for value in y_pred]accuracy = accuracy_score(y_test, predictions)print("Accuracy: %.2f%%" % (accuracy * 100.0))ll = log_loss(y_test, y_pred)print("Log_loss: %f" % ll)print(model)
产生以下输出:
Accuracy: 73.54%Log_loss: 9.139162XGBClassifier(base_score=0.5, colsample_bylevel=1, colsample_bytree=1, gamma=0, learning_rate=0.02, max_delta_step=0, max_depth=5, min_child_weight=1, missing=None, n_estimators=5000, nthread=-1, objective='binary:logistic', reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=0, silent=True, subsample=1)
有谁知道我对数损失高的原因吗?谢谢!
回答:
解决方案:使用model.predict_proba(),而不是model.predict()
这将对数损失从7+降低到了0.52,处于预期范围内。model.predict()输出的值非常大,比如1e18,看来它需要通过某个函数将其转换为有效的概率分数(在0到1之间)。