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: 

step1  load your data

      step2  plot your data

      step3  use Gradien Descent to learn theta

        step4  debugging

        step5  visualizing CostFunction J of theta



Here is a example of Linear Regression of one variable.

step1: loading data: in Octave Command  window,input :

 data=load(ex1data1.txt)       %then the Octave variable data hava the dataset of this example,

   includingX(exclude X0)  and y.

         X=data(:,1) ; y=data(:,2)       %X is variable vector ,y is the result vector

           m=length(y)                           %number of training example

step2:plotting data: in Octave Command  window,input : 

plot(x, y, 'rx', 'MarkerSize', 10);

    ylabel('Profit in $10,000s');

    xlabel('Population of City in 10,000s');

step3:Gradient Descent (important) ,including batch gradient descent and stochastic gradient descent,

   remember our goal is to get theta:

    X = [ones(m, 1), data(:,1)]; % Add a column of ones to x

    theta = zeros(2, 1); % initialize fitting parameters

            iterations = 1500;

    alpha=0.5 ; 

according to J of theta(this is used for checking weather J is on decressing):

   

input octave commands:

plot(X(:,2), X*theta, '-')
legend('Training data', 'Linear regression')


after that ,you now can use your data to predict,i.e:

predict= [1, 3.5] * theta;

step4 debugging:

1) Octave array indices start from one, not zero. If you’re storing θ0 andθ1 in a vector called theta, the values will be theta(1) and theta(2).

2)  If you are seeing many errors at runtime, inspect your matrix operations to make sure that you’re adding and multiplying matrices of compatible dimensions. Printing the dimensions of variables with the size command will help you debug.

3)    By default, Octave interprets math operators to be matrix operators.This is a common source of size incompatibility errors. If you don’t want matrix multiplication, you need to add the “dot” notation to specify this to Octave. For examples, A*B does a matrix multiply, while A.*B does an element-wise multiplication.

step5 Visualizing J(θ): in order to understand how J(θ) varies with changes in θ0 and θ1 .

input these commands in the picture below: 

        and amazing,U will get a 3-D figure and also a 2-D contour figure 

      

      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值