zoukankan      html  css  js  c++  java
  • 手写一个机器学习的入门算法-感知器算法

    用4x+5y=2000作为分界线制造了100个点;
    初始分界线为0,0;
    经过1000轮纠正后,结果是:
    22 x+31 y = 11876
    对比结果4 x + 5 y = 2000
    还是比较接近的。
     
    刚开始更新w的那行代码搞错了,以为是用predict去纠正,其实应该用sample的真实值去纠正。
     
    import random;

    def find_split(points):
    w=(0,0,0)
    for _ in range(1,2000):
    print 'w='+str(w);
    for pt in points:
    (x1,x2,z) = pt;
    (w1,w2,w3)=w;
    predict = int((w1+w2*x1+w3*x2)>0)*2-1
    if predict!=z:
    print 'wrong: '+str(pt)
    w=(w1+z,w2+z*x1,w3+z*x2);
    # break;
    else:
    print 'right: '+str(pt)
    return w;

    def test_split(points,w):
    points_2 = filter(lambda pt:((int(w[0]+w[1]*pt[0]+w[2]*pt[1])>=0)*2-1)==pt[2],points)
    return points_2;

    def init_points(max_x,max_y,num_of_pts):
    points=[];
    for i in range(1,num_of_pts,1):
    x = int(random.random()*max_x);
    y = int(random.random()*max_y);
    z = int((4*x+5*y)>=2000)*2-1
    points.append((x,y,z));
    return points;


    if __name__ == '__main__':
    points = init_points(400,500,100);
    print points;
    line = find_split(points);
    print(line);
    pts = test_split(points,line);
    print points;
    print len(pts);
  • 相关阅读:
    51nod 1179 最大的最大公约数 (数论)
    POJ 3685 二分套二分
    POJ 3045 贪心
    LIC
    HDU 1029 Ignatius and the Princess IV
    HDU 1024 Max Sum Plus Plus
    HDU 2389 Rain on your Parade
    HDU 2819 Swap
    HDU 1281 棋盘游戏
    HDU 1083 Courses
  • 原文地址:https://www.cnblogs.com/alphablox/p/5793041.html
Copyright © 2011-2022 走看看