MOOC Machine Learning 作業交流帖5
阿新 • • 發佈:2019-01-01
wk5 Regularized Linear Regression and Bias v.s. Variance
最近正在學習MOOC上的經典課程:Machine learning (by Andrew Ng), 具體課程連結:MACHINE LEARNING
根據進度將作業的關鍵程式碼部分貼上,僅供交流與討論。
- linearRegCostFunction
h=X*theta;
J=sum((h-y).^2)/(2*m)+lambda*sum(theta(2:end).^2)/(2*m);
grad(1)=(h-y)'*X(:,1)/m;
grad(2:end)=X(:,2:end)' *(h-y)/m+lambda*theta(2:end)/m;
- learningCurve
for i =1:m
theta=trainLinearReg(X(1:i,:), y(1:i), lambda);
error_train(i)=linearRegCostFunction(X(1:i,:), y(1:i), theta, 0);
error_val(i)=linearRegCostFunction(Xval, yval, theta, 0);
end
- polyFeatures
for i =1:p
X_poly(:,i)=X.^i;
end
- validationCurve
for i= 1:length(lambda_vec)
lambda=lambda_vec(i);
theta=trainLinearReg(X, y, lambda);
error_train(i)=linearRegCostFunction(X, y, theta, 0);
error_val(i)=linearRegCostFunction(Xval, yval, theta, 0);
end
——轉載請註明出處