[Machine Learning][Andrew ng's lesson] 标准表达式 Normal Equation

Normal Equation :

According to Features Matrix X and the Result Matrix y

and the H(x) turns out to be

i=0nXiθi(Xo=1)

From pinv(X’*X)*X’*y to get the Correct θ Matrix.
Theory: To set the partial derivative(θj to J(θ)) equal to 0.

(In the same time ,it is not necessary to use Feature Scaling)

    h = 0 + 5*X1 + 7*X2 
    X = [1 2 4; 1 8 6; 1 3 7]
    y = [38;82;64]
    pinv(X' * X) * X' * y : 
    ans =
        -1.2221e-012
        5.0000e+000
        7.0000e+000

Disadvantage:
Need to compute pinv(X’X) O(n*n*n)
Slow if n is very large
Notice:
Even the X’*X can’t be inversed. Using pinv() instead of using inv() will compute the answers correctly.

Expand

It’s obviously less possible that a matrix can’t be inversed, but it can happen.
The Reasons are probably below:
1. Some features are same or have linear connection. For example, mile and feet are same, so we may know θx + 3.33θy = AExactNumber, but we can’t decide the θx and θy’s value, because there are too many possibilities.
2. The number of data (m) is less than the number of features. For example, we have 10 datas and 100 features. However ,we have a technique to solve this problem, which can be used to get h(x) which is made up of many variables from small datas.

Technique Explanation:

This technique will also solve the problem of overfitting by plusing a penality to the size of θ in J(θ).
(NOTICE! We only penalize θ from 1 to n, we don’t penalize θ0)
So the J may like this
这里写图片描述
And the normal equation will change to

newM = eye(n + 1);
newM(1,1) = 0;
pinv(X'*X + λ*newM)*X'*y

It can be proved that the Matrix which is to plus a new Matrix(λ >0) must has inverse Matrix.
So, we can get correct θ from small datas now!(λ>0)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值