Andrew Ng机器学习week6(Regularized Linear Regression and Bias/Variance)编程习题

本文主要是介绍Andrew Ng机器学习week6(Regularized Linear Regression and Bias/Variance)编程习题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Andrew Ng机器学习week6(Regularized Linear Regression and Bias/Variance)编程习题

linearRegCostFunction.m

function [J, grad] = linearRegCostFunction(X, y, theta, lambda)
%LINEARREGCOSTFUNCTION Compute cost and gradient for regularized linear 
%regression with multiple variables
%   [J, grad] = LINEARREGCOSTFUNCTION(X, y, theta, lambda) computes the 
%   cost of using theta as the parameter for linear regression to fit the 
%   data points in X and y. Returns the cost in J and the gradient in grad% Initialize some useful values
m = length(y); % number of training examples% You need to return the following variables correctly 
J = 0;
grad = zeros(size(theta));% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost and gradient of regularized linear 
%               regression for a particular choice of theta.
%
%               You should set J to the cost and grad to the gradient.
%predictions = X * theta;
sqrErrors = (predictions - y) .^ 2;
theta_r = [0;theta(2:end)];
J = 1 / (2 * m) * sum(sqrErrors) + lambda / (2 * m) * sum(theta_r .^ 2);grad = X' * (predictions - y) / m + theta_r * lambda / m;% =========================================================================grad = grad(:);end

learningCurve.m

function [error_train, error_val] = ...learningCurve(X, y, Xval, yval, lambda)
%LEARNINGCURVE Generates the train and cross validation set errors needed 
%to plot a learning curve
%   [error_train, error_val] = ...
%       LEARNINGCURVE(X, y, Xval, yval, lambda) returns the train and
%       cross validation set errors for a learning curve. In particular, 
%       it returns two vectors of the same length - error_train and 
%       error_val. Then, error_train(i) contains the training error for
%       i examples (and similarly for error_val(i)).
%
%   In this function, you will compute the train and test errors for
%   dataset sizes from 1 up to m. In practice, when working with larger
%   datasets, you might want to do this in larger intervals.
%% Number of training examples
m = size(X, 1);% You need to return these values correctly
error_train = zeros(m, 1);
error_val   = zeros(m, 1);% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return training errors in 
%               error_train and the cross validation errors in error_val. 
%               i.e., error_train(i) and 
%               error_val(i) should give you the errors
%               obtained after training on i examples.
%
% Note: You should evaluate the training error on the first i training
%       examples (i.e., X(1:i, :) and y(1:i)).
%
%       For the cross-validation error, you should instead evaluate on
%       the _entire_ cross validation set (Xval and yval).
%
% Note: If you are using your cost function (linearRegCostFunction)
%       to compute the training and cross validation error, you should 
%       call the function with the lambda argument set to 0. 
%       Do note that you will still need to use lambda when running
%       the training to obtain the theta parameters.
%
% Hint: You can loop over the examples with the following:
%
%       for i = 1:m
%           % Compute train/cross validation errors using training examples 
%           % X(1:i, :) and y(1:i), storing the result in 
%           % error_train(i) and error_val(i)
%           ....
%           
%       end
%% ---------------------- Sample Solution ----------------------
for i = 1:mtheta = trainLinearReg([ones(i,1), X(1:i,:)], y(1:i), lambda);error_train(i) = linearRegCostFunction([ones(i,1), X(1:i,:)], y(1:i), theta, 0);error_val(i) = linearRegCostFunction([ones(size(Xval,1),1), Xval], yval, theta, 0);
end% -------------------------------------------------------------% =========================================================================end

polyFeatures.m

function [X_poly] = polyFeatures(X, p)
%POLYFEATURES Maps X (1D vector) into the p-th power
%   [X_poly] = POLYFEATURES(X, p) takes a data matrix X (size m x 1) and
%   maps each example into its polynomial features where
%   X_poly(i, :) = [X(i) X(i).^2 X(i).^3 ...  X(i).^p];
%% You need to return the following variables correctly.
X_poly = zeros(numel(X), p);% ====================== YOUR CODE HERE ======================
% Instructions: Given a vector X, return a matrix X_poly where the p-th 
%               column of X contains the values of X to the p-th power.
%
% for i = 1:pX_poly(:,i) = X .^ i;
end% =========================================================================end

validationCurve.m

