首页 > 解决方案 > Scikit-Learn 的感知器训练准确率低于 100%,尽管数据是线性可分的

问题描述

我在数据集上使用sklearn's感知器wine,我希望看到 100% 的训练准确度,因为我知道这些类是线性可分的(如下所示)。代码:

from sklearn import datasets
from sklearn.linear_model import Perceptron
import numpy as np

# import data
wine = datasets.load_wine()
X = wine['data']
y = wine['target']
X_binary = X[y != 2].copy() # only keep 2 classes
y_binary = y[y != 2].copy() # only keep 2 classes

# fit, etc.
perceptron = Perceptron()
perceptron.fit(X_binary, y_binary);
np.all(perceptron.predict(X_binary) == y_binary) # False

有人知道为什么最后一行不是True吗?

表明数据是线性可分的:

beta = np.array(
      [-0.03733196, -0.00865585, -0.07756126,  0.00770558, -0.00001171,
        0.01579195, -0.01308789,  0.01526745,  0.00937851, -0.00191233,
        0.00859206, -0.0352664 , -0.00013141])

print(max((X_binary @ beta)[y_binary == 0]))
print(min((X_binary @ beta)[y_binary == 1]))
# max for the 0 case is less than the min for the 1 case

非常感谢!

标签: pythonmachine-learningscikit-learnperceptron

解决方案


推荐阅读