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

问题描述 投票:8回答:3

我理解以下内容:

在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可以从权重向量中获取)但我不知道如何绘制它。

提前致谢。

machine-learning classification linear perceptron
3个回答
13
投票

将权重插入一般形式(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  

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

slope = -(w0/w2)/(w0/w1)  
intercept = -w0/w2

3
投票

最近我试图实现同样的事情,但是太混淆了如何用三个权重$ 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')

simple perceptron classifies two different class


2
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.