sklearn.metrics.classification可能会报告翻转后的值以进行精确度和召回率?
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.neighbors import KNeighborsClassifier
from sklearn import datasets
def calc_precision_recall(conf_matrix, class_labels):
# for each class
for i in range(len(class_labels)):
# calculate true positives
true_positives =(conf_matrix[i, i])
# false positives
false_positives = (conf_matrix[i, :].sum() - true_positives)
# false negatives
false_negatives = 0
for j in range(len(class_labels)):
false_negatives += conf_matrix[j, i]
false_negatives -= true_positives
# and finally true negatives
true_negatives= (conf_matrix.sum() - false_positives - false_negatives - true_positives)
# print calculated values
print(
"Class label", class_labels[i],
"T_positive", true_positives,
"F_positive", false_positives,
"T_negative", true_negatives,
"F_negative", false_negatives,
"\nSensitivity/recall", true_positives / (true_positives + false_negatives),
"Specificity", true_negatives / (true_negatives + false_positives),
"Precision", true_positives/(true_positives+false_positives), "\n"
)
return
# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, 0:3] # we only take the first two features.
y = iris.target
# Random_state parameter is just a random seed that can be used to reproduce these specific results.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=27)
# Instantiate a K-Nearest Neighbors Classifier:
KNN_model = KNeighborsClassifier(n_neighbors=2)
# Fit the classifiers:
KNN_model.fit(X_train, y_train)
# Predict and store the prediction:
KNN_prediction = KNN_model.predict(X_test)
# Generate the confusion matrix
conf_matrix = confusion_matrix(KNN_prediction, y_test)
# Print the classification report
print(classification_report(KNN_prediction, y_test))
# Dummy class labels for the three iris classes
class_labels = [0,1,2]
# Own function to calculate precision and recall from the confusion matrix
calc_precision_recall(conf_matrix, class_labels)
我的函数为每个类返回以下内容:
类标签0 T_正7 F_正0 T_负23 F_负0
灵敏度/召回率1.0特异性1.0精度1.0
类标签1 T_正11 F_正1 T_负18 F_负0
灵敏度/召回率1.0特异性0.9473684210526315精度0.9166666666666666
类标签2 T_positive 11 F_positive 0 T_negative 18 F_negative 1
灵敏度/召回率0.9166666666666666特异性1.0精度1.0
precision recall
0 1.00 1.00
1 0.92 1.00
2 1.00 0.92
我的功能假定混淆矩阵由顶部x轴的实际值和左侧y轴的预测值构成。 该结构与Wikipedia中使用的结构相同,并且文档中为混淆矩阵函数引用的结构与之相同。
相比之下,这些是sklearn.metrics报告的结果。
precision recall f1-score support
0 1.00 1.00 1.00 7
1 1.00 0.92 0.96 12
2 0.92 1.00 0.96 11
系统:
python:3.8.1(默认,2020年1月8日,22:29:32)[GCC 7.3.0]
可执行文件:/ home / will / anaconda3 / envs / ElStatLearn / bin / python
机器:Linux-4.15.0-91-generic-x86_64-with-glibc2.10
Python依赖项:
点:20.0.2
设置工具:38.2.5
sklearn:0.22.1
numpy的:1.18.1
scipy:1.4.1
Cython:无
熊猫:1.0.1
matplotlib:3.1.3
作业库:0.14.1
使用OpenMP构建:True
我认为y_test
应该在print(classification_report(KNN_prediction, y_test))
在第一位。
因此: print(classification_report(y_test, KNN_prediction))
。
函数sklearn.metrics.classification_report(y_true, y_pred, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn')
具有y_true
作为第一个参数。 这将翻转精度并召回。
编辑:您的混淆矩阵也向后,但是可以解决,因为sklearn的混淆矩阵是从Wikipedia向后的。
>>> from sklearn.metrics import confusion_matrix
>>> y_true = [2, 0, 2, 2, 0, 1]
>>> y_pred = [0, 0, 2, 2, 0, 2]
>>> confusion_matrix(y_true, y_pred)
array([[2, 0, 0],
[0, 0, 1],
[1, 0, 2]])
您可以看到在第1行中有1个观测值,在第1列中有0个观测值,因此这些行是基本事实,而各列是预测。 因此,您可以使用confusion_matrix中显示的C[i, j]
表示法
非常感谢您澄清-维基百科参考使我感到困惑!
没问题,可能应该让Wikipedia将其示例切换为sklearn方向。
最有用的评论
我认为
y_test
应该在print(classification_report(KNN_prediction, y_test))
在第一位。因此:
print(classification_report(y_test, KNN_prediction))
。函数
sklearn.metrics.classification_report(y_true, y_pred, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn')
具有y_true
作为第一个参数。 这将翻转精度并召回。请参阅classification_report 。
编辑:您的混淆矩阵也向后,但是可以解决,因为sklearn的混淆矩阵是从Wikipedia向后的。
您可以看到在第1行中有1个观测值,在第1列中有0个观测值,因此这些行是基本事实,而各列是预测。 因此,您可以使用confusion_matrix中显示的
C[i, j]
表示法