Machine learning(4-Linear Regression with multiple variables )

1、Multiple features

  • image.png
  • So what the form of the hypothesis should be ?

image.png

  • For convenience, define x0=1

image.png

  • 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 :

image.png

2、Gradient descent for multiple variables

  • image.png
  • Here is the gradient descent looks like

image.png

  • 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

image.png

4、Gradient descent in practice II: Learning rate

  • image.png
  • image.png
  • image.png

5、Features and Polynomial Regression

  • Housing price prediction

image.png

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

image.png

  • Or maybe a cubic model :

image.png

  • image.png
  • According to the graphical characteristics of the function, we can also use :

image.png

6、Normal Equation

  • Normal equation : method to solve for θ analytically
  • image.png
  • It is too long and involved
  • image.png
  • And now,I am going to take the dataset and add an extra column
  • image.png
  • Then construct a matrix X :

image.png

  • And construct a vector y :

image.png

  • Solve the vector using the normal equation :

image.png

  • We get :

image.png

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

image.png

  • 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

  • image.png
  • image.png

8、Supplement

image.pngimage.png

posted @ 2021-06-10 11:10  我在吃大西瓜呢  阅读(72)  评论(0编辑  收藏  举报