具有多种特征的 SVM

数据挖掘 Python 分类 scikit-学习 支持向量机
2022-02-14 14:55:07
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn import svm


X=[[1,0,0,0,0],
           [0,1,0,0,0],
           [0,0,1,0,0],
           [1,0,0,1,0],
           [1,0,0,0,1]] 

y=[0,1,1,1,0]

model=svm.SVC()
model.fit(X,y)
print(model.predict([1,0,1,0,0]))

我正在研究这个,但我收到一个错误

"if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=[1. 0. 1. 0. 0.].
Reshape your data either using array.reshape(-1, 1) if your data has a single fe
ature or array.reshape(1, -1) if it contains a single sample.

我是新手,你们能帮帮我吗?

2个回答

表演完之后

X=np.array([[1,0,0,0,0],
           [0,1,0,0,0],
           [0,0,1,0,0],
           [1,0,0,1,0],
           [1,0,0,0,1]])

y=np.array([0,1,1,1,0])

您必须执行以下操作:

y = y.reshape(1, -1)

model=svm.SVC()
model.fit(X,y)
test = np.array([1,0,1,0,0])
test = test.reshape(1,-1)
print(model.predict(test))

将来,您必须扩展数据集。您可以使用 Standard Scaler(建议)或 MinMax Scaler。

您的输入是列表,但它们必须是数组。

X=np.array([[1,0,0,0,0],
           [0,1,0,0,0],
           [0,0,1,0,0],
           [1,0,0,1,0],
           [1,0,0,0,1]])

y=np.array([0,1,1,1,0])