线性回归(linear regression)实践篇

之前一段时间在coursera看了Andrew ng的机器学习的课程,感觉还不错,算是入门了。

这次打算以该课程的作业为主线,对机器学习基本知识做一下总结。小弟才学疏浅,如有错误。敬请指导。

问题原描写叙述:

you will implement linear regression with one
variable to predict prots for a food truck. Suppose you are the CEO of a
restaurant franchise and are considering dierent cities for opening a new
outlet. The chain already has trucks in various cities and you have data for
prots and populations from the cities.

简单来说,就是依据一个城市的人口数量,来预測一辆快餐车能获得的利益。

数据集大概是这样子的:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGluZ2VybGFubGFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

一行数据为一个样本。第一列表示人口,第二列表示利益。

首先。先把数据可视化。

%% ======================= Part 2: Plotting =======================
fprintf('Plotting Data ...\n')
data = load('ex1data1.txt');
X = data(:, 1); y = data(:, 2);
m = length(y); % number of training examples % Plot Data
% Note: You have to complete the code in plotData.m
plotData(X, y); fprintf('Program paused. Press enter to continue.\n');
pause;
function plotData(x, y)
%PLOTDATA Plots the data points x and y into a new figure
% PLOTDATA(x,y) plots the data points and gives the figure axes labels of
% population and profit. % ====================== YOUR CODE HERE ======================
% Instructions: Plot the training data into a figure using the
% "figure" and "plot" commands. Set the axes labels using
% the "xlabel" and "ylabel" commands. Assume the
% population and revenue data have been passed in
% as the x and y arguments of this function.
%
% Hint: You can use the 'rx' option with plot to have the markers
% appear as red crosses. Furthermore, you can make the
% markers larger by using plot(..., 'rx', 'MarkerSize', 10); figure; % open a new figure window plot(x, y, 'rx', 'MarkerSize', 10); % Plot the data
ylabel('Profit in $10,000s'); % Set the y label
xlabel('Population of City in 10,000s'); % Set the x label % ============================================================ end

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGluZ2VybGFubGFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

计算cost function

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
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost.
H = X*theta;
diff = H - y;
%J = sum(diff.^2)/(2*m);
J = sum(diff.*diff)/(2*m); % ========================================================================= end

为了方便理解上面代码,看看各变量大概长什么样子的。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGluZ2VybGFubGFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

