【Week1】Linear Regression

Summary: Some prerequisites for the study course

  1. Knowledge of basic computer science principles and skills, at a level sufficient to write a reasonably non-trivial computer program.
  2. Familiarity with the basic probability theory.
  3. Familiarity with the basic linear algebra.

一、Introduction

1.What is Machine Learning?

Two definitions of Machine Learning are offered. Arthur Samuel described it as: “the field of study that gives computers the ability to learn without being explicitly programmed.” This is an older, informal definition.

Tom Mitchell provides a more modern definition: “A computer program is said to learn from experience E with respect to some class of tasks T and performance measure P, if its performance at tasks in T, as measured by P, improves with experience E.”

Example: playing checkers.

E = the experience of playing many games of checkers

T = the task of playing checkers.

P = the probability that the program will win the next game.

In general, any machine learning problem can be assigned to one of two broad classifications:

Supervised learning and Unsupervised learning.

2.Supervised Learning

Insert picture description here

In supervised learning, we are given a data set and already know what our correct output should look like, having the idea that there is a relationship between the input and the output.

Supervised learning problems are categorized into “regression” and “classification” problems. In a regression problem, we are trying to predict results within a continuous output, meaning that we are trying to map input variables to some continuous function. In a classification problem, we are instead trying to predict results in a discrete output. In other words, we are trying to map input variables into discrete categories.

Example 1:

Given data about the size of houses on the real estate market, try to predict their price. Price as a function of size is a continuous output, so this is a regression problem.

We could turn this example into a classification problem by instead making our output about whether the house “sells for more or less than the asking price.” Here we are classifying the houses based on price into two discrete categories.

Example 2:

(a) Regression - Given a picture of a person, we have to predict their age on the basis of the given picture

(b) Classification - Given a patient with a tumor, we have to predict whether the tumor is malignant or benign.

  1. Application scenarios of regression problems

The regression problem is usually used to predict a value, such as predicting housing prices, future weather conditions, etc., for example, the actual price of a product is 500 yuan, and the predicted value through regression analysis is 499 yuan. We think this is a better regression analysis. . A more common regression algorithm is the linear regression algorithm (LR). In addition, regression analysis is used on neural networks, and the uppermost layer does not need to add the softmax function, but directly accumulates the previous layer. Regression is an approximate prediction of the true value.

  1. Application scenarios for classification problems

Classification problems are used to put a label on things, usually the result is a discrete value. For example, to determine whether an animal in a picture is a cat or a dog, the classification is usually based on regression, and the softmax function is usually used to determine the category of the last layer of classification. Classification does not have the concept of approximation. There is only one correct result in the end, and the wrong one is wrong, and there will be no similar concepts. The most common classification method is logistic regression, or logical classification.

eg:
Insert picture description here

3.Unsupervised Learning

Insert picture description here
Unsupervised learning allows us to approach problems with little or no idea what our results should look like. We can derive structure from data where we don’t necessarily know the effect of the variables.

We can derive this structure by clustering the data based on relationships among the variables in the data.

With unsupervised learning there is no feedback based on the prediction results.

Example:

Clustering: Take a collection of 1,000,000 different genes, and find a way to automatically group these genes into groups that are somehow similar or related by different variables, such as lifespan, location, roles, and so on.

Non-clustering: The “Cocktail Party Algorithm”, allows you to find structure in a chaotic environment. (i.e. identifying individual voices and music from a mesh of sounds at a cocktail party).

eg:
Insert picture description here

二、Model and Cost Function

1.Model Representation

To describe the supervised learning problem slightly more formally, our goal is, given a training set, to learn a function h : X → Y so that h(x) is a “good” predictor for the corresponding value of y. For historical reasons, this function h is called a hypothesis. Seen pictorially, the process is therefore like this:

Insert picture description here

When the target variable that we’re trying to predict is continuous, such as in our housing example, we call the learning problem a regression problem. When y can take on only a small number of discrete values (such as if, given the living area, we wanted to predict if a dwelling is a house or an apartment, say), we call it a classification problem.

2.Cost Function

We can measure the accuracy of our hypothesis function by using a cost function. This takes an average difference (actually a fancier version of an average) of all the results of the hypothesis with inputs from x’s and the actual output y’s.
Insert picture description here