function [lambda_vec, error_train, error_val] = ...validationCurve(X, y, Xval, yval)
%VALIDATIONCURVE Generate the train and validation errors needed to
%plot a validation curve that we can use to select lambda
%   [lambda_vec, error_train, error_val] = ...
%       VALIDATIONCURVE(X, y, Xval, yval) returns the train
%       and validation errors (in error_train, error_val)
%       for different values of lambda. You are given the training set (X,
%       y) and validation set (Xval, yval).
%% Selected values of lambda (you should not change this)
lambda_vec = [0 0.001 0.003 0.01 0.03 0.1 0.3 1 3 10]';% You need to return these variables correctly.
error_train = zeros(length(lambda_vec), 1);
error_val = zeros(length(lambda_vec), 1);% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return training errors in 
%               error_train and the validation errors in error_val. The 
%               vector lambda_vec contains the different lambda parameters 
%               to use for each calculation of the errors, i.e, 
%               error_train(i), and error_val(i) should give 
%               you the errors obtained after training with 
%               lambda = lambda_vec(i)
%
% Note: You can loop over lambda_vec with the following:
%
%       for i = 1:length(lambda_vec)
%           lambda = lambda_vec(i);
%           % Compute train / val errors when training linear 
%           % regression with regularization parameter lambda
%           % You should store the result in error_train(i)
%           % and error_val(i)
%           ....
%           
%       end
%
%for i = 1:length(lambda_vec)theta = trainLinearReg(X, y, lambda_vec(i));error_train(i) = linearRegCostFunction(X, y, theta, 0);error_val(i) = linearRegCostFunction(Xval, yval, theta, 0);
end% =========================================================================end

这篇关于Andrew Ng机器学习week6(Regularized Linear Regression and Bias/Variance)编程习题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/959276

相关文章

javaweb学习-jstl-c:forEach中 varStatus的属性简介

varStatus是<c:forEach>jstl循环标签的一个属性,varStatus属性。就拿varStatus=“status”来说,事实上定义了一个status名的对象作为varStatus的绑定值。该绑定值也就是status封装了当前遍历的状态,比如,可以从该对象上查看是遍历到了第几个元素:${status.count} 我们常会用c标签来遍历需要的数据,为了方便使用,varSta

java学习—null和isEmpty 区别

String fly1 = new String(); String fly2 = ""; String fly3 = null; 解释如下: 此时fly1是分配了内存空间,但值为空,是绝对的空,是一种有值(值存在为空而已) 此时fly2是分配了内存空间,值为空字符串,是相对的空,是一种有值(值存在为空字串) 此时fly3是未分配内存空间,无值,是一种无值(

javaWeb学习—getRequestURI,getRequestURL等的学习

我使用的是SpringMVC框架,做一个小的例子,说明一下对这个内容的学习和理解! 1:我的项目名称为 dufyun_SpringMVC  2:我测试的地址为 ${pageContext.servletContext.contextPath}/testName 3;后台获取的代码为: @RequestMapping(value="/testName",method=Req

maven学习系列——(五)maven聚合与继承

这一篇学习和整理maven的聚合和继承! 并用具体的项目讲解说明! 参考: http://www.cnblogs.com/xdp-gacl/p/4242221.html

maven学习系列——(四)maven仓库

这一篇学习和整理maven仓库的一些知识点 ! 参考: http://blog.csdn.net/wanghantong/article/details/36427433

maven学习系列——(三)maven项目的创建

这一篇大概会整理和总结到有如下知识点: (1):maven的使用入门一些命令 (2):用命令创建项目 (3):使用IDE集成工具创建项目–Eclipse和idea 3:使用Maven命令和Eclipse的Maven插件,创建Maven项目 (1)maven命令生成项目 新建一个文件目录,dos进入该目录并执行下面命令: mvn archetype:create -DgroupId=c

maven学习系列——(二)maven的安装和一些基本的配置

这一篇主要会总结maven在window上的安装,以及Eclipse安装maven插件。 会整理和贴出具体的安装步骤等! 配置大概会整理一下,方便自己查看和使用! Maven 的使用在Windows上使用比较多,一般的开发都是在Windows上;Linux上的使用相对比较少,不过会总结Windows和Linux系统两种安装方式。 首先会介绍在Windows和Eclipse安装Maven,L

maven学习系列——(一)maven简介

这个系列学习maven,主要是看maven实战和其他网站上整理出自己一些知识点,方便自己以后查找和使用! 这个系列的我先根据自己在公司经常使用到的一些知识点进行整理,后期在做完善! 计划:要在2017 年之前学习和整理完成! 1:什么 Maven ? - Maven意为“知识的积累”、“专家”或者“内行”的意思,maven是一个跨平台的项目管理工具,是 Apache的一个成功的开源

git学习—git log 和git diff

大概整理一下,供自查看 git log 对比两个分支差异: http://blog.csdn.net/u011240877/article/details/52586664 git diff文档 http://web.mit.edu/~mkgray/project/silk/root/afs/sipb/project/git/git-doc/git-diff.html git diff(

mysql学习—查询数据库中特定的值对应的表

遇到一个问题,我将问题抽象简单描述如下: 循环查询数据库所有表,查出字段中包含tes值的表,并且将test修改为hello? 因为自己不才找了很久也没有找到很好的方法,又对mysql的游标等用法不是很了解,在时间有限的情况下,发现了下面的方法,分享给大家: 1:查找 (1)使用工具 我使用的mysql的Navicat for MySQL的工具 (2)使用sql的语法 这个方式暂时