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 ; 

Machine Learning by Andrew Ng ---Linear Regression with one variable_第1张图片

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

Machine Learning by Andrew Ng ---Linear Regression with one variable_第2张图片   Machine Learning by Andrew Ng ---Linear Regression with one variable_第3张图片

input octave commands:

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


Machine Learning by Andrew Ng ---Linear Regression with one variable_第4张图片

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: 

Machine Learning by Andrew Ng ---Linear Regression with one variable_第5张图片

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

      

      

你可能感兴趣的:(Machine Learning by Andrew Ng ---Linear Regression with one variable)