This function is otherwise called the “Squared error function”, or “Mean squared error”. The mean is halved (1/2)as a convenience for the computation of the gradient descent, as the derivative term of the square function will cancel out the 1/2 term. The following image summarizes what the cost function does
Insert picture description here
Insert picture description here

Cost Function - Intuition I(θ0 = 0)

Insert picture description here
Insert picture description here
Insert picture description here

Cost Function - Intuition II

Insert picture description here
Insert picture description here
Insert picture description here

三、Parameter Learning

1.Gradient Descent

So we have our hypothesis function and we have a way of measuring how well it fits into the data. Now we need to estimate the parameters in the hypothesis function. That’s where gradient descent comes in.

Imagine that we graph our hypothesis function based on its fields θ0 and θ1 (actually we are graphing the cost function as a function of the parameter estimates). We are not graphing x and y itself, but the parameter range of our hypothesis function and the cost resulting from selecting a particular set of parameters.

We put θ0 on the x axis and θ1 on the y axis, with the cost function on the vertical z axis. The points on our graph will be the result of the cost function using our hypothesis with those specific theta parameters. The graph below depicts such a setup.
Insert picture description here
We will know that we have succeeded when our cost function is at the very bottom of the pits in our graph, i.e. when its value is the minimum. The red arrows show the minimum points in the graph.

The way we do this is by taking the derivative (the tangential line to a function) of our cost function. The slope of the tangent is the derivative at that point and it will give us a direction to move towards. We make steps down the cost function in the direction with the steepest descent. The size of each step is determined by the parameter α, which is called the learning rate.
For example, the distance between each ‘star’ in the graph above represents a step determined by our parameter α. A smaller α would result in a smaller step and a larger α results in a larger step. The direction in which the step is taken is determined by the partial derivative of J(\theta_0,\theta_1)J(θ0,θ1). Depending on where one starts on the graph, one could end up at different points. The image above shows us two different starting points that end up in two different places.

The gradient descent algorithm is:

repeat until convergence:
Insert picture description here
ps:
Insert picture description here

2.Gradient Descent Intuition

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

3.Gradient Descent For Linear Regression

When specifically applied to the case of linear regression, a new form of the gradient descent equation can be derived. We can substitute our actual cost function and our actual hypothesis function and modify the equation to :
Insert picture description here
Insert picture description here
The point of all this is that if we start with a guess for our hypothesis and then repeatedly apply these gradient descent equations, our hypothesis will become more and more accurate.

So, this is simply gradient descent on the original cost function J. This method looks at every example in the entire training set on every step, and is called batch gradient descent. Note that, while gradient descent can be susceptible to local minima in general, the optimization problem we have posed here for linear regression has only one global, and no other local, optima; thus gradient descent always converges (assuming the learning rate α is not too large) to the global minimum. Indeed, J is a convex quadratic function. Here is an example of gradient descent as it is run to minimize a quadratic function.
Insert picture description here
The ellipses shown above are the contours of a quadratic function. Also shown is the trajectory taken by gradient descent, which was initialized at (48,30). The x’s in the figure (joined by straight lines) mark the successive values of θ that gradient descent went through as it converged to its minimum.

四、Linear Algebra Review

This part is relatively simple linear algebra knowledge, you can skip it, but you can learn about some function applications of Octave

1.Matrices and Vectors

Insert picture description here
Run the cell below to get familiar with the commands in Octave/Matlab. Feel free to create matrices and vectors and try out different things.

test
% The ; denotes we are going back to a new row.
 A = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12]

% Initialize a vector 
 v = [1;2;3] 

% Get the dimension of the matrix A where m = rows and n = columns
 [m,n] = size(A)

% You could also store it this way
 dim_A = size(A)

% Get the dimension of the vector v 
 dim_v = size(v)

% Now let's index into the 2nd row 3rd column of matrix A
 A_34 = A(4,3)

result:

A =

    1    2    3
    4    5    6
    7    8    9
   10   11   12

v =

   1
   2
   3

m =  4
n =  3
dim_A =

   4   3

dim_v =

   3   1

A_34 =  12

2.Addition and Scalar Multiplication

Insert picture description here
Experiment below with the Octave/Matlab commands for matrix addition and scalar multiplication. Feel free to try out different commands. Try to write out your answers for each command before running the cell below.

test
% Initialize matrix A and B 
A = [1, 2, 4; 5, 3, 2]
B = [1, 3, 4; 1, 1, 1]

