python中用等高线图绘制SVM决策边界的问题
我正在尝试建立一个支持向量机,我想用等高线图绘制决策边界。问题是python抱怨索引无效。 我事先知道,我应该使用以下几行来进行绘图:python中用等高线图绘制SVM决策边界的问题,python,matplotlib,svm,Python,Matplotlib,Svm,我正在尝试建立一个支持向量机,我想用等高线图绘制决策边界。问题是python抱怨索引无效。 我事先知道,我应该使用以下几行来进行绘图: xgrid=numpy.linspace(-5,5) ygrid=numpy.linspace(-4,4) grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid]) plt.contour(xgrid, ygrid, grid,
xgrid=numpy.linspace(-5,5)
ygrid=numpy.linspace(-4,4)
grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid])
plt.contour(xgrid, ygrid, grid,
(-1.0,0.0,1.0),
colors=('red','black','blue'),
linewidths=(1,3,1))
我相信问题出在我自己的指示符函数代码中(该函数用于分类新数据点):
但它无论如何都不起作用。
输入
是包含如下数据点的向量
array([[-6.63554270e-02, -6.37843596e-01],
[ 4.75689244e-02, -4.97290290e-01],
[-3.68237660e-01, -4.26781355e-01], ...
targets
是一个包含如下类的向量(仅包含负/正类)
_列表
是一个包含非零字母以及相应目标和数据点的列表
[array([10. , 3.70001649, 6.93179314, 0.63180963]),
array([-1., 1., 1., -1.]),
array([[-0.3227157 , -0.20585723],
[ 1.41237288, 0.27633635],
[-1.26207555, 0.16187663],
[-0.17244547, -0.25010605]])]
alphas是由库函数minimize
计算的,我已经被告知这些是正确的
最后,我得到的错误是:
grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid])
Traceback (most recent call last):
File "<ipython-input-101-4447fd0f8b62>", line 1, in <module>
grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid])
File "<ipython-input-101-4447fd0f8b62>", line 1, in <listcomp>
grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid])
File "<ipython-input-101-4447fd0f8b62>", line 1, in <listcomp>
grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid])
File "<ipython-input-97-b3eda9982efd>", line 7, in indicator
I_value=the_list[0][i]*the_list[1][i]*linear_kernel(s[i],the_list[2][i])
IndexError: invalid index to scalar variable.
但是我应该如何使用
指示器来解决这个问题呢?我已经试了几个小时了,所以任何帮助都是非常欢迎的 有人有什么想法吗?
array([-1., -1., -1., 1., -1., -1., ...
[array([10. , 3.70001649, 6.93179314, 0.63180963]),
array([-1., 1., 1., -1.]),
array([[-0.3227157 , -0.20585723],
[ 1.41237288, 0.27633635],
[-1.26207555, 0.16187663],
[-0.17244547, -0.25010605]])]
grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid])
Traceback (most recent call last):
File "<ipython-input-101-4447fd0f8b62>", line 1, in <module>
grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid])
File "<ipython-input-101-4447fd0f8b62>", line 1, in <listcomp>
grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid])
File "<ipython-input-101-4447fd0f8b62>", line 1, in <listcomp>
grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid])
File "<ipython-input-97-b3eda9982efd>", line 7, in indicator
I_value=the_list[0][i]*the_list[1][i]*linear_kernel(s[i],the_list[2][i])
IndexError: invalid index to scalar variable.
grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid])
Traceback (most recent call last):
File "<ipython-input-105-4447fd0f8b62>", line 1, in <module>
grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid])
File "<ipython-input-105-4447fd0f8b62>", line 1, in <listcomp>
grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid])
File "<ipython-input-105-4447fd0f8b62>", line 1, in <listcomp>
grid=numpy.array([[indicator(inputs,targets) for inputs in xgrid] for targets in ygrid])
File "<ipython-input-104-9e034c04c6fb>", line 7, in indicator
if I>0:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()