感知器问题

2024-09-30 22:15:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用一条线(感知器)f,在另一边使点+1和-1,从而得到一组训练数据点。然后画一条新的线g,通过更新w=w+y(t)x(t),使其尽可能接近f,其中w是权重,y(t)是+1,-1,x(t)是一个错误分类点的坐标。在实现这一点之后,我并没有得到一个很好的适合从g到f。这是我的代码和一些示例输出。在

import random

random.seed()

points = [ [1, random.randint(-25, 25), random.randint(-25,25), 0] for k in range(1000)]

weights = [.1,.1,.1]

misclassified = []

############################################################# Function f

interceptf = (0,random.randint(-5,5))

slopef = (random.randint(-10, 10),random.randint(-10,10))

point1f = ((interceptf[0] + slopef[0]),(interceptf[1] + slopef[1]))
point2f = ((interceptf[0] - slopef[0]),(interceptf[1] - slopef[1]))

############################################################# Function G starting
interceptg = (-weights[0],weights[2])

slopeg = (-weights[1],weights[2])

point1g = ((interceptg[0] + slopeg[0]),(interceptg[1] + slopeg[1]))
point2g = ((interceptg[0] - slopeg[0]),(interceptg[1] - slopeg[1]))
#############################################################

def isLeft(a, b, c):
      return ((b[0] - a[0])*(c[1] - a[1]) - (b[1] - a[1])*(c[0] - a[0])) > 0


for i in points:
    if isLeft(point1f,point2f,i):
        i[3]=1
    else:
        i[3]=-1

for i in points:
    if (isLeft(point1g,point2g,i)) and (i[3] == -1):
        misclassified.append(i)

    if (not isLeft(point1g,point2g,i)) and (i[3] == 1):
        misclassified.append(i)

print len(misclassified)


while misclassified:
    first = misclassified[0]
    misclassified.pop(0)

    a = [first[0],first[1],first[2]]
    b = first[3]

    a[:] = [x*b for x in a]

    weights = [(x + y) for x, y in zip(weights,a)]

interceptg = (-weights[0],weights[2])

slopeg = (-weights[1],weights[2])

point1g = ((interceptg[0] + slopeg[0]),(interceptg[1] + slopeg[1]))
point2g = ((interceptg[0] - slopeg[0]),(interceptg[1] - slopeg[1]))

check = 0

for i in points:
    if (isLeft(point1g,point2g,i)) and (i[3] == -1):
        check += 1

    if (not isLeft(point1g,point2g,i)) and (i[3] == 1):
        check += 1

print weights
print check

117<;——原始误分类的g

[-116.9,-300.9,190.1]<;---最终重量

617<;——g-after算法的原始误分类数

956<;---原始错误分类的g

[-33.9,-12769.9,-572.9]<;---最终重量

461<;——g after算法的原始误分类数


Tags: inltfor分类randomrandintweightsmisclassified
1条回答
网友
1楼 · 发布于 2024-09-30 22:15:28

你的算法至少有几个问题:

  • 你的“while”条件是错误的-感知器学习并不是像现在这样迭代一次遍历所有错误分类的点。算法应该迭代所有的点,只要其中任何一点分类错误。尤其是-每次更新都会使一些正确分类的点成为错误的点,因此您必须始终迭代所有这些点并检查是否一切正常。

  • 我很确定您真正想要的是以(y(i)-p(i))x(i)的形式更新规则,其中p(i)是预测标签,y(i)是一个真正的标签(但是如果您只更新错误的分类,这显然会使您的方法退化)

相关问题 更多 >