梯度下降法计算參数theta

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESENT(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, 1); for iter = 1: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) - sum(H.* X(:,1))*alpha/m;%感觉这样写挺搓的
theta(2) = theta(2) - sum(H.* X(:,2))*alpha/m;
%theta = theta - alpha * (X' * (X * theta - y)) / m; % ============================================================ % Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta); end end

难以理解的是theta = theta - alpha * (X' * (X * theta - y)) / m; 这样的向量化算法。

先看看theta本质是怎么计算的

再看看各变量长什么样子的

算出theta之后,就能够画出拟合直线了。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGluZ2VybGFubGFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

注:本文作者linger,如有转载。请标明转载于http://blog.csdn.net/lingerlanlan。

本文链接:http://blog.csdn.net/lingerlanlan/article/details/32162559

从零单排入门机器学习:线性回归(linear regression)实践篇的更多相关文章

  1. 从零单排入门机器学习:Octave/matlab的经常使用知识之矩阵和向量

    Octave/matlab的经常使用知识之矩阵和向量 之前一段时间在coursera看了Andrew ng的机器学习的课程,感觉还不错.算是入门了.这次打算以该课程的作业为主线,对机器学习基本知识做一 ...

  2. Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable

    原文:http://blog.csdn.net/abcjennifer/article/details/7700772 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...

  3. 机器学习(三)--------多变量线性回归(Linear Regression with Multiple Variables)

    机器学习(三)--------多变量线性回归(Linear Regression with Multiple Variables) 同样是预测房价问题  如果有多个特征值 那么这种情况下  假设h表示 ...

  4. 斯坦福CS229机器学习课程笔记 Part1:线性回归 Linear Regression

    机器学习三要素 机器学习的三要素为:模型.策略.算法. 模型:就是所要学习的条件概率分布或决策函数.线性回归模型 策略:按照什么样的准则学习或选择最优的模型.最小化均方误差,即所谓的 least-sq ...

  5. 机器学习 (一) 单变量线性回归 Linear Regression with One Variable

    文章内容均来自斯坦福大学的Andrew Ng教授讲解的Machine Learning课程,本文是针对该课程的个人学习笔记,如有疏漏,请以原课程所讲述内容为准.感谢博主Rachel Zhang的个人笔 ...

  6. 机器学习 (二) 多变量线性回归 Linear Regression with Multiple Variables

    文章内容均来自斯坦福大学的Andrew Ng教授讲解的Machine Learning课程,本文是针对该课程的个人学习笔记,如有疏漏,请以原课程所讲述内容为准.感谢博主Rachel Zhang 的个人 ...

  7. TensorFlow 学习笔记(1)----线性回归(linear regression)的TensorFlow实现

    此系列将会每日持续更新,欢迎关注 线性回归(linear regression)的TensorFlow实现 #这里是基于python 3.7版本的TensorFlow TensorFlow是一个机器学 ...

  8. Ng第二课:单变量线性回归(Linear Regression with One Variable)

    二.单变量线性回归(Linear Regression with One Variable) 2.1  模型表示 2.2  代价函数 2.3  代价函数的直观理解 2.4  梯度下降 2.5  梯度下 ...

  9. 斯坦福第二课:单变量线性回归(Linear Regression with One Variable)

    二.单变量线性回归(Linear Regression with One Variable) 2.1  模型表示 2.2  代价函数 2.3  代价函数的直观理解 I 2.4  代价函数的直观理解 I ...

随机推荐

  1. Php learn note

    Php learn note 1. Between two part of ECHO, there is , sign rather than + sign. echo 'Hello World!!' ...

  2. nyoj--491--幸运三角形(dfs)

    幸运三角形 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 话说有这么一个图形,只有两种符号组成('+'或者'-'),图形的最上层有n个符号,往下个数依次减一,形成倒置的 ...

  3. Gym-101915J The Volcano Eruption 计算几何

    题面 题意:给你一个矩阵,然后有很多的圆,这些圆可能相交着,一个或者几个就导致这个矩形被分割开了,就是从最下面的边到上面的边,连线被这些圆阻隔了,每一堆圆当做一个阻碍,问一共有几个阻碍 题解:看起来好 ...

  4. js设计模式-工厂模式(XHR工厂)

    场景:如果代码中需要多次执行Ajax请求,那么明智的做法是把创建这种对象的代码提取到一个类中,并创建一个包装器来包装在实际请求时所要经历的一系列步骤.简单工厂非常适合这种场合. /*AjaxHandl ...

  5. js作业

    1.一张纸的厚度是0.0001米,将纸对折,对折多少次厚度超过珠峰高度8848米var sum=0;i=0;a=0.0001;for(i=0;i<100;i++){ a=a*2; sum=sum ...

  6. Kettle通用数据贴源作业设计

    本设计基于以下需求提出 1. 快速接入数据源表(贴源/落地) 2. 无须给单独表开发转换/作业 3. 动态生成数据源连接, 表字段等信息(预先保存在数据仓库中) 本设计所需条件 1. 数据源为关系型数 ...

  7. chrome、firefox、IE中input输入光标位置错位解决方案

    以前在项目里碰到过一个问题 input输入框用一个背景图模拟,设置height和line-height一样的高度,使里面的输入文字能够居中, 在FF下出现的情况是:点击input时,输入光标其实上跟i ...

  8. <转>Socket编程——基础介绍

    最近系统的看了下unix网络编程的一些内容,对socket的理解有了进一步的加深,在看APUE的时候,那会儿看socket上面介绍的比较少,只是模糊的懂了如何去写一个简单的TCP服务端和客户端,对其中 ...

  9. PhotoZoom Classic 7有什么用?高品质的放大模糊图片!

    PhotoZoom Classic 7专门用于放大照片,同时保持质量.该软件配备了BenVista独特的S-Spline技术,可轻松超越Photoshop的双三次插值等替代解决方案. PhotoZoo ...

  10. idea中SVN的运用

    1.1.1 上传代码时可以指定忽略一些上传目录 1.1.2 设置项目上传的路径 1.1.3 解决上传路径中包含“svn”目录问题 上传 从 SVN 服务器中检出代码到工作空间