簡體   English   中英

使用 sklearn 中的 linear_model 感知器模塊來分離點

[英]Using the linear_model perceptron module from sklearn to separate points

我正在嘗試將此 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

我可以在代碼中更改哪些內容來調整綠色/紫色邊界線以包含五個紅色圓圈?

不錯的代碼。 您需要更改 eta0 值,

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM