如何使用线性感知器中的权重向量绘制线条?

use*_*603 9 classification machine-learning perceptron

我理解以下内容:

在2D空间中,每个数据点都有2个特征:x和y.2D空间中的权重向量包含3个值[bias,w0,w1],可以将其重写为[w0,w1,w2].为了计算它与权重向量之间的点积,每个数据点需要一个人工坐标[1,x,y].

用于更新每个错误分类点的权重向量的学习规则是w:= w + yn*xn

我的问题是:你如何从权重向量w = [A,B,C]中得出两个点来绘制决策边界?

我理解A + Bx + Cy = 0是一般形式的线性方程(A,B,C可以从权重向量中获取)但我不知道如何绘制它.

提前致谢.

Jos*_*art 15

将权重插入一般形式(w0 + w1x + w2y = 0)并求解x,x = 0,y,y = 0:

x = -(w0 - w2y)/w1  
x = 0 when y = -w0/w2  
y = -(w0 - w1x)/w2  
y = 0 when x = -w0/w1  
Run Code Online (Sandbox Code Playgroud)

现在我们有两点在线:(0,-w0/w2)和(-w0/w1,0)

slope = -(w0/w2)/(w0/w1)  
intercept = -w0/w2
Run Code Online (Sandbox Code Playgroud)


Rub*_*ran 6

最近我正在尝试实现同样的东西,但太困惑如何绘制具有三个权重 $w_0,w_1,w_2$ 的决策边界图。基于上面提到的@Joshu 解决方案,我编写了 matplotlib 代码来绘制边界线。

def plot_data(self,inputs,targets,weights):
    # fig config
    plt.figure(figsize=(10,6))
    plt.grid(True)

    #plot input samples(2D data points) and i have two classes. 
    #one is +1 and second one is -1, so it red color for +1 and blue color for -1
    for input,target in zip(inputs,targets):
        plt.plot(input[0],input[1],'ro' if (target == 1.0) else 'bo')

    # Here i am calculating slope and intercept with given three weights
    for i in np.linspace(np.amin(inputs[:,:1]),np.amax(inputs[:,:1])):
        slope = -(weights[0]/weights[2])/(weights[0]/weights[1])  
        intercept = -weights[0]/weights[2]

        #y =mx+c, m is slope and c is intercept
        y = (slope*i) + intercept
        plt.plot(i, y,'ko')
Run Code Online (Sandbox Code Playgroud)

简单感知器分类两个不同的类


cur*_*ous 3

绘制直线的最佳方法是找到显示轴上的最小 x 值和最大 x 值。使用已知的直线方程 (-(A+BX)/C) 计算 y 值。这导致两点使用内置绘图命令画一条线。