1. 程式人生 > >Machine Learning - week 2 - Multivariate Linear Regression

Machine Learning - week 2 - Multivariate Linear Regression

learning itl 技術分享 隨著 刪除 mean 9.png ear 可能性

Gradient Descent in Practice - Feature Scaling

Make sure features are on a similar scale.

Features 的範圍越小,總的可能性就越小,計算速度就能加快。

Dividing by the range

通過 feature/range 使每個 feature 大概在 [-1, 1] 的範圍內

下題是一個例子:

技術分享

技術分享

Mean normalization

將值變為接近 0。除了 x0,因為 x0 的值為 1。

技術分享

mu1 是 average value of x1 in trainning sets;

S1 是 x1 的範圍大小,比如臥室是 [0, 5],那麽範圍為 5 - 0 = 5。

確保 gradient descent 工作正確

技術分享

如上圖,這個圖像是正確的,隨著循環次數的增加,J(θ) 主鍵減小。超過一定循環次數之後,J(θ) 曲線趨於平緩。可以根據圖像得出什麽時候停止,或者當每次循環,J(θ) 的變化小於 ε 時停止。

圖像上升

技術分享

說明 α 取值大了,應該減小。真實的圖像可能如下:

技術分享

如果 α 足夠小,那麽能緩慢但完全覆蓋。

如果 α 太大:在每次循環時,可能不會減少從而不能完全覆蓋。

Features and polynomial regression

可以使用自定義的 features 而不是完全照搬已存在的 features。比如房子有長寬兩個屬性,我們可以創建一個新屬性--面積。然後,表達式變成

技術分享,但是這個曲線是先減小後增大的,與實際數據不符(面積越大,總價越高)。所以調整為

技術分享

Normal equation

Gradient Descent 隨著循環次數增加,逐步逼近最小值。如圖:

技術分享

Normal equation 是通過方法直接計算出 θ。

導數為 0 時最小

技術分享

然後解出 θ0 到 θn

求解 θ 的方程

技術分享

Matrix 概念見 Machine Learning - week 1

什麽時候用 Gradient Descent 或者 Normal Equation

技術分享

當 n 較大時,右邊的會很慢,因為計算 技術分享 是 O(n3)

當 n 小的時候,右邊會更快,因為它是直接得出結果,不需要 iterations 或者 feature scaling。

如果 技術分享 是 non-invertible?

1. Redundant features (are not linearly independent).

E.g. x1 = size in feet2; x2 = size in m2

2. Too many features(e.g. m <= n)

比如 m = 10, n = 100,意思是你只有 10 個數據,但有 100 個 features,顯然,數據不足以覆蓋所有的 features。

可以刪除一些 features(只保留與數據相關的 features)或者使用 regularization。

習題

1. 技術分享

不知道如何同時使用兩種方法,這兩種方法是否是順序相關的?

使用 Dividing by the range

range = max - min = 8836 - 4761 = 4075

vector / range 後變為

1.9438
1.2721
2.1683
1.1683

對上述使用 mean normalization

avg = 1.6382

range = 2.1683 - 1.1683 = 1

x2(4) = (1.1683 - 1.6382) / 1 = -0.46990 保留兩位小數為 -0.47

5. 技術分享

上面提到了“Features 的範圍越小,總的可能性就越小,計算速度就能加快。”(多選題也可以單選)

Machine Learning - week 2 - Multivariate Linear Regression