Hangyuan Li Teacher's "Statistical Learning Method" chapter II Algorithm of MATLAB program

Source: Internet
Author: User
Tags sprintf

Reference to the Http://blog.sina.com.cn/s/blog_bceeae150102v11v.html#post


% of the original form of the Perceptual machine learning algorithm, algorithm 2.1 reference Hangyuan Li the second chapter in the book "Statistical Learning Method" P29

Close all
Clear All
Clc
x=[3,3;4,3;1,1]; y=[1,1,-1];% training data sets and tags
learnrate=1;% Learning Rate
Omega=zeros (1,size (x,2))
Initial value of b=0%%ω and B
i=1;k=0;
While 1
If Y (i) * (SUM (omega.*x (i,:)) +b) <=0% The point is not properly classified, adjusted
Omega=omega+learnrate*y (i) *x (i,:);
B=b+learnrate*y (i);
i=1;% after adjusting the w,b, check each point again
k=k+1;% Record Iteration Count
fprintf (' Iteration count k=%d ', k);
omega=sprintf ('%2.0f\t ', Omega)
fprintf (' b=%d\n ', b);
Continue
Else
i=i+1;
End
If I>size (x,1)
Break
End
End
W=omega
B

% Perceptual machine learning algorithm dual form, algorithm 2.2 reference Hangyuan Li "Statistical Learning Method" book in the second chapter of the algorithm P33

Close all
Clear All
Clc
x=[3,3;4,3;1,1]; y=[1,1,-1];% training data sets and tags
learnrate=1;% Learning Rate (0,1)
Alpha=zeros (1,size (x,1))
B=0%%alpha and B's initial value size (X,dim) Dim=1 returns the number of rows (with several X points), dim=2 returns the number of columns
G=x*x '; Calculate gram matrix
i=1;k=0;
While 1
If Y (i) * (SUM (alpha.*y.*g (i,:)) +b) <=0% The point is not properly classified, adjusted
Alpha (i) =alpha (i) +1;
B=b+y (i);
i=1;% after adjusting the w,b, check each point again
k=k+1;% Record Iteration Count
fprintf (' Iteration count k=%d ', k);
alpha=sprintf ('%2.0f\t ', alpha)
fprintf (' b=%d\n ', b);
continue;% skips some statements in the loop body. When the statement is executed in the loop body, the program skips all the remaining statements in the loop body and proceeds to the next loop.
Else
i=i+1;
End
If I>size (x,1)
break;% terminates the execution of the loop, and when the break statement is executed in the loop body, the program jumps out of the loop body and proceeds to the next statement outside the loop body
End
End
W=alpha.*y*x% * is multiplication, that is, matrix multiplication,. * is a point multiplication, that is, the corresponding element multiplied
B

Hangyuan Li Teacher's "Statistical Learning Method" chapter II Algorithm of MATLAB program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.