Machine learning(4-Linear Regression with multiple variables )

时间:2023-03-09 07:19:03
Machine learning(4-Linear Regression with multiple variables )

1、Multiple features

  • Machine learning(4-Linear Regression with multiple variables )
  • So what the form of the hypothesis should be ?

Machine learning(4-Linear Regression with multiple variables )

  • For convenience, define x0=1

Machine learning(4-Linear Regression with multiple variables )

  • At this time, the parameter in the model is a ( + 1)-dimensional vector, and any training instance is also a ( + 1)-dimensional vector. The dimension of the feature matrix is { ∗ ( + 1)} , so the formula can be simplified to :

Machine learning(4-Linear Regression with multiple variables )

2、Gradient descent for multiple variables

  • Machine learning(4-Linear Regression with multiple variables )
  • Here is the gradient descent looks like

Machine learning(4-Linear Regression with multiple variables )

  • Python code:
def computeCost(X, y, theta):
inner = np.power(((X * theta.T) - y), 2)
return np.sum(inner) / (2 * len(X)

3、Gradient descent in practice I :Feature Scaling

  • An idea about feature scaling(特征缩放) --- make sure features are on a similar scale and get every feature into approximately a -1≤xi≤1 range

Machine learning(4-Linear Regression with multiple variables )

4、Gradient descent in practice II: Learning rate

  • Machine learning(4-Linear Regression with multiple variables )
  • Machine learning(4-Linear Regression with multiple variables )
  • Machine learning(4-Linear Regression with multiple variables )

5、Features and Polynomial Regression

  • Housing price prediction

Machine learning(4-Linear Regression with multiple variables )

  • Linear regression is not suitable for all data, sometimes we need a curve to fit our data, such as a quadratic model :

Machine learning(4-Linear Regression with multiple variables )

  • Or maybe a cubic model :

Machine learning(4-Linear Regression with multiple variables )

  • Machine learning(4-Linear Regression with multiple variables )
  • According to the graphical characteristics of the function, we can also use :

Machine learning(4-Linear Regression with multiple variables )

6、Normal Equation

  • Normal equation : method to solve for θ analytically
  • Machine learning(4-Linear Regression with multiple variables )
  • It is too long and involved
  • Machine learning(4-Linear Regression with multiple variables )
  • And now,I am going to take the dataset and add an extra column
  • Machine learning(4-Linear Regression with multiple variables )
  • Then construct a matrix X :

Machine learning(4-Linear Regression with multiple variables )

  • And construct a vector y :

Machine learning(4-Linear Regression with multiple variables )

  • Solve the vector using the normal equation :

Machine learning(4-Linear Regression with multiple variables )

  • We get :

Machine learning(4-Linear Regression with multiple variables )

pinv(X'*X)*X'*y
  • How to choose gradient descent or normal equation ?

Machine learning(4-Linear Regression with multiple variables )

  • Use python to implement Normal Equation
import numpy as np

def normalEqn(X, y):
theta = np.linalg.inv(X.T@X)@X.T@y #X.T@X 等价于 X.T.dot(X)
return theta

7、Normal Equation Non-invertibility

  • Machine learning(4-Linear Regression with multiple variables )
  • Machine learning(4-Linear Regression with multiple variables )

8、Supplement

Machine learning(4-Linear Regression with multiple variables )Machine learning(4-Linear Regression with multiple variables )