python 中使用 SVM 进行机器学习的分类报告测试集错误
Error in classification report test set for machine learning with SVM in python
我将数据分成测试集和训练集,这两个集的目标值都是“0”和“1”。但是在使用 SVM 进行拟合和预测后,分类报告指出测试样本中存在零个“0”,这是不正确的。
from sklearn.datasets import load_breast_cancer
data = load_breast_cancer()
df = pd.DataFrame(data = data['data'],columns=data['feature_names'])
x = df
y = data['target']
xtrain,xtest,ytrain,ytest
= train_test_split(x,y,test_size=0.3,random_state=42)
如下所示,测试有 0 和 1,但分类报告中的支持表明没有任何 0!
(始终在示例中包含您的相关代码是个好主意,不要在图像中)
the classification report states that there are Zero '0's in the test sample which is not true.
这是因为,从链接图像中的代码来看,您已经切换了 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: Precision and F-score are ill-defined and
being set to 0.0 in labels with no predicted samples. 'precision',
'predicted', average, warn_for)
因为,正如评论中已经指出的那样,您只预测 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))
我将数据分成测试集和训练集,这两个集的目标值都是“0”和“1”。但是在使用 SVM 进行拟合和预测后,分类报告指出测试样本中存在零个“0”,这是不正确的。
from sklearn.datasets import load_breast_cancer
data = load_breast_cancer()
df = pd.DataFrame(data = data['data'],columns=data['feature_names'])
x = df
y = data['target']
xtrain,xtest,ytrain,ytest
= train_test_split(x,y,test_size=0.3,random_state=42)
如下所示,测试有 0 和 1,但分类报告中的支持表明没有任何 0!
(始终在示例中包含您的相关代码是个好主意,不要在图像中)
the classification report states that there are Zero '0's in the test sample which is not true.
这是因为,从链接图像中的代码来看,您已经切换了 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: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. 'precision', 'predicted', average, warn_for)
因为,正如评论中已经指出的那样,您只预测 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))