SVM分类-错误的输入形状错误

时间:2020-02-28 00:11:31

标签: svm

我尝试搜索时输入形状有误,但由于我在SVM中是新手,所以我还不明白。

train.csv
testing.csv

# importing required libraries
import numpy as np

# import support vector classifier
from sklearn.svm import SVC 
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)


X = pd.read_csv("train.csv")
y = pd.read_csv("testing.csv")

clf = SVC()
clf.fit(X, y)
clf.decision_function(X)
print(clf.predict(X))



raise ValueError("bad input shape {0}".format(shape))
ValueError: bad input shape (1, 6)

1 个答案:

答案 0 :(得分:0)

这里的问题是,您只是将带有训练数据的整个表(加上标签)作为训练数据的输入,然后尝试使用SVM预测测试数据的表(数据和标签)

这种方式不起作用。

您需要做的是用您的训练数据训练SVM(因此,数据点+每个数据点的标签),然后根据您的测试数据对其进行测试(测试数据点+标签)。

您的代码应改为:

# Load training and testing dataset from .csv files
training_dataset = pd.read_csv("train.csv")
testing_dataset = pd.read_csv("testing.csv")

# Load training data points with all relevant features
X_train = training_dataset[['feature1','feature2','feature3','feature4']]

# Load training labels from dataset
y_train = training_dataset['label']

# Load testing data points with all relevant features
X_test = testing_dataset[['feature1','feature2','feature3','feature4']]

# Load testing labels from dataset
y_test = testing_dataset['label']


clf = SVC()

# Train the SVC with the training data (data points and labels)
clf.fit(X_train, y_train)

# Evaluate the decision function with test samples
clf.decision_function(X_test)

# Predict the test samples
print(clf.predict(X_test))

我希望对您有所帮助,并且这段代码可以为您运行。让我知道我是否误解了什么,或者您还有其他问题。 :)