Linear regression with one variable

原创 2016年05月30日 23:49:18

1.Promble:

       In this part of this exercise, you will implement linear regression with one variable to predict profits for a food truck. Suppose you are the CEO of a restaurant franchise and are considering different cities for opening a new outlet. The chain already has trucks in various cities and you have data for profits and populations from the cities.
       You would like to use this data to help you select which city to expand to next.

       The file ex1data1.txt contains the dataset for our linear regression problem. The first column is the population of a city and the second column is the profit of a food truck in that city. A negative value for profit indicates a loss.


2.Computing the cost J(θ):


此部分matlab代码如下:

<span style="font-size:14px;">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 = 0;
 
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
%               You should set J to the cost.
J = sum((X * theta - y).^2) / (2*m);     % X(79,2)  theta(2,1)</span>
 
end




3.Gradient Descent&Update Equations



此部分matlab代码如下:

<span style="font-size:14px;">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);
theta_s=theta;
 
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.
    %
    theta(1) = theta(1) - alpha / m * sum(X * theta_s - y);       
    theta(2) = theta(2) - alpha / m * sum((X * theta_s - y) .* X(:,2));   
    theta_s=theta; 
    
 
    % ============================================================
 
    % Save the cost J in every iteration    
    J_history(iter) = computeCost(X, y, theta);
 
end
J_history
end</span>


最终程序运行结果如下:








整个程序的代码见:http://download.csdn.net/detail/zhe123zhe123zhe123/9535946



版权声明:本文为博主原创文章,未经博主允许不得转载。

Stanford机器学习---第三讲. 逻辑回归和过拟合问题的解决 logistic Regression & Regularization

本栏目(Machine learning)包括单参数的线性回归、多参数的线性回归、Octave Tutorial、Logistic Regression、Regularization、神经网络、机器学...
  • abcjennifer
  • abcjennifer
  • 2012年07月07日 20:45
  • 171426

LibLinear(SVM包)使用说明之(一)README

LibLinear(SVM包)使用说明之(一)READMEzouxy09@qq.comhttp://blog.csdn.net/zouxy09  本文主要是翻译liblinear-1.93版本的REA...
  • zouxy09
  • zouxy09
  • 2013年09月02日 19:53
  • 64543

Machine Learning - II. Linear Regression with One Variable单变量线性回归 (Week 1)

http://blog.csdn.net/pipisorry/article/details/43115525机器学习Machine Learning - Andrew NG courses学习笔记L...
  • pipisorry
  • pipisorry
  • 2015年01月25日 17:50
  • 2264

Machine Learning by Andrew Ng ---Linear Regression with one variable

Linear Regression is often used for predicting . The simple step of Linear Regression is always like...
  • meanme
  • meanme
  • 2015年03月08日 14:48
  • 888

Linear Regression & Ridge Regression的matlab实现

背景与原理: 给定一些离散点,利用线性回归过程获取多项式逼近拟合回归曲线。 Polynomial Curve Fitting 其中 a是需要求的位置系数。 于是根据mea...
  • rk2900
  • rk2900
  • 2013年06月09日 10:30
  • 7823

Android开发笔记(成长轨迹)

1.控制台输出:called unimplemented OpenGL ES API 调用了未实现的OpenGL ES API函数,一般由于导入的第三方库如地图库,里面有用到OpenGL,但是模拟器...
  • mad1989
  • mad1989
  • 2013年06月21日 09:44
  • 127903

【Stanford机器学习笔记】1-Linear Regression with One Variable

【Stanford机器学习笔记】1-Linear Regression with One Variable
  • Neil_Pan
  • Neil_Pan
  • 2016年04月28日 21:07
  • 1010

Linear Regression with One Variable

Linear Regression with One Variable =========================Model and Cost Function=======...
  • qq_31600497
  • qq_31600497
  • 2015年12月17日 21:02
  • 196

Stanford Machine Learning: (2). Logistic_Regression

Classification Where y is a discrete value Develop the logistic regression algorithm to determ...
  • chlele0105
  • chlele0105
  • 2014年07月29日 13:38
  • 1042

Spring4.0+Hibernate4.2.整合出现java.lang.ClassNotFoundException: org.hibernate.engine.FilterDefinition

1. 异常 Exception in thread "main"  org.springframework.beans.factory.BeanCreationException: Error cre...
  • biezhihua
  • biezhihua
  • 2013年11月15日 18:31
  • 17554
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Linear regression with one variable
举报原因:
原因补充:

(最多只允许输入30个字)