我使用 LightGBM 制作了一个二元分类模型。数据集相当不平衡,但我对它的输出很满意,但不确定如何正确校准输出概率。模型的基线得分sklearn.dummy.DummyClassifier为:
dummy = DummyClassifier(random_state=54)
dummy.fit(x_train, y_train)
dummy_pred = dummy.predict(x_test)
dummy_prob = dummy.predict_proba(x_test)
dummy_prob = dummy_prob[:,1]
print(classification_report(y_test, dummy_pred))
precision recall f1-score support
0 0.98 0.98 0.98 132274
1 0.02 0.02 0.02 2686
micro avg 0.96 0.96 0.96 134960
macro avg 0.50 0.50 0.50 134960
weighted avg 0.96 0.96 0.96 134960
模型的输出如下,结果没问题:
print(classification_report(y_test, y_pred))
precision recall f1-score support
0 1.00 0.95 0.97 132274
1 0.27 0.96 0.42 2686
micro avg 0.95 0.95 0.95 134960
macro avg 0.63 0.95 0.70 134960
weighted avg 0.98 0.95 0.96 134960
我想使用输出概率,所以我想我应该看看模型的校准情况,因为基于树的模型通常不能很好地校准。我曾经sklearn.calibration.calibration_curve绘制曲线:
import matplotlib.pyplot as plt
from sklearn.calibration import calibration_curve
gb_y, gb_x = calibration_curve(y_test, rf_probs, n_bins=10)
plt.plot([0, 1], [0, 1], linestyle='--')
# plot model reliability
plt.plot(gb_x, gb_y, marker='.')
plt.show()
我尝试对数据进行 Platt 缩放,即将逻辑拟合到验证集输出概率并将其应用于测试数据。虽然它经过了更多校准,但概率被限制在大约 0.4 的最大值。我希望输出有一个很好的范围,即概率低和高的人。
有人知道我会怎么做吗?

