在jupyterlab中使用scikit-learn版本0.22.1。我无法提供一个最小的可复制示例,但是希望这没问题,因为它更多是一个概念性问题。
我正在建立一个分类模型。我的特征在X中,目标变量在y中。我拟合了逻辑回归模型并计算预测:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
from sklearn.linear_model import LogisticRegression
logmodel = LogisticRegression(solver='liblinear')
logmodel.fit(X_train, y_train)
predictions = logmodel.predict(X_test)
现在,我想查看混淆矩阵,准确性得分,准确性得分和召回得分。因此,我运行以下命令:
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score
print(f"Confusion matrix: \n {confusion_matrix(y_test, predictions)}")
print(f"Accuracy: \t {accuracy_score(y_test, predictions):.2%}")
print(f"Precision: \t {precision_score(y_test, predictions):.3f}")
print(f"Recall: \t {recall_score(y_test, predictions):.3f}")
>> Confusion matrix:
>> [[128838 54]
>> [ 8968 279]]
>> Accuracy: 93.47%
>> Precision: 0.838
>> Recall: 0.030
召回分数应为TP /(TP + FP)= 128838 /(128838 + 8968)= 0.934923008。为什么sklearn给我0.03以便召回?我是在计算错误,还是recall_score
工作与预期不同?
您正在计算班级0的召回率。
这里的召回率为279 /(279 + 8968)= 0.03
并且精度为279 /(279 + 54)= 0.83
矩阵在这里是
true 0 | true 1
而不是相反。