大家好,我是Mac Jiang,今天和大家分享Coursera-NTU-機器學習基石(Machine Learning Foundations)-作业四 Q13-20的MATLAB实现。
曾经的代码都是通过C++实现的。可是发现C++实现这些代码太麻烦。这次作业还要频繁更改參数值,所以选择用MATLAB实现了。与C++相比。MATLAB实现显然轻松非常多。在数据导入方面也更加方便。我的代码尽管可以得到正确答案,可是当中可能有某些思想或者细节是错误的,假设各位博友发现,请及时留言纠正,谢谢。再次声明,博主提供实现代码的原因不是为了让各位通过測试,而是为学习有困难的同学提供一条解决思路。希望我的文章对您的学习有一些帮助!
本文出处:http://blog.csdn.net/a1015553840/article/details/51173020
其它问题解答请看汇总帖:http://blog.csdn.net/a1015553840/article/details/51085129
1.sign函数
function S = sign(x) %计算sign [m,n] = size(x); for i = 1:m, for j = 1:n; if x(i,j) <= 0, S = 1; else S = -1; end end end end
2.计算正则化线性回归函数LGwithRegularization
function Wreg = LGwithRegularization(X,y,lambda) [m,n] = size(X); Wreg = inv(X' * X + lambda * eye(n)) * X' * y;%正则化的线性回归求解 end
3.错误计算函数Error01 (注意。这里用的是0/1错误)
function E = Error01(X,y,Wreg) [m,n] = size(X); E = 1 - sum(sign(X * Wreg) == y) / m;%计算错误率 end
4.主进程
clc trainingData = load('trainingData.txt'); Xtrain = trainingData(:, [1, 2]); ytrain = trainingData(:, 3); testData = load('testData.txt'); Xtest = testData(:,[1,2]);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,:) = [];%出去用于求交叉验证的样本 % 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)%利用交叉验证的样本求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.第十三题
(1)题意:从两个站点下载训练样本和測试样本,利用正则化的线性回归,參数lambda取10。得到Ein 和Eout
(2)答案:Ein = 0.050 Eout = 0.045
14-15:第14-15题
(1)题意: 14.分别取lamda值为.....计算Ein和Eout。选取最小的Ein相应的正确答案,假设两个lambda相应的答案一样,选择大的lambda
15.选取最小Eout相应的正确答案
(2)答案:14.log = -8, Ein = 0.015,Eout = 0.02
15.log = -7,Ein = 0.03,Eout = 0.015
16.第十六题
(1)题意:利用前120个样本作为训练样本,后80个样本作为測试样本,分别计算不同lambda相应的Etrain,Eval,Eout。选择最小的Etrain相应的答案
(2)答案:log = -8,Etrain = 0, Eval = 0.05, Eout = 0.025
17.第十七题
(1)题意:和16题的做法一样。选择最小Eval相应的正确答案
(2)答案:log = 0, Etrain = 0.0333,Eval = 0.0375,Eout = 0.0280
18.第十八题
(1)题意:利用17题得到的最优lambda,利用全部样本作为训练样本。计算Ein,Eout
(2)答案:Ein = 0.035 Eout=0.02
19-20:第19-20题
(1)题意:19.把样本分为5份,利用交叉验证的方法计算Ecv,计算得到最小的Ecv
20.利用19得到的最小Ecv相应的lambda值,计算Ein,Eout
(2)答案:19. log=-8, Eval = 0.03
20.Ein = 0.015。Eout = 0.02
本文出处:http://blog.csdn.net/a1015553840/article/details/51173020
其它问题解答请看汇总帖:http://blog.csdn.net/a1015553840/article/details/51085129