【问题标题】:Using the linear_model perceptron module from sklearn to separate points使用 sklearn 中的 linear_model 感知器模块来分离点
【发布时间】:2021-12-13 13:52:15
【问题描述】:

我正在尝试将此 sklearn 模块用于二进制分类问题,并且我的数据显然是线性可分的。 我不明白为什么我的情节的绿色区域不包括五个红色圆圈。

.

我尝试将迭代次数参数(max_iter)从 100 更改为 10000,但没有任何区别。

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import Perceptron

def learn_and_display_Perceptron(datafile):
   #taking data reading this from the above functions
   data = np.loadtxt(datafile)
   n,d = data.shape
   x = data[:,0:2]
   y = data[:,2]

   clf = Perceptron(max_iter=10000)
   clf.fit(x, y)
   sv = np.zeros(n,dtype=bool)  ## all False array
   notsv = np.logical_not(sv)   # all True array
   
       # Determine the x1- and x2- limits of the plot
   x1min = min(x[:,0]) - 1
   x1max = max(x[:,0]) + 1
   x2min = min(x[:,1]) - 1
   x2max = max(x[:,1]) + 1
   plt.xlim(x1min,x1max)
   plt.ylim(x2min,x2max)
   # Plot the data points, enlarging those that are support vectors
   plt.plot(x[(y==1)*notsv,0], x[(y==1)*notsv,1], 'ro')
   plt.plot(x[(y==1)*sv,0], x[(y==1)*sv,1], 'ro', markersize=10)
   plt.plot(x[(y==-1)*notsv,0], x[(y==-1)*notsv,1], 'k^')
   plt.plot(x[(y==-1)*sv,0], x[(y==-1)*sv,1], 'k^', markersize=10)
   # Construct a grid of points and evaluate classifier at each grid points
   grid_spacing = 0.05
   xx1, xx2 = np.meshgrid(np.arange(x1min, x1max, grid_spacing), np.arange(x2min, x2max, grid_spacing))
   grid = np.c_[xx1.ravel(), xx2.ravel()]
   Z = clf.predict(grid)
   # Quantize the values to -1, -0.5, 0, 0.5, 1 for display purposes
   for i in range(len(Z)):
       Z[i] = min(Z[i],1.0)
       Z[i] = max(Z[i],-1.0)
       if (Z[i] > 0.0) and (Z[i] < 1.0):
           Z[i] = 0.5
       if (Z[i] < 0.0) and (Z[i] > -1.0):
           Z[i] = -0.5
   # Show boundary and margin using a color plot
   Z = Z.reshape(xx1.shape)
   plt.pcolormesh(xx1, xx2, Z, cmap=plt.cm.PRGn, vmin=-2, vmax=2, shading='auto')
   plt.show()

我的数据文件,data_1.txt 可以在这里找到,https://github.com/bluetail14/MyCourserapractice/tree/main/Edx

我可以在我的代码中进行哪些更改来调整绿色/紫色边界线以包含五个红色圆圈?

【问题讨论】:

    标签: python-3.x numpy matplotlib scikit-learn perceptron


    【解决方案1】:

    不错的代码。你需要改变eta0的值,

    clf = Perceptron(max_iter=10000, eta0=0.1)
    

    【讨论】:

      猜你喜欢
      • 2018-03-30
      • 1970-01-01
      • 2018-06-04
      • 2017-06-13
      • 2014-06-29
      • 2012-08-01
      • 2013-01-04
      • 1970-01-01
      • 2019-03-10
      相关资源
      最近更新 更多