在 Python 中执行 SVM 时的值错误

Value Error when conducting SVM in Python

我正在尝试 运行 使用生成的数据集的 SVM 线性内核。我的数据集有 5000 行和 4 列:

CL_scaled.head()[screenshot of data frame][1]

我将数据分成 20% 的测试和 80% 的训练:

train, test = train_test_split(CL_scaled, test_size=0.2)

并为训练获得 (4000,4) 的形状,为测试获得 (1000,4) 的形状

但是,当我运行训练和测试数据上的svm时,我得到以下错误:

svclassifier = SVC(kernel='linear', C = 5)
svclassifier.fit(train, test)
ValueError                                Traceback (most recent call last)
<ipython-input-81-4c4a7bdcbe85> in <module>

----> 1 svclassifier.fit(train, test)

~/anaconda3/lib/python3.7/site-packages/sklearn/svm/base.py in fit(self, X, y, sample_weight)
    144         X, y = check_X_y(X, y, dtype=np.float64,
    145                          order='C', accept_sparse='csr',
--> 146                          accept_large_sparse=False)
    147         y = self._validate_targets(y)
    148 

~/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    722                         dtype=None)
    723     else:
--> 724         y = column_or_1d(y, warn=True)
    725         _assert_all_finite(y)
    726     if y_numeric and y.dtype.kind == 'O':

~/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in column_or_1d(y, warn)
    758         return np.ravel(y)
    759 
--> 760     raise ValueError("bad input shape {0}".format(shape))
    761 
    762 

ValueError: bad input shape (1000, 4)

有人可以告诉我我的代码或数据有什么问题吗?提前致谢!

 train.head()
                 0         1             2            3 
    2004    1.619999    1.049560    1.470708    -1.323666
    1583    1.389370    -0.788002   -0.320337   -0.898712
    1898    -1.436903   0.994719    0.326256    0.495565
     892    1.419123    1.522091    1.378514    -1.731400
     4619   0.063095    1.527875    -1.285816   -0.823347

test.head()
            0           1           2         3
1118    -1.152435   -0.484851   -0.996602   1.617749
4347    -0.519430   -0.479388   1.483582    -0.413985
2220    -0.966766   -1.459475   -0.827581   0.849729
 204    1.759567    -0.113363   -1.618555   -1.383653
3578    0.329069    1.151323    -0.652328   1.666561


print(test.shape)
print(train.shape)
(1000, 4)
(4000, 4)

错误是因为 train, test = train_test_split(CL_scaled, test_size=0.2)

首先你需要分离数据和输出变量并将其传递给 train_test_split

# I am assuming your last column is output variable
train_test_split(CL_scaled[:-1], CL_scaled[-1], test_size=0.2).

并且 train_test_split 将您的数据分成 4 部分 X_train, X_test, y_train, y_test

此外,svclassifier.fit取参数自变量和输出变量。所以你需要传递 X_trainy_train

所以你的代码应该是

X_train, X_test, y_train, y_test = train_test_split(CL_scaled[:-1], CL_scaled[-1], test_size=0.2)

svclassifier = SVC(kernel='linear', C = 5)

svclassifier.fit(X_train, y_train)

有关详细信息,请参阅 documentation

您缺少监督机器学习的基本概念。

在 class化问题中,你有特征 X 并且你想用它们预测 class Y.例如,这可能看起来像这样:

X                    y
Height Weight        class
170    50            1
180    60            1
10     10            0

算法的想法是它们有一个训练部分(你去足球训练训练)和一个测试部分(你周末在球场上测试你的技能)。

因此您需要将数据拆分为训练集和测试集。

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(CL_scaled[:-1], CL_scaled[-1], test_size=0.2)

CL_scaled[:-1]是你的X,CL_scalded[-1]是你的Y。

那么你正在使用它来适应你的 classifier(训练部分):

svclassifier = SVC(kernel='linear', C = 5)
svclassifier.fit(X_train, y_train)

然后就可以测试了:

prediction = svcclassifier.predict(X_test, y_test)

这将 return 您对测试部分 (y_predict) 的预测,您可以根据您的 y_test.

来衡量它