《机器学习基石》第2讲 学习笔记

来源:互联网 发布:北京亚信智慧数据科技 编辑:程序博客网 时间:2024/06/10 20:29

PLA算法 Matlab实现代码,如有不对还请多多指点。

x_1=[120 185 215 275 310 337];
x_2=[110 125 185 250 130 137];
plot(x_1,x_2,'ob','linewidth',3,'markersize',15); 
hold on;

x1=[55 98 115 110 95 122 70 205 225 ];
y1=[90 178 170 225 270 270 310 345 290 ];
plot(x1,y1,'xr','linewidth',3,'markersize',15)
hold on;

negpoints = [55,90,-1;310,130,1;98,178,-1;115,110,1;115,165,-1;185,125,1;110,225,-1;215,185,1;95,270,-1;275,260,1;122,270,-1;70,310,-1;337,137,1;205,345,-1;225,280,-1]
pospoints = [310,130,-1;115,110,-1;185,125,-1;215,185,-1;275,260,-1;337,137,-1]

weight = [0,200,200]
H_value = 0
sig=true
axis([50 350 50 350])
while sig
    for i=1:1:15
        sig=false
        q = sign(negpoints(i,3))
        h_x_i = sign(weight(1)+weight(2)*negpoints(i,1)+weight(3)*negpoints(i,2))
        if h_x_i == q
            if (i==15 && sig==false )            
               
                x =[50,100,200,250,350]
                y = -(weight(2)/weight(3))*x -( weight(1)/weight(3))
                plot(x,y,'b');           
                hold on;
            else
                continue
            end
        else  
            sig=true
            ew1 = weight(2)
            ew2 = weight(3)
            weight(1)= (weight(1)+ q*1)
            weight(2)= (weight(2)+ q*negpoints(i,1))
            weight(3)= (weight(3)+ q*negpoints(i,2))
           
            x =[50,100,200,250,350]
            x1 =[50,100,200,250,350]
            y1 = (weight(3)/weight(2))*(x1-200) +200
            plot(x1,y1,'b');           
            hold on;
            y = -(weight(2)/weight(3))*x -( weight(1)/weight(3))
            plot(x,y,'r');           
            hold on;
        end
    end  
end

0 0
原创粉丝点击