我正在使用 scikit-learn 通过逻辑回归来实现分类。
使用 predict()
函数预测类标签,而使用 predict_proba()
函数打印预测概率。
下面粘贴了代码片段:
# Partition the dataset into train and test data
X_train, X_test, y_train, y_test = train_test_split(ds_X, ds_y, test_size=0.33, random_state=42)
y_pred = logreg.predict(X_test) # Predicted class labels from test features
y_predicted_proba = logreg.predict_proba(X_test) # Predicted probabilities from test features
预测标签打印为
array([1, 1, 1, 1, 1, 1, 1, 1, 0, 1.......... and so on
相应的预测概率打印为
array([[ 0.03667012, 0.96332988],
[ 0.03638475, 0.96361525],
[ 0.03809274, 0.96190726],
[ 0.01746768, 0.98253232],
[ 0.02742639, 0.97257361],
[ 0.03676579, 0.96323421],
[ 0.02881874, 0.97118126],
[ 0.03082288, 0.96917712],
[ 0.65332179, 0.34667821],
[ 0.02091977, 0.97908023],
.
'
and so on
观察,
第一个预测标签-1
第一个预测概率 - [ 0.03667012, 0.96332988]
为什么先打印 0.03667012 而不是 0.96332988 ?
应该是另一种方式吗?
最佳答案
第 0 列是第 0 类的概率,
第 1 列是类别 1 的概率。
如果您有 n 个类别,则输出概率形状将为 (n_examples, n_classes)。
关于python - 为什么 predict_proba 函数以相反的顺序打印概率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42241991/