【发布时间】:2021-09-06 20:22:36
【问题描述】:
我正在使用一组包含六列的数据,并且我正在尝试使用 xgboost 预测可以是 0,1 的目标特征。
由于我仍在学习使用 xgboost,因此我同时使用 softprob 和 softmax 作为目标。当我最后把它们一起打印时,softprob 和 softmax 的结果并不一致(意味着 xgboost 没有选择概率最高的特征)。我不明白我的代码是否有错误,或者我对 xgboost 的工作方式抱有错误的期望。你能帮我弄清楚吗?
import xgboost as xgb
x_train,x_test,y_train,y_test=train_test_split(x, y, test_size=0.33,shuffle=False)
#使用 softprob 进行预测
train = xgb.DMatrix(x_train,label=y_train)
test = xgb.DMatrix(x_test,label=y_test)
params= {
"max_depth":4,
"eta":0.3,
"objective":"multi:softprob",
"num_class":2
}
epochs=10
model=xgb.train(params,train,epochs)
predictions=model.predict(test)
dim1,dim2 = predictions.shape
predictions=np.reshape(predictions,(dim2,dim1))
y_test=y_test.to_numpy()
results = pd.DataFrame(y_test,columns=(["Actual"]))
results["0"] = predictions[0]
results["1"] = predictions[1]
#使用softmax进行预测
train = xgb.DMatrix(x_train,label=y_train)
test = xgb.DMatrix(x_test,label=y_test)
params= {
"max_depth":4,
"eta":0.3,
"objective":"multi:softmax",
"num_class":2
}
epochs=10
model=xgb.train(params,train,epochs)
predictions=model.predict(test)
results["Prediction"] = predictions
print(results)
打印结果:
Actual 0 1 Prediction
0 1.0 0.718783 0.670569 0.0
1 1.0 0.281217 0.685960 1.0
2 1.0 0.389004 0.314040 1.0
3 0.0 0.610996 0.685960 0.0
4 1.0 0.473767 0.314040 1.0
.. ... ... ... ...
456 1.0 0.504193 0.710020 0.0
457 1.0 0.495807 0.580242 0.0
458 1.0 0.253023 0.419758 1.0
459 1.0 0.746977 0.608333 0.0
460 1.0 0.329431 0.391667 0.0
[461 rows x 4 columns]
【问题讨论】:
-
您是否正在重新训练以获得 softmax 预测?看起来这是两个具有不同目标的不同模型
-
我想我必须重新训练它才能通过不同的目标。或者有没有办法同时得到 softmax 和 softprob 的结果?
-
当您重新训练时,不能保证模型/权重相同,因此请记住这一点,对于不太确定的示例,这两个模型可能有不同的预测。您应该检查每一项的准确性并选择性能最佳的一项。如果你需要两者,那么可能使用 softprob,然后在你这边的模型输出上实现 softmax。
标签: python machine-learning xgboost supervised-learning