(在示例中包含您的相关代码始终是个好主意,而在图片中包含不)
分类报告指出测试样本中有零个“0”,这是不正确的。
这是因为,从链接图像中的代码可以看出,您已经切换了classification_report 中的参数;你用过:
print(classification_report(pred, ytest)) # wrong order of arguments
确实给出了:
precision recall f1-score support
class 0 0.00 0.00 0.00 0
class 1 1.00 0.63 0.77 171
avg / total 1.00 0.63 0.77 171
但正确的用法(见docs)是
print(classification_report(ytest, pred)) # ytest first
给了
precision recall f1-score support
class 0 0.00 0.00 0.00 63
class 1 0.63 1.00 0.77 108
avg / total 0.40 0.63 0.49 171
以及以下警告消息:
C:\Users\Root\Anaconda3\envs\tensorflow1\lib\site-packages\sklearn\metrics\classification.py:1135:
UndefinedMetricWarning:精度和 F 分数定义不明确,并且
在没有预测样本的标签中设置为 0.0。 '精确',
'predicted', 平均, warn_for)
因为正如 cmets 中已经指出的那样,您只能预测 1:
pred
# result:
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
原因是另一个故事,而不是当前问题的一部分。
以下是上述的完整可重现代码:
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(return_X_y=True)
xtrain,xtest,ytrain,ytest = train_test_split(X,y,test_size=0.3,random_state=42)
from sklearn.svm import SVC
svc=SVC()
svc.fit(xtrain, ytrain)
pred = svc.predict(xtest)
print(classification_report(ytest, pred))