Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sklearn-svm提供了一个糟糕的匹配_Python_Python 3.x_Machine Learning_Scikit Learn_Svm - Fatal编程技术网

Python sklearn-svm提供了一个糟糕的匹配

Python sklearn-svm提供了一个糟糕的匹配,python,python-3.x,machine-learning,scikit-learn,svm,Python,Python 3.x,Machine Learning,Scikit Learn,Svm,我试图将一个SVM绘制到我的样本数据中,但我遇到了一个问题:绘图似乎根本不正确,这很奇怪,因为我使用了来自的样本代码(更具体地说,是“发生了什么?”部分)。他们的代码对我来说很好,所以我假设问题与我的数据有关。我注意到拟合系数非常小,可以理解,这会破坏直线 这是可复制的代码 import matplotlib.pyplot as plt import numpy as np from sklearn import svm import matplotlib as mpl plt.figure(

我试图将一个SVM绘制到我的样本数据中,但我遇到了一个问题:绘图似乎根本不正确,这很奇怪,因为我使用了来自的样本代码(更具体地说,是“发生了什么?”部分)。他们的代码对我来说很好,所以我假设问题与我的数据有关。我注意到拟合系数非常小,可以理解,这会破坏直线

这是可复制的代码

import matplotlib.pyplot as plt
import numpy as np
from sklearn import svm
import matplotlib as mpl

plt.figure(figsize=(5,5))
in_cir = lambda x,y: True if x**2 + y**2 <= 4 else False # Checking if point is in the purple circle
f = lambda x,e: 1.16*x + 0.1 + e                         # True function
ran = np.arange(-5,6)       
lsp = np.linspace(-5,5,170)                              # X1 axis
np.random.seed(69)
dots = f(lsp,[np.random.normal(0,1.5) for i in lsp])     # X2 axis
blue_dots, pur_dots, lsp1, lsp2 = [], [], [], []
for i, x in zip(dots, lsp):
  if in_cir(x,i): pur_dots.append(i); lsp2.append(x)     # Getting all purple dots's X1 and X2
  else: blue_dots.append(i); lsp1.append(x)              # Same for blue ones
plt.scatter(lsp1, blue_dots, color='cornflowerblue')
plt.scatter(lsp2, pur_dots, color='magenta')
plt.xlabel('$X_1$', fontsize=15)
plt.ylabel('$X_2$', fontsize=15)


x, y = np.array(list(zip(lsp, dots))), np.where( np.array([in_cir(x,i) for x,i in zip(lsp,dots)]) == True, 'p','b' )
                                                        # On two lines above I made x a 2d array
                                                        # of coordinates for each dot
                                                        # And y is a list of 'b' if the corresponding
                                                        # dot is blue and 'p' otherwise

ft = svm.SVC(kernel='linear', C=1).fit(x, y)            # Fitting svc

                                                        # Here starts the code from the link
w = ft.coef_[0]
print('w', w)                                           # w components are really small
a = -w[0] / w[1]
xx = np.linspace(-5, 5)
yy = a * xx - (ft.intercept_[0]) / w[1]                 # This is where it all goes wrong

b = ft.support_vectors_[0]
yy_down = a * xx + (b[1] - a * b[0])
b = ft.support_vectors_[-1]
yy_up = a * xx + (b[1] - a * b[0])

plt.plot(xx, yy, 'k-')
plt.plot(xx, yy_down, 'k--')
plt.plot(xx, yy_up, 'k--')


plt.ylim(-5, 5.5)                                      # To make it interpretable
plt.xlim(-5, 4.5)                                      # the plot will be squished because of
plt.show()                                             # high values if removed
其结果是:


您试图使用线性分类器进行分离(即,您无法绘制分隔两组的直线)。您可以使用另一个内核,例如RBF:

import matplotlib.pyplot as plt
import numpy as np
from sklearn import svm
from mlxtend.plotting import plot_decision_regions

plt.figure(figsize=(5,5))
in_cir = lambda x,y: True if x**2 + y**2 <= 4 else False # Checking if point is in the purple circle
f = lambda x,e: 1.16*x + 0.1 + e                         # True function
ran = np.arange(-5,6)
lsp = np.linspace(-5,5,170)                              # X1 axis
np.random.seed(69)
dots = f(lsp,[np.random.normal(0,1.5) for i in lsp])     # X2 axis
blue_dots, pur_dots, lsp1, lsp2 = [], [], [], []
for i, x in zip(dots, lsp):
  if in_cir(x,i): pur_dots.append(i); lsp2.append(x)     # Getting all purple dots's X1 and X2
  else: blue_dots.append(i); lsp1.append(x)              # Same for blue ones

x, y = np.array(list(zip(lsp, dots))), np.where(np.array([in_cir(x,i) for x,i in zip(lsp,dots)]), 'p','b')

y[y == 'b'] = 0  # replacing letters with integers as the plot_decision_regions function accepts only integers
y[y == 'p'] = 1
y = y.astype(int)

ft = svm.SVC(kernel='rbf', C=1).fit(x, y)            # Fitting svc
plot_decision_regions(X=x,
                      y=y,
                      clf=ft,
                      legend=2)
plt.show()
导入matplotlib.pyplot作为plt
将numpy作为np导入
从sk学习输入svm
从mlxtend.plotting导入绘图\决策\区域
plt.图(figsize=(5,5))

in_cir=lambda x,y:True如果x**2+y**2谢谢,这是真的。然而,我还是设法做到了。如果有人感兴趣,我会更新帖子。
import matplotlib.pyplot as plt
import numpy as np
from sklearn import svm
from mlxtend.plotting import plot_decision_regions

plt.figure(figsize=(5,5))
in_cir = lambda x,y: True if x**2 + y**2 <= 4 else False # Checking if point is in the purple circle
f = lambda x,e: 1.16*x + 0.1 + e                         # True function
ran = np.arange(-5,6)
lsp = np.linspace(-5,5,170)                              # X1 axis
np.random.seed(69)
dots = f(lsp,[np.random.normal(0,1.5) for i in lsp])     # X2 axis
blue_dots, pur_dots, lsp1, lsp2 = [], [], [], []
for i, x in zip(dots, lsp):
  if in_cir(x,i): pur_dots.append(i); lsp2.append(x)     # Getting all purple dots's X1 and X2
  else: blue_dots.append(i); lsp1.append(x)              # Same for blue ones

x, y = np.array(list(zip(lsp, dots))), np.where(np.array([in_cir(x,i) for x,i in zip(lsp,dots)]), 'p','b')

y[y == 'b'] = 0  # replacing letters with integers as the plot_decision_regions function accepts only integers
y[y == 'p'] = 1
y = y.astype(int)

ft = svm.SVC(kernel='rbf', C=1).fit(x, y)            # Fitting svc
plot_decision_regions(X=x,
                      y=y,
                      clf=ft,
                      legend=2)
plt.show()