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.明显已经包含单一变量的情况,所以完成多变量可以一并解 ... 
随机推荐
- LAMP中php配置
			内容概要:一. php配置1. 配置disable_functiondisable_functions = eval,assert,popen,passthru,escapeshellarg,esc ... 
- 控制面板cpl大全
			ALSNDMGR.CPL AC97 Audio组态设定appwiz.cpl 添加和删除程序bthprops.cpldesk.cpl 显示属性firewall.cpl Windows防火墙hdwwi ... 
- 5分钟教程:如何通过UART获得root权限
			写在前面的话 你知道物联网设备以及其他硬件制造商是如何调试和测试自家设备的吗?没错,绝大多数情况下,他们都会留下一个串行接口,这样就可以利用这个接口并通过shell来读取实时的调试日志或与硬件进行交互 ... 
- SparkMLlib聚类学习之KMeans聚类
			SparkMLlib聚类学习之KMeans聚类 (一),KMeans聚类 k均值算法的计算过程非常直观: 1.从D中随机取k个元素,作为k个簇的各自的中心. 2.分别计算剩下的元素到k个簇中心的相异度 ... 
- Java 分支结构 - if...else/switch
			Java 分支结构 - if...else/switch 顺序结构只能顺序执行,不能进行判断和选择,因此需要分支结构. Java 有两种分支结构: if 语句 switch 语句 if 语句 一个 i ... 
- DNS主从服务器
			一.目的: 我们知道,DNS服务器在网络服务中可能出现故障当机等状况,会导致DNS服务瘫痪,显然在实际的网络应用中我们不希望出现这种状况,所有我们就要配置从 服务器来在主DNS服务器出现故障时代替他来 ... 
- HTML基础知识总结一
			一.HTML是什么? HTML是超文本标记语言的英文缩写,"超文本"就是指页面内能够包括图片.链接.甚至音乐.程序等非文字元素.超文本标记语言的结构包括"头"部 ... 
- HDU4647:Another Graph Game(贪心)
			Problem Description Alice and Bob are playing a game on an undirected graph with n (n is even) nodes ... 
- (三)Thymeleaf标准表达式之——文字(literals)
			2.2 文字(literals) 模板名称:text.html 2.2.1 文本(text literals)(放在 单引号 里面,可以是任意字符) <p> Now you are loo ... 
- java替换文本中所有的正则符号 Java问题通用解决代码
			开发时遇到一个需求,需要对一段文本中的所有正则符号进行转义,不然使用split分割方法分割文本的话无效,想到用替换来做,全部替换正则符号为转义后的符号 贴java实现代码: 1.测试版 ... 
