MachineLearning Exercise 5 :Regularized Linear Regression and Bias vs Variance

之前本打算把ML每一堂课做个笔记来着,现在coursera内容太详细了,再接着截图做笔记感觉太逗逼了,就把课后编程练习做下笔记吧。。。

linearRegCostFunction

pre = X*theta-y;
J = (1/(2*m))*(pre'*pre+lambda*(theta(2:end)'*theta(2:end)));

grad = (1/m)*(pre'*X)';
temp = theta;
temp(1) = 0;
grad = grad + (lambda/m)*(temp);

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

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

polyFeatures

for i =1:p
    X_poly(:,i) = X.^i;
end

其它练习会抽空补上,至于解释,且听下回分解~~

你可能感兴趣的:(mac)