python - 如何解决 ValueError : bad input shape (11, 11)?

标签 python pandas machine-learning scikit-learn svm

我是 Python 和机器学习新手。我在网上发现了一个包含 Arduino 和 Python 的项目,我决定尝试一下。该项目的github链接为here顺便提一句。 Arduino 部分已经可以运行,我已经可以将数据写入 csv 文件。我正在尝试将 csv 文件中的数据插入到 svm 中的训练中。但是,我无法通过训练部分。代码如下。

import numpy as np
import csv
from sklearn import svm
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
import pandas as pd
dataframe= pd.read_csv("csvdata.csv", delimiter=',')

from sklearn.model_selection import train_test_split
train, test = train_test_split(df, test_size = 0.2)

train_features = train[['LABEL','F1','F2','F3','F4','F5','X','Y','Z','C1','C2']]
train_label = train.values

test_features = test[['LABEL','F1','F2','F3','F4','F5','X','Y','Z','C1','C2']]
test_label = test.values

## SVM
model = svm.SVC(kernel='linear', gamma=1, C=1)
model.fit(train_features, train_label)

每当我运行代码时,我都会在最后一行偶然发现此错误:

Traceback (most recent call last):
  File "C:\Python27\projects\practice\modeling.py", line 32, in <module>
    model.fit(train_features, train_label)
  File "C:\Python27\lib\site-packages\sklearn\svm\base.py", line 149, in fit
    accept_large_sparse=False)
  File "C:\Python27\lib\site-packages\sklearn\utils\validation.py", line 761, in check_X_y
    y = column_or_1d(y, warn=True)
  File "C:\Python27\lib\site-packages\sklearn\utils\validation.py", line 797, in column_or_1d
    raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape (11, 11)

我真的很困惑,不知道该怎么办。我不知道标签和功能以及通过 csv 进行的训练是如何工作的。我研究过通过在 Python 脚本中手动插入数据进行训练,它们可以工作,但我在 csv 训练中没有找到解决方案。

这是 csv 文件的内容

F1,F2,F3,F4,F5,X,Y,Z,C1,C2

525, 505, 544, 557, 545, 1268, -8264, 14888, 1, 1

523, 505, 544, 557, 545, 1480, -8320, 14684, 1, 1

517, 505, 544, 557, 544, 1460, -8352, 14712, 1, 1

524, 505, 544, 557, 545, 1436, -8312, 14496, 1, 1

568, 506, 544, 557, 544, 1308, -8348, 14744, 1, 1

578, 506, 544, 557, 544, 1128, -8484, 14376, 1, 1

583, 506, 544, 557, 545, 1376, -8180, 14768, 1, 1

583, 505, 544, 557, 545, 1380, -8220, 14636, 1, 1

550, 505, 544, 557, 544, 1332, -8376, 14700, 1, 1

510, 505, 544, 557, 545, 1412, -8320, 14620, 1, 1

510, 505, 544, 557, 545, 1412, -8320, 14620, 1, 1

510, 505, 544, 557, 545, 1412, -8320, 14620, 1, 1

我希望有人能帮忙,我已经好几个星期想弄清楚这个问题了。谢谢。

最佳答案

在您的model.fit中,您应该传递特征和标签;但按照原样(train_label = train.values),您最终得到的特征和标签本质上是相同的。您的标签应该是一维的(您可以在错误消息中看到y = column_or_1d),并且当然不是您的功能的一部分。

您应该按如下方式更改功能和标签定义:

train_features = train[['F1','F2','F3','F4','F5','X','Y','Z','C1','C2']]
train_label = train['LABEL']

test_features = test[['F1','F2','F3','F4','F5','X','Y','Z','C1','C2']]
test_label = test['LABEL']

如果您的 model.fit 无法按原样工作,请尝试:

model.fit(train_features.values, train_label.values)

关于python - 如何解决 ValueError : bad input shape (11, 11)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55195697/

相关文章:

python - 无法使用 google plus API 进行授权

python - pandas loc 函数在写入和读取时引用不同的行 -> ValueError

python - 特定格式的数据框枢轴

python - Pandas str.contains 超过一个列表?

python - 如何为特定列的每个不同值选择一行并合并以在 Python 中形成一个新的数据框?

matlab - G和GHAT需要是相同的分类树

r - R 中奇怪的 svm 行为 (e1071)

python - 如何修复[Errno 111]连接被拒绝

python - Wine 质量数据集的训练准确率无法超过 55%

python - 在 CentOS 与 python 2.6 上针对 information_schema 执行 "SELECT"语句时,psycopg2 挂起