% Initialize constant s 
s = 2

% See how element-wise addition works
add_AB = A + B 

% See how element-wise subtraction works
sub_AB = A - B

% See how scalar multiplication works
mult_As = A * s

% Divide A by s
div_As = A / s

% What happens if we have a Matrix + scalar?
add_As = A + s

result:

A =

   1   2   4
   5   3   2

B =

   1   3   4
   1   1   1

s =  2
add_AB =

   2   5   8
   6   4   3

sub_AB =

   0  -1   0
   4   2   1

mult_As =

    2    4    8
   10    6    4

div_As =

   0.50000   1.00000   2.00000
   2.50000   1.50000   1.00000

add_As =

   3   4   6
   7   5   4

3.Matrix-Vector Multiplication

Insert picture description here
Below is an example of a matrix-vector multiplication. Make sure you understand how the multiplication works. Feel free to try different matrix-vector multiplications.

test
A = [1, 2, 4; 5, 3, 2]
B = [1, 3, ;4, 1; 1, 1]

Clichong = A * B

result:

A =

   1   2   4
   5   3   2

B =

   1   3
   4   1
   1   1

Clichong =

   13    9
   19   20

An m x n matrix multiplied by an n x o matrix results in an m x o matrix. In the above example, a 3 x 2 matrix times a 2 x 2 matrix resulted in a 3 x 2 matrix.

To multiply two matrices, the number of columns of the first matrix must equal the number of rows of the second matrix.

test2
% Initialize a 3 by 2 matrix 
A = [1, 2, 6; 3, 4, 62;5, 6, 62]

% Initialize a 2 by 1 matrix 
B = [1; 2; 5] 

% We expect a resulting matrix of (3 by 2)*(2 by 1) = (3 by 1) 
mult_AB = A*B

% Make sure you understand why we got that result

result:

A =

    1    2    6
    3    4   62
    5    6   62

B =

   1
   2
   5

mult_AB =

    35
   321
   327

4.Matrix Multiplication Properties

Insert picture description here
When multiplying the identity matrix after some matrix (A∗I), the square identity matrix’s dimension should match the other matrix’s columns. When multiplying the identity matrix before some other matrix (I∗A), the square identity matrix’s dimension should match the other matrix’s rows.

test
% Initialize random matrices A and B 
A = [1,2,4;4,5,7;3,7,8]
B = [1,1,2;2,1,2;5,6,7]

% Initialize a 2 by 2 identity matrix
I = eye(3)

% The above notation is the same as I = [1,0;0,1]

% What happens when we multiply I*A ? 
IA = I*A 

% How about A*I ? 
AI = A*I 

% Compute A*B 
AB = A*B 

% Is it equal to B*A? 
BA = B*A 

% Note that IA = AI but AB != BA

result:

A =

   1   2   4
   4   5   7
   3   7   8

B =

   1   1   2
   2   1   2
   5   6   7

I =

Diagonal Matrix

   1   0   0
   0   1   0
   0   0   1

IA =

   1   2   4
   4   5   7
   3   7   8

AI =

   1   2   4
   4   5   7
   3   7   8

AB =

   25   27   34
   49   51   67
   57   58   76

BA =

    11    21    27
    12    23    31
    50    89   118

5.Inverse and Transpose

Insert picture description here

test
% Initialize matrix A 
A = [1,2,0;0,5,6;7,0,9]

% Transpose A 
A_trans = A' 

% Take the inverse of A 
A_inv = inv(A)

% What is A^(-1)*A? 
A_invA = inv(A)*A

result:

A =

   1   2   0
   0   5   6
   7   0   9

A_trans =

   1   0   7
   2   5   0
   0   6   9

A_inv =

   0.348837  -0.139535   0.093023
   0.325581   0.069767  -0.046512
  -0.271318   0.108527   0.038760

A_invA =

   1.00000  -0.00000   0.00000
   0.00000   1.00000  -0.00000
  -0.00000   0.00000   1.00000
test2
Clichong = [1,2;2,1]
C = Clichong'
D = inv(Clichong)
E = D * Clichong

result:

Clichong =

   1   2
   2   1

C =

   1   2
   2   1

D =

  -0.33333   0.66667
   0.66667  -0.33333

E =

   1   0
   0   1

Reference materials: https://www.coursera.org

Guess you like

Origin blog.csdn.net/weixin_44751294/article/details/109086962