支持向量机决策边界绘制错误

2024-09-28 23:05:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图模仿我在Kaggle上找到的关于绘制SVM决策边界的代码。我使用的是我自己的数据集,有608个数据和10个特征,有2个类。举例来说,这两门课就是你是否是糖尿病患者。我复制了这个链接上的SVM部分的代码(当你在底部向下滚动它时,你可以在其中找到),其中提到了决策边界可视化Here's the link to my reference

但是,我得到了这样一个错误:“X必须是Numpy数组”。有人能给我解释一下这是什么意思吗

下面的代码就是我所做的。请注意,我的数据集已经预先标准化了。另外,我把数据分成70:30的比例

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.pyplot as show
import matplotlib as cm
import matplotlib.colors as colors
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn import svm
from mlxtend.plotting import plot_decision_regions


autism = pd.read_csv('diabetec.csv')

x = autism.drop(['TARGET'], axis = 1)  
y = autism['TARGET']
x_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.30, random_state=1)

t = np.array(y_train)
t = t.astype(np.integer)
clf_svm = SVC(C=1.3, gamma=0.8, kernel='rbf')
clf_svm.fit(x_train, t)
plt.figure(figsize=[15,10])
plot_decision_regions(x_train, t, clf = clf_svm, hide_spines = False, colors = 'purple,limegreen', markers = ['x','o'])
plt.title('Support Vector Machine')


Tags: 数据代码fromtestimportmatplotlibasnp