Exercise 2: Linear Regression

话说LaTex用起来好爽

Matlab代码

迭代并且画出拟合曲线

Linear regression 公式如下

hθ(x)=θTx=∑i=0nθixi

(i是代表x的个数)

batch gradient descent update rule

θj:=θj−α1m∑i=1m(h(i)θ−y(i))x(i)j(for all j)
α=0.07
x = load('L:\\MachineLearning2016\\ex2x.dat');
y = load('L:\\MachineLearning2016\\ex2y.dat'); m = length(y);
x = [ones(m, 1), x];
theta=[0,0];%row vector figure % open a new figure window
plot(x(:,2), y, 'o');
ylabel('Height in meters')
xlabel('Age in years') hold on % Plot new data without clearing old plot
plot(x(:,2), x*transpose(theta), '-')
legend('Training data', 'Linear regression') %迭代方式1
newTheta1 =theta(1,1) - transpose(x(:,1)) * (x*transpose(theta) -y) *0.07 * 0.02;
newTheta2 =theta(1,2) - transpose(x(:,2)) * (x*transpose(theta) -y) *0.07 * 0.02;
theta=[newTheta1,newTheta2]; %迭代方式2
for ii = 1:1500
theta = theta - transpose( x*transpose(theta) -y ) * x * 0.07 * 0.02;
if rem(ii,100) == 0
hold on % Plot new data without clearing old plot
plot(x(:,2), x*transpose(theta), '-') end
end hold on % 打印最后一条拟合曲线
plot(x(:,2), x*transpose(theta), '+')

画出J(θ)的图像

Understanding J(θ)

J(θ)=12m∗∑i=1m(h(i)θ−y(i))2(i means the ith of sample)
J_vals = zeros(100, 100);   % initialize Jvals to 100x100 matrix of 0's
theta0_vals = linspace(-3, 3, 100);
theta1_vals = linspace(-1, 1, 100);
for i = 1:length(theta0_vals)
for j = 1:length(theta1_vals)
t = [theta0_vals(i); theta1_vals(j)];%column vector
J_vals(i,j) = sum( (x*t' -y).^2 ) * 0.01;
end
end % Plot the surface plot
% Because of the way meshgrids work in the surf command, we need to
% transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals';
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1')

斯坦福机器学习课程 Exercise 习题二的更多相关文章

  1. 斯坦福机器学习课程 Exercise 习题四

    Exercise 4: Logistic Regression and Newton’s Method 回顾一下线性回归 hθ(x)=θTx Logistic Regression hθ(x)=11+ ...

  2. 斯坦福机器学习课程 Exercise 习题三

    Exercise 3: Multivariate Linear Regression 预处理数据 Preprocessing the inputs will significantly increas ...

  3. 关于Coursera上的斯坦福机器学习课程的编程作业提交问题

    学习Coursera上的斯坦福机器学习课程的时候,需要向其服务器提交编程作业,我遇到如下问题: 'Submission failed: unexpected error: urlread: Peer ...

  4. Andrew Ng机器学习课程笔记(二)之逻辑回归

    Andrew Ng机器学习课程笔记(二)之逻辑回归 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7364636.html 前言 ...

  5. [基础]斯坦福cs231n课程视频笔记(二) 神经网络的介绍

    目录 Introduction to Neural Networks BP Nerual Network Convolutional Neural Network Introduction to Ne ...

  6. 【原】Coursera—Andrew Ng斯坦福机器学习(0)——课程地址和软件下载

    斯坦福大学机器学习 课程信息 机器学习是一门研究在非特定编程条件下让计算机采取行动的学科.最近二十年,机器学习为我们带来了自动驾驶汽车.实用的语音识别.高效的网络搜索,让我们对人类基因的解读能力大大提 ...

  7. cs229 斯坦福机器学习笔记(一)-- 入门与LR模型

    版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/Dinosoft/article/details/34960693 前言 说到机器学习,非常多人推荐的学习资 ...

  8. 斯坦福机器学习视频笔记 Week1 Linear Regression and Gradient Descent

    最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...

  9. 斯坦福机器学习视频笔记 Week1 线性回归和梯度下降 Linear Regression and Gradient Descent

    最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...

随机推荐

  1. js运动基础2(运动的封装)

    简单运动的封装 先从最简单的封装开始,慢慢的使其更丰富,更实用. 还是上一篇博文的代码,在此不作细说. 需求:点击按钮,让元素匀速运动. <!DOCTYPE html> <html ...

  2. 链表-LinkList

    什么是链表 维基百科:链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer).由于不必须按顺序存 ...

  3. maven手动添加jar包到pom仓库

    此处以顺丰jar包为例: mvn install:install-file -Dfile=D:\TSBrowserDownloads\SF-CSIM-EXPRESS-SDK-V1.-\SF-CSIM- ...

  4. Linux 文件复制命令cp

    文件复制命令cp 命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination) cp [option] source1 source2 source3 ... ...

  5. Angular 表单嵌套、动态表单

    说明: 组件使用了ng-zorro (https://ng.ant.design/docs/introduce/zh) 第一类:嵌套表单 1. 静态表单嵌套 demo.component.html & ...

  6. Java中的static(1)【持续更新】——关于Eclipse的No enclosing instance of type ... 错误的理解和改正

    No enclosing instance of type SomeClass is accessible. Must qualify the allocation with an enclosing ...

  7. yii中缓存(cache)详解 - 彼岸あ年華ツ

    缓存是用于提升网站性能的一种即简单又有效的途径.通过存储相对静态的数据至缓存以备所需,我们可以省去生成 这些数据的时间.在 Yii 中使用缓存主要包括配置和访问缓存组件 . 内部方法 一.缓存配置: ...

  8. Java 学习笔记之 Suspend和Resume

    Suspend和Resume: Suspend和Resume使用方法: 以下例子证明了线程确实被暂停了,而且还可以恢复成运行状态. public class SuspendResumeThread e ...

  9. Flume 学习笔记之 Flume NG+Kafka整合

    Flume NG集群+Kafka集群整合: 修改Flume配置文件(flume-kafka-server.conf),让Sink连上Kafka hadoop1: #set Agent name a1. ...

  10. 『开发技术』Ubuntu与Windows如何查看CPU&GPU&内存占用量

    0 序·简介 在使用Ubuntu或者Windows执行一些复杂数据运算时,需要关注下CPU.GPU以及内存占用量,如果数据运算超出了负荷,会产生难以预测的错误.本文将演示如何用简单地方式,实时监控Ub ...