【发布时间】:2019-08-07 07:21:03
【问题描述】:
我是 Python 和机器学习的新手。我在网上找到了一个结合了 Arduino 和 Python 的项目,我决定试一试。该项目的github链接是herebtw。 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
我希望任何人都可以帮助它已经几周了,我想弄清楚这一点。谢谢。
【问题讨论】:
-
您在发布的数据中缺少
LABEL列(不是导致错误的原因) -
train.values 的形状为 (11,11),它必须是一维 numpy 数组才能成为标签
标签: python pandas machine-learning scikit-learn svm