感知器的决策边界图


11

我试图绘制感知器算法的决策边界,我对一些事情感到非常困惑。我的输入实例的格式为,基本上是2D输入实例(x 1x 2)和二进制类目标值(y)[1或0]。[(x1,x2),y]x1x2y

因此,我的权重向量的形式为:[w1,w2]

现在我必须合并一个额外的偏置参数,因此我的权重向量变成3 × 1向量?是1 × 3向量吗?我认为应该是1 × 3,因为向量只有1行n列。w03×11×31×3

现在假设我将实例化为随机值,该如何绘制决策边界?含义w 0在这里表示什么?是瓦特0 / Ñ ø ř 瓦特的判定区域的离原点的距离是多少?如果是这样,我如何捕获它并使用matplotlib.pyplot或其等效的Matlab在Python中绘制它?[w0,w1,w2]w0w0/norm(w)

对于这个问题,我什至会提供一点帮助。

Answers:


16

感知器在每次迭代中预测输出的方式是通过遵循以下方程式:

yj=f[wTx]=f[wx]=f[w0+w1x1+w2x2+...+wnxn]

ww01

n×11×nn×1

请记住,这是针对训练集中的每个输入完成的。此后,更新权重向量以校正预测输出和实际输出之间的误差。

至于决策边界,这是我在这里找到的scikit学习代码的修改:

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

X = np.array([[2,1],[3,4],[4,2],[3,1]])
Y = np.array([0,0,1,1])
h = .02  # step size in the mesh


# we create an instance of SVM and fit our data. We do not scale our
# data since we want to plot the support vectors

clf = Perceptron(n_iter=100).fit(X, Y)

# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))

# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, m_max]x[y_min, y_max].
fig, ax = plt.subplots()
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot
Z = Z.reshape(xx.shape)
ax.contourf(xx, yy, Z, cmap=plt.cm.Paired)
ax.axis('off')

# Plot also the training points
ax.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)

ax.set_title('Perceptron')

产生以下图:

在此处输入图片说明

基本上,该想法是为覆盖每个点的网格中的每个点预测一个值,然后使用绘制具有适当颜色的每个预测contourf


0

w0,w1,w2

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')

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.