看了台湾大学林轩田的机器学习第二章感知器算法,做一些笔记备忘。理论部分以后再补,直接上代码:
%感知器算法实例 % creat the value of w x0 = ones(20, 1); %creat datasets,(x_1,x_2) x1 = rand(20, 2)*10; x = [x0, x1]; test = rand(20, 1); y = ones(20, 1); for i=1:20 if x1(i, 1)>6 y(i) = -1; end end j=1; for i=1:20 if y(i)==-1 u(j) = x1(i, 1); v(j) = x1(i, 2); j = j+1; end end scatter(u, v, ‘or‘); hold on; j=1; u=[]; v=[]; for i=1:20 if y(i)==1 u(j) = x1(i, 1); v(j) = x1(i, 2); j = j+1; end end scatter(u, v, ‘xk‘); hold on; %Example of PLA algorithm w = [0, 0, 0]; while true pd = false; for i=1:20 t = x(i, :); if w*t‘*y(i)<=0 w = w + y(i)*t; pd = true; break; end end if pd == false break; end end w v = linspace(0, 10, 100); u = -w(3)/w(2)*v - w(1)/w(2); plot(u, v, ‘.‘); hold on %Next is the code of PLA algorithm by Nerual Network Toolbox t = 1; y(y==-1)=0; net = newp([0, 10; 0, 10], t); net = train(net, x1‘, y‘); newt = sim(net, x1‘); iw = net.iw; b = net.b; ww = [b{1}, iw{1}]; vv = linspace(0, 10, 100); uu = -ww(3)/ww(2)*v - ww(1)/ww(2); plot(uu, vv, ‘.k‘);
得到的结果如下:
黄色部分是按照理论写的代码,而黑色是根据神经网络工具箱跑出来的结果。