Coursera machine learning 第二周 编程作业 Linear Regression
必做:
[*] warmUpExercise.m - Simple example function in Octave/MATLAB
[*] plotData.m - Function to display the dataset
[*] computeCost.m - Function to compute the cost of linear regression
[*] gradientDescent.m - Function to run gradient descent
1.warmUpExercise.m
A = eye();
2.plotData.m
plot(x, y, 'rx', 'MarkerSize', ); % Plot the data
ylabel('Profit in $10,000s'); % Set the y-axis label
xlabel('Population of City in 10,000s'); % Set the x-axis label
3.computeCost.m
function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y % Initialize some useful values
m = length(y); % number of training examples % You need to return the following variables correctly
J = ; % ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost. H = X*theta-y;
J = (1/(2*m))*sum(H.*H); % ========================================================================= end
公式: 
注意matlab中 .* 的用法。
4.gradientDescent.m
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha % Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, ); for iter = :num_iters % ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCost) and gradient here. H = X*theta-y;
theta(1)=theta(1)-alpha*(1/m)*sum(H.*X(:,1));
theta(2)=theta(2)-alpha*(1/m)*sum(H.*X(:,2)); % ============================================================ % Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta); end end
单变量梯度下降
对函数J(θ)求偏导

即 H.*X(:,1)
θi向着梯度最小的方向减少,alpha为步长。

theta(i)=theta(i)-alpha*(1/m)*sum(H.*X(:,i));
Coursera machine learning 第二周 编程作业 Linear Regression的更多相关文章
- Coursera machine learning 第二周 quiz 答案 Linear Regression with Multiple Variables
https://www.coursera.org/learn/machine-learning/exam/7pytE/linear-regression-with-multiple-variables ...
- Coursera machine learning 第二周 quiz 答案 Octave/Matlab Tutorial
https://www.coursera.org/learn/machine-learning/exam/dbM1J/octave-matlab-tutorial Octave Tutorial 5 ...
- Coursera-AndrewNg(吴恩达)机器学习笔记——第二周编程作业
一.准备工作 从网站上将编程作业要求下载解压后,在Octave中使用cd命令将搜索目录移动到编程作业所在目录,然后使用ls命令检查是否移动正确.如: 提交作业:提交时候需要使用自己的登录邮箱和提交令牌 ...
- Coursera-AndrewNg(吴恩达)机器学习笔记——第二周编程作业(线性回归)
一.准备工作 从网站上将编程作业要求下载解压后,在Octave中使用cd命令将搜索目录移动到编程作业所在目录,然后使用ls命令检查是否移动正确.如: 提交作业:提交时候需要使用自己的登录邮箱和提交令牌 ...
- 【Machine Learning】单参数线性回归 Linear Regression with one variable
最近开始看斯坦福的公开课<Machine Learning>,对其中单参数的Linear Regression(未涉及Gradient Descent)做个总结吧. [设想] ...
- Andrew Ng机器学习编程作业: Linear Regression
编程作业有两个文件 1.machine-learning-live-scripts(此为脚本文件方便作业) 2.machine-learning-ex1(此为作业文件) 将这两个文件解压拖入matla ...
- [Machine Learning (Andrew NG courses)]II. Linear Regression with One Variable
- [Machine Learning (Andrew NG courses)]IV.Linear Regression with Multiple Variables
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenFoXzE5OTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- Coursera公开课-Machine_learing:编程作业
第二周编程作业:Linear Regression 分为单一变量和多变量,假想函数为:hθ(x)=θ0+θ1x1+θ2x2+θ3x3+⋯+θnxn.明显已经包含单一变量的情况,所以完成多变量可以一并解 ...
随机推荐
- JAVA实现网页快照,存为图片格式
原文:http://blog.csdn.net/java2000_net/article/details/3643528 截取的google的效果,将就吧,不是特别好. 但是作为普通的应用,我想这个效 ...
- ASIHTTPRequest框架使用总结系列之阿堂教程4(下载数据)
从本篇开始,阿堂准备进一步介绍ASIHTTPRequest框架下载数据和上传数据的实际应用. 为了实现多线程并发请求网络能力,ASIHTTPRequest被设计成 NSOperation ...
- 求逆序对数总结 & 归并排序
用归并排序方式 最原始的方法的复杂度是O(n^2). 使用归并排序的方式,可以把复杂度降低到O(nlgn). 设A[1..n]是一个包含N个非负整数的数组.如果在i〈 j的情况下,有A〉A[j],则( ...
- Hive设置参数的三种方法
Hive提供三种可以改变环境变量的方法,分别是:(1).修改${HIVE_HOME}/conf/hive-site.xml配置文件:(2).命令行参数:(3).在已经进入cli时进行参数声明.下面分别 ...
- Android学习(十八)Toast的使用
一.什么是Toast 1.Toast是一种提供给用户简洁提示信息的视图. 2.该视图以浮于应用程序之上的形式呈现给用户, Toast提示界面不获取焦点,在不影响用户使用的情况下,给用户某些提示. 3. ...
- OpenStack二三事(1)
更新系列不是教材,不说教,不讲道理,仅仅记录. OpenStack在云计算领域大热,没有理由不去了解它. 先说说我对OpenStack的感觉,开源.廉价.麻烦.大家都在用,在了解开发流程后.OpenS ...
- 怎么关闭win10快速访问功能?关闭Windows10系统快速访问方法
怎么关闭win10快速访问功能?关闭Windows10系统快速访问方法 Windows10系统的"快速访问"功能很容易泄露电脑中的隐私,用什么方法可以让这个功能消失,避免电脑的个人 ...
- SecureCRT如何调整好看的黄色
1.常规 →默认会话→编辑默认编辑→白黑 字体为console 2.全局选项 ANSI颜色有一个 把黄色 拖过去即可
- 文件——文件指针——文件练习(随机产生N个手机号)——自动关文件
python 2 3file() #python 2读模式 写模式 追加模式 只要沾上了r,文件不存的就会报错读模式 r 读写模式 r+(可以写)1.不能写2.文件不存在报错f=open('123', ...
- 奥巴马(Obama)获胜演讲全文[中英对照]+高清视频下载
http://www.amznz.com/obama-speech/如果还有人对美国是否凡事都有可能存疑,还有人怀疑美国奠基者的梦想在我们所处的时代是否依然鲜活,还有人质疑我们的民主制度的力量,那么今 ...