Hello everyone, I am mac Jiang, today and everyone to share the coursera-ntu-machine learning Cornerstone (Machines learning foundations)-Job four q13-20 MATLAB implementation.
Once the code is implemented through C + +. However, it is too cumbersome to discover that C + + implements this code. This job also need to change the number of participants frequently, so choose to use MATLAB to achieve. Compared to C + +. MATLAB implementation is obviously much easier. It is also more convenient to import data. Although my code can get the correct answer, but there may be some ideas or details are wrong, suppose you bo friends found, please timely message correction, thank you. Again, bloggers provide the reason for implementing the code not to let you pass the test, but for students with difficulties in learning to provide a solution. Hope my article has some help for your study!
The source of this article: http://blog.csdn.net/a1015553840/article/details/51173020
For other questions, see the summary: http://blog.csdn.net/a1015553840/article/details/51085129
1.sign function
function S = sign (x) % calculation Sign[m,n] = size (x); For i = 1:m, for j = 1:n; If X (i,j) <= 0, S = 1; else S =-1; End Endendend
2. Calculation of regularization linear regression function lgwithregularization
function Wreg = lgwithregularization (X,Y,LAMBDA) [m,n] = size (X); WREG = INV (x ' * x + lambda * Eye (n)) * x ' * y;% regularization linear regression solution end
3. Error calculation function Error01 (note. 0/1 error is used here.)
function E = Error01 (X,y,wreg) [m,n] = size (X); E = 1-sum (sign (X * wreg) = = y)/m;% calculation error rate end
4. Main process
Clctrainingdata = Load (' trainingData.txt '); Xtrain = Trainingdata (:, [1, 2]); Ytrain = Trainingdata (:, 3); testData = Load (' testData.txt '); Xtest = TestData (:, [+]); ytest = TestData (:, 3); [M,n] = size (Xtrain); Xtrain = [Ones (m,1), Xtrain]; [A, b] = size (Xtest); Xtest = [Ones (a,1), Xtest];%13-15%lambda = 10^-3;%wreg = Lgwithregularization (XTRAIN,YTRAIN,LAMBDA);%ein = Error01 ( Xtrain,ytrain,wreg)%eout = Error01 (xtest,ytest,wreg)%16-17%lambda = 10^-3;%wreg = Lgwithregularization (Xtrain (1:120, :), Ytrain (1:120,:), lambda);%etrain = Error01 (Xtrain (1:120,:), Ytrain (1:120,:), wreg)%eval = Error01 (Xtrain (121:200, :), Ytrain (121:200,:), wreg)%eout = Error01 (xtest,ytest,wreg)%18%lambda = 10^0;%wreg = Lgwithregularization (Xtrain, YTRAIN,LAMBDA);%ein = Error01 (xtrain,ytrain,wreg)%eout = Error01 (xtest,ytest,wreg)%19%lambda = 10^-6%ecv = 0;%v = 5;%per = M/v;%for i = 1:v,% xtemp = xtrain;% ytemp = ytrain;% xtemp (1+ (i-1) *per:i*per,:) = [];% out for cross-validation samples% ytemp ( 1+ (i-1) *per:i*per,:) = [];% wreg = LgwithrEgularization (XTEMP,YTEMP,LAMBDA);% Error01 (Xtrain (1+ (i-1) *per:i*per,:), Ytrain (1+ (i-1) *per:i*per,:), wreg)% Use cross-validated samples for ecv% ECV = ECV + Error01 (Xtrain (1+ (i-1) *per:i*per,:), Ytrain (1+ (i-1) *per:i*per,:), wreg);%end%ecv = ecv/v%20% Lambda = 10^-8;%wreg = Lgwithregularization (XTRAIN,YTRAIN,LAMBDA);%ein = Error01 (xtrain,ytrain,wreg)%eout = Error01 ( Xtest,ytest,wreg)
13. Question 13th
(1) Test instructions: Download the training samples and test samples from two sites, use the regularization linear regression, take the lambda 10. Get Ein and Eout
(2) Answer: Ein = 0.050 eout = 0.045
14-15: The question of 第14-15
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">
(1) Test instructions: 14. Take LAMDA value respectively. Calculate Ein and Eout. Select the appropriate correct answer for the smallest ein, assuming that the two lambda corresponding answer is the same, choose a large lambda
15. Select the correct answer for the minimum eout
(2) Answer: 14.log =-8, Ein = 0.015,eout = 0.02
15.log = -7,ein = 0.03,eout = 0.015
16. Question 16th
(1) Test instructions: Using the first 120 samples as training samples, and the last 80 samples as test samples, the corresponding etrain,eval,eout of different lambda were calculated respectively. Choose the smallest etrain corresponding answer
(2) Answer: Log = -8,etrain = 0, Eval = 0.05, eout = 0.025
17. Question 17th
(1) Test instructions: The same as the practice of 16 questions. Select the appropriate correct answer for minimum eval
(2) Answer: Log = 0, Etrain = 0.0333,eval = 0.0375,eout = 0.0280
18. Question 18th
(1) Test instructions: Using the optimal lambda obtained from 17, the whole sample is used as the training sample. Calculate Ein,eout
(2) Answer: Ein = 0.035 eout=0.02
19-20: The question of 第19-20
(1) Test instructions: 19. Divide the sample into 5 parts, calculate the ECV by the method of cross-validation, calculate the minimum ecv
20. Calculate ein,eout with the corresponding lambda value for the minimum ecv obtained by 19
(2) Answer: 19. Log=-8, Eval = 0.03
20.Ein = 0.015. Eout = 0.02
The source of this article: http://blog.csdn.net/a1015553840/article/details/51173020
For other questions, see the summary: http://blog.csdn.net/a1015553840/article/details/51085129
Robot Learning Cornerstone (Machine learning foundations) Learn Cornerstone Job four q13-20 MATLAB implementation