ITC NCUE
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: 線性迴歸 tags: [machine learning,regression] --- 線性迴歸(Linear Regression) === ## Table of Contents [TOC] 線性迴歸(Linear Regression) --- 迴歸(Regression)是預測**連續型**的目標變數(Y),線性迴歸是假設目標變數(Y)與特徵(X)是**線性**的關係,公式如下: $$ \hat{y} = w \cdot \text{x} + b $$ 其中: - $w$:斜率(slope) - $b$:截距(bias/intercept) - $x$:可以是1至多個特徵 - $y$:目標變數 ### [簡單線性迴歸(Simple Linear Regression)以及目標函數](https://hackmd.io/@gdgncue/HJfs6CjVWe) ### [多元線性迴歸(Multiple Linear Regression)](https://hackmd.io/@gdgncue/Bylw71nN-x) ### [多項式回歸(Polynomial Regression)](https://hackmd.io/@gdgncue/B1gUr12EZe) ### [梯度下降迴歸法(Stochastic Gradient Descent Regressor,SGDRegressor)](https://hackmd.io/@gdgncue/Sy-QB53Vbl) ### [L1 正則化(Lasso Regression)](https://hackmd.io/@gdgncue/SkrbzDAVZg) ### [L2 正則化(Ridge Regression)](https://hackmd.io/@gdgncue/BkodSi1SZg) ### L3、L4、L5...? <iframe width="560" height="315" src="https://www.youtube.com/embed/Cm6rX7E13So?si=YrrOSarJOSATt1K7" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> ### 彈性網路迴歸(Elastic Net Regression) <iframe width="560" height="315" src="https://www.youtube.com/embed/t6E0Dt5rg9U?si=66ezGAs0BRHEB0pW" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> ### Fused lasso <iframe width="560" height="315" src="https://www.youtube.com/embed/OeGToydVHz8?si=h6tR2VdbGVHcusN1" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> ### Bayesian Linear Regression <iframe width="560" height="315" src="https://www.youtube.com/embed/Z6HGJMUakmc?si=OrOw6DD1at8-2taE" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> 迴歸假設與缺點 --- 回歸假設$X$是「獨立同分配」(Independent and identically distributed,或稱獨立同分配,縮寫為iid、 i.i.d.、IID),意思是我們希望**誤差**: * 互相沒有關聯(獨立):A 的誤差不會影響 B 的誤差(如果誤差彼此有關,代表你有「沒放進模型的重要資訊」。) * 都來自同一個機制(同分布):誤差不是有時很大、有時很小,而是波動程度都差不多(反之則表現不均勻:你預測某些區段很好、某些區段很差 ) ### 特徵間彼此要獨立嗎? 理想是 | 關係 | 目標 | 原因 | | ------------------------ | ---------- | --------------- | | **特徵 vs 預測值(y)** | ✅ 要有「強關聯」 | 代表這個特徵對預測是「有用的」 | | **特徵 vs 特徵之間(x₁ vs x₂)** | ❌ 不要「高度相關」 | 避免重複資訊、共線性問題 | **但是如果你只在乎預測準確率:共線性影響不大!** | 情況 | 處理方式 | | ------ | ------------------ | | 只在意預測 | 不一定要處理,共線性不嚴重時影響不大 | | 需要解釋模型 | 一定要處理,否則係數和顯著性不可信 | :::success 所以**多項式迴歸就是一種結構性共線性(structural multicollinearity),但我們還是可以使用它,是因為有時在機器學習中,我們更在乎整體模型泛化效果。** ::: #### 多重共線性(Multicollinearity) 當回歸模型中的自變數彼此之間有高度相關性時,就產生了多重共線性。 * 回歸的理想情況:每個特徵都獨立對應目標變數 * 現實共線性情況:特徵 A 和特徵 B 幾乎同時變動,難以分清各自對 y 的貢獻 :::success 例如我們使用OLS去做**偏微分**的時候,我們就是為了求不同特徵的$w$去做的(「只調一個參數、觀察 SSE 改變」就是偏微分概念,我想知道$w_1$增加一點點時,誤差改變多少,$w_2$不變),而$w$也可以估計每個變數對目標的獨立貢獻(像權重一樣)。 ::: 如果 $x_1$ 跟 $x_2$ 高度相關,那就很難假設$x_1$單獨改變而$x_2$不變 → 模型無法清楚分辨誰在影響 y → 難以分辨到底是調整$w_1$還是$w_2$對 SSE 有貢獻 #### 會造成哪些問題? 注意:這些問題只影響「高度相關的變數」,不是全部變數都會出問題。 1. 係數不穩定: 加或刪除一個變數,其他變數的係數變很多(甚至符號改變),模型敏感到不敢信 → 模型解釋性降低 2. p 值不可信: 共線性會讓標準誤放大,p 值變大 → 有影響的變數看起來像「不顯著」,模型評估、特徵選擇都受到干擾 #### 怎麼偵測多重共線性? * 相關矩陣:看特徵間**皮爾森相關係數(Pearson Correlation Coefficient)是否過高(如 > 0.8,可調整)** * 皮爾森相關係數(Pearson Correlation Coefficient),就是共變異數(Covariance)的標準化版本(把單位都換成標準差,避免單位不同的影響)。 $$ r = \frac{\mathrm{Cov}(X, Y)}{\sigma_X \cdot \sigma_Y} = \frac{ \sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y}) }{\sqrt{ \sum (x_i - \bar{x})^2 } \cdot \sqrt{ \sum (y_i - \bar{y})^2 }} $$ * $\mathrm{Cov}(X, Y)$:X 與 Y 的共變異數 * $\sigma_X$:X 的標準差 * $\sigma_Y$:Y 的標準差 * $VIF$(變異膨脹因子):一個變數的 $\mathrm{VIF_j} = 1 / (1 - R_j^{2})$ $R_j^2$ 是在計算 $VIF$ 時的核心概念,它的意思是: 「把第 $j$ 個變數 $x_j$ 當成目標(y),然後用其餘所有變數(不是 y,而是其他 x)來做線性回歸,計算出來的決定係數 $R^2$。」 這就是 $R_j^2$。 :::info 一般來說 * VIF 等於 1 = 變數不相關 * VIF 介於 1 到 5 之間 = 變數中等相關 * VIF 大於 5 = 變數高度相關 ::: #### 資料來源 [多重共線性](https://statisticsbyjim.com/regression/multicollinearity-in-regression-analysis/) [Variance Inflation Factor (VIF)](https://www.investopedia.com/terms/v/variance-inflation-factor.asp) [p值](https://chih-sheng-huang821.medium.com/%E7%B5%B1%E8%A8%88%E5%AD%B8-%E5%A4%A7%E5%AE%B6%E9%83%BD%E5%96%9C%E6%AD%A1%E5%95%8F%E7%9A%84%E7%B3%BB%E5%88%97-p%E5%80%BC%E6%98%AF%E4%BB%80%E9%BA%BC-2c03dbe8fddf) ### 受離群值影響很大 另一個缺點是迴歸非常容易受到離群值的影響,離群值可能會造成模型嚴重偏移 #### scikit程式碼範例:測試離群值對迴歸的影響 生成隨機資料 ```python= from sklearn.datasets import make_regression X, y = make_regression(n_samples=20, n_features=1, noise=50) ``` 繪圖檢查 ```python= from matplotlib import pyplot as plt fig = plt.figure(figsize=(8,8)) plt.scatter(X, y, color='blue', alpha=0.5) plt.title('test data') ``` ![離群值原資料](https://hackmd.io/_uploads/BJS4yAVBlg.png) 模型訓練 ```python= from sklearn.linear_model import LinearRegression lr = LinearRegression() lr.fit(X, y) print(lr.coef_, lr.intercept_) ``` 製造離群值 ```python= # 製造離群值 y[0] += 2000 ``` 模型二次訓練 ```python= from sklearn.linear_model import LinearRegression lr2 = LinearRegression() lr2.fit(X, y) print(lr2.coef_, lr2.intercept_) ``` 二次繪圖檢查 ```python= from matplotlib import pyplot as plt import numpy as np fig = plt.figure(figsize=(8,8)) plt.scatter(X, y, color='blue', alpha=0.5) line_X = np.array([-3, 3]) plt.plot(line_X, line_X*lr.coef_+lr.intercept_, c='green', label='Original Regression') plt.plot(line_X, line_X*lr2.coef_+lr2.intercept_, c='red', label='New Regression') plt.title('test data') plt.legend(); ``` ![離群值迴歸比較](https://hackmd.io/_uploads/HkhpxA4rel.png) 可以看見一個離群值就會造成很大影響 時間序列分析(Time Series Analyse) --- 時間序列(Time series)是依時間順序產生的一組資料,分析的目的包括掌握季節/月份/星期的波動效應、趨勢,進而預測未來。 * 當外在因素複雜時,仍然可以依據前期資料預測下一期 * 不符合iid假設時,時間序列仍可以依據前期資料建構模型 :::info 某個時間點的數值會受到前面幾個時間點的影響,表示資料之間不是獨立的。 不一定同分布,可能有趨勢/季節性變動 ::: 分析時間序列有很多演算法,這邊指講解迴歸相關部分 #### 拿簡單線性回歸做測試 隨機產生300筆具有週期性和上升趨勢的資料 ```python= import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error # 資料產生 np.random.seed(42) n = 300 date_range = pd.date_range(start='2020-01-01', periods=n, freq='D') noise = np.random.normal(loc=0, scale=1, size=n) seasonality = 2 * np.sin(np.linspace(0, 4 * np.pi, n)) trend = np.linspace(0, 10, n) # 線性上升趨勢 combined_series = seasonality + trend + noise ts_with_trend_and_seasonality = pd.Series(combined_series, index=date_range) ``` 繪圖檢查 ```python= # 繪圖檢查 ts_with_trend_and_seasonality.plot(title='Seasonality + trend + Noise Time Series', figsize=(10, 4)) plt.xlabel('Date') plt.ylabel('Value') plt.grid(True) plt.tight_layout() plt.show() ``` ![時間序列資料點](https://hackmd.io/_uploads/Hy5mvnOSel.png) 嘗試使用簡單性性迴歸預測 ```python= # 準備資料(x 為時間索引整數, y 為原始值) x = np.arange(n).reshape(-1, 1) # sklearn 需要 2D array y = ts_with_trend_and_seasonality.values # 線性回歸模型 model = LinearRegression() model.fit(x, y) y_pred = model.predict(x) # 計算 MSE residuals = y - y_pred mse = mean_squared_error(y, y_pred) print("MSE=",mse) ``` 繪圖檢查(預測線和殘差圖) ```python= # 畫圖(兩個子圖) fig, axs = plt.subplots(2, 1, figsize=(12, 8), sharex=True, gridspec_kw={'height_ratios': [3, 1]}) # 主圖:時間序列與回歸線 axs[0].plot(date_range, y, label='Original Data') axs[0].plot(date_range, y_pred, color='red', label=f'Regression Line\ny = {model.coef_[0]:.2f}x + {model.intercept_:.2f}') axs[0].set_title(f'Scikit-Learn Linear Regression (MSE = {mse:.2f})') axs[0].set_ylabel('Value') axs[0].legend() axs[0].grid(True) # 殘差圖 axs[1].plot(date_range, residuals, color='gray') axs[1].axhline(0, color='red', linestyle='--', linewidth=1) axs[1].set_title('Residuals Over Time') axs[1].set_xlabel('Date') axs[1].set_ylabel('Residual') axs[1].grid(True) plt.tight_layout() plt.show() ``` ![時間序列資料點預測結果](https://hackmd.io/_uploads/H1FxO3uBxx.png) 會看到實際上**簡單線性回歸只抓到成長趨勢,而沒有捕抓到周期性變化** 在上面的例子裡,我們使用的X實際上是天數(時間本身,0~299),**可解釋整體線性上升趨勢,卻無法捕抓週期性,在沒有其他特徵的輔助下**,我們要開始介紹另一種迴歸模型:**自我迴歸模型(Autoregressive model,AR)** ### 自我迴歸模型(Autoregressive model,AR) AR(自迴歸)模型的本質,就是一種特別形式的線性回歸,只是它的特徵(X)是「目標變數自己的過去值」,也就是過去的Y去預測現在的Y。 $$ y_t = \phi_1 y_{t-1} + \phi_2 y_{t-2} + \cdots + \phi_p y_{t-p} + \varepsilon_t $$ 1. 應該選用多久之前呢? 2. 如何分析哪些時間段應該放入特徵? 要回答這些問題,就會需要**LAG(滯後)、PACF(偏自相關函數)與 ACF(自相關函數)** #### Lag(滯後) Lag 是指資料中「相對於現在時間點的過去某個時間點的距離」,在接下來的ACF、PACF中會有相關設定。 | 時間(t) | 價格 yₜ | Lag=1 (yₜ₋₁) | Lag=2 (yₜ₋₂) | | ----- | ----- | ------------ | ------------ | | t=1 | 10 | 無資料 | 無資料 | | t=2 | 12 | 10 | 無資料 | | t=3 | 11 | 12 | 10 | | t=4 | 13 | 11 | 12 | :::info 常見決定lag的方法有 * $lag\approx \sqrt{n}$ * ACF / PACF 圖判讀 * 模型評分(AIC/BIC) * 資料週期性與預測目標(每週、月、季重複) ::: #### ACF(Autocorrelation Function,自相關函數) ACF 是 $y_t$ 和 $y_{t-k}$ 的皮爾森相關係數(Pearson correlation) $$ \text{ACF}(k) = \frac{\operatorname{Cov}(y_t, y_{t-k})}{\sqrt{\operatorname{Var}(y_t)\operatorname{Var}(y_{t-k})}} $$ ACF 中的 $\operatorname{Cov}(y_t, y_{t-k})$ 沒有控制其他 lag 的影響,所以它會把「$y_{t-k}$經過$y_{t-k+1}$...影響 $y_{t}$」的路徑也計算進去,**也就是除了$y_{t-k}$的包含直接影響外,也會包含間接影響**,例如: 其中一種可能就是「T−2 → T−1 → T」,這就是**T−2 與 T 的間接關係** 假設: $$ y_t = 0.9 * y_{t-1} + \varepsilon_t $$ 這是一個 AR(1) 模型,代表: * $y_t$ 僅僅由 $y_{t-1}$ 直接影響 * $y_{t-2}$ 對 $y_t$ **沒有直接影響** 但是,因為: $$ y_{t-1} = 0.9 * y_{t-2} + \varepsilon_t $$ (根據同樣的 AR(1) 遞推) 所以: $$ y_t \approx 0.9^2 * y_{t-2} + \dots $$ 換句話說: **$y_{t-2}$ 雖然沒有直接影響 $y_t$,但透過 $y_{t-1}$ 的傳遞,還是對$y_t$有關聯!** 這就是**間接關聯**:透過中介的變數$y_{t-1}$形成的相關 #### PACF(Partial Autocorrelation Function,偏自相關函數) PACF(k) 是在已經控制住 $y_{t-1}, y_{t-2}, ..., y_{t-k+1}$ 的情況下,$y_t$ 和 $y_{t-k}$ 的**直接相關性**。 :::success PACF跟AR基本上一樣,但PACF不是為了實際預測,而是為了看$\phi_{k}$是不是顯著,來判斷「是否需要包含 lag=k」 ::: 建構一個線性模型: $$ y_t = \phi_1 y_{t-1} + \phi_2 y_{t-2} + \dots + \phi_k y_{t-k} + \varepsilon_t $$ **PACF(k)** 就是這個迴歸中: **$y_{t-k}$ 的迴歸係數 $\phi_k$** 這表示:它衡量的是 $y_{t-k}$ 對 $y_t$ 的**直接貢獻** ```mermaid flowchart LR A["T-2 (yₜ₋₂)"] --> B["T-1 (yₜ₋₁)"] B --> C["T (yₜ)"] A --- C subgraph ACF direction TB ACFT2["corr(yₜ, yₜ₋₂) 包含 A→C 直接 + A→B→C 間接"] end subgraph PACF direction TB PACFT2["回歸:yₜ = α yₜ₋₁ + β yₜ₋₂ + ..."] PACFcoef["PACF(2) = β"] end C -->|觸發 ACF 計算| ACFT2 C -->|排除中介,只保留直接| PACFT2 PACFT2 --> PACFcoef ``` #### numpy程式碼 產生資料 ```python= import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error # 資料產生 np.random.seed(42) n = 300 date_range = pd.date_range(start='2020-01-01', periods=n, freq='D') noise = np.random.normal(loc=0, scale=1, size=n) seasonality = 2 * np.sin(np.linspace(0, 4 * np.pi, n)) trend = np.linspace(0, 10, n) # 線性上升趨勢 combined_series = seasonality + trend + noise ts_with_trend_and_seasonality = pd.Series(combined_series, index=date_range) x = np.arange(n).reshape(-1, 1) # sklearn 需要 2D array y = ts_with_trend_and_seasonality.values ``` 定義ACF、PACF,使用Lag=20 ```python= #ACF 計算 def calc_acf(y, lag): y = np.array(y) return np.corrcoef(y[lag:], y[:-lag])[0, 1] acfs = [calc_acf(y, lag) for lag in range(1, 21)] #PACF 計算(使用殘差回歸) def calc_pacf(y, lag): """ 計算第 lag 階的 PACF(使用多元線性回歸,控制 y_{t-1}...y_{t-lag+1}) """ df = pd.DataFrame({'y_t': y[lag:]}) for i in range(1, lag + 1): df[f'lag_{i}'] = y[lag - i: -i] X = df[[f'lag_{i}' for i in range(1, lag + 1)]].values y_target = df['y_t'].values model = LinearRegression().fit(X, y_target) return model.coef_[-1] # PACF(k) = φ_k (最後一個係數) pacfs = [calc_pacf(y, lag) for lag in range(1, 21)] ``` 定義AR模型 ```python= def fit_ar_model(y, k): df = pd.DataFrame({'y_t': y[k:]}) for i in range(1, k + 1): df[f'lag_{i}'] = y[k - i: -i] X = df[[f'lag_{i}' for i in range(1, k + 1)]].values y_target = df['y_t'].values model = LinearRegression().fit(X, y_target) y_pred = model.predict(X) residuals = y_target - y_pred return y_pred, residuals, model ``` 訓練+繪圖 ```python= # 訓練 AR(5) k = 5 y_pred_ar, residuals_ar, ar_model = fit_ar_model(y, k) y_target = y[k:] # 繪圖 fig, axs = plt.subplots(2, 2, figsize=(14, 8)) # 原始資料與 AR 預測 axs[0, 0].plot(date_range[k:], y_target, label='Original') axs[0, 0].plot(date_range[k:], y_pred_ar, label=f'AR({k}) Prediction', color='red') axs[0, 0].set_title(f'AR({k}) Prediction vs Actual') axs[0, 0].legend() axs[0, 0].grid(True) # 殘差圖 axs[1, 0].plot(date_range[k:], residuals_ar, color='gray') axs[1, 0].axhline(0, color='red', linestyle='--') axs[1, 0].set_title('Residuals') # ACF axs[0, 1].stem(range(1, 21), acfs) axs[0, 1].set_title('ACF (manual with NumPy)') axs[0, 1].set_xlabel('Lag') axs[0, 1].set_ylabel('Correlation') axs[0, 1].grid(True) # PACF axs[1, 1].stem(range(1, 21), pacfs) axs[1, 1].set_title('PACF (manual with NumPy + regression)') axs[1, 1].set_xlabel('Lag') axs[1, 1].set_ylabel('Partial Corr') axs[1, 1].grid(True) plt.tight_layout() plt.show() ``` ![自迴歸訓練](https://hackmd.io/_uploads/rkVsbf5Sll.png) :::info 一般決定LAG是否具有影響力是看**信賴區間( Confidence Interval,CI)**,一般模組庫繪圖時會附上 [Confidence Interval](https://www.geeksforgeeks.org/dsa/confidence-interval/) ::: :::success AR只是最基本的時間序列模型,後續還有MA、ARIMA、SARIMAX等模型,可以參看下面的yt播放清單得到更詳細內容,以及透過colab範例實作。 [Time Series Analysis](https://youtube.com/playlist?list=PLvcbYUQ5t0UHOLnBzl46_Q6QKtFgfMGc3&si=_vZbc-jhDDbJTOzb) [colab 實作](https://colab.research.google.com/drive/14OXRkk1tlUJZDFiQizr4TA6V7nT0XTWo?usp=sharing) ::: 時間序列分析可以修正迴歸的缺點,也適用於特徵變數太多,無法掌握的情形。 過度擬合(Overfitting)與正則化(Regulation) --- 建構模型時,訓練資料預測的準確率極高,但測試時準確率偏低,稱為過度擬合(Overfitting),原因是若**原始資料包含過多特徵或訓練過度,導致模型非常複雜,新資料若有些微偏差,可能會有預測錯誤**。 ![under_over_fitting](https://hackmd.io/_uploads/rkUSf2nHxl.png) * 低度擬合(Under-fitting),模型過於簡單,錯誤分類很多,準確率低。 * 過度擬合(Overfitting),模型很複雜,訓練資料分得很準,但只要測試資料稍有差異就會出錯。 矯正過度擬合的方法常見的有兩種 * 正則化(Regularization):在目標函數上加上懲罰項,避免權重過大的特徵主在整個模型,以致其他特徵被忽視,影響模型效能。 * DropOut:常用於神經網路,訓練過程中拋棄一定比例的神經元,使權重過大的特徵被忽視,進而找到最佳解 ![DropOut](https://hackmd.io/_uploads/Hyfa433Hee.png) #### 正則化(Regularization) 針對迴歸,常見三種Regularization 1. Lasso迴歸:採用L1懲罰項,為權重絕對值總合,目標函數為:$Loss=MSE+\lambda\sum{|W|}$ 2. Ridge迴歸:採用L2懲罰項,為權重平方合,目標函數為:$Loss=MSE+\lambda\sum{W^{2}}$ 3. Elastic net regularization:同時採用L1、L2,目標函數為$Loss=MSE+\lambda{1}\sum{|W|}+\lambda{2}\sum{W^{2}}$ #### L1、L2差異 L2為權重平方合,L1為權重絕對值合,L2強度較大 L1會造成部分權重歸0(Zeros out),進而使特徵失效、簡化模型,L2只會使權重接近0,但部會等於0。 偏差(Bias)與變異(Variance) --- 這裡的偏差(Bias)不是指迴歸的截距,變異(Variance)也不是指機率分配的變異數,而是說明預測的準確度與穩定度。低偏差(Low Bias)表示準確度高,低變異(Low Variance)表示穩定性高,多次預測不會差異很大。 ![bias-and-variance_orig](https://hackmd.io/_uploads/Byb4RoxPxg.png) ![Bias-Variance-2](https://hackmd.io/_uploads/BkTLCjgPxl.png) * 低偏差低變異(Low Bias,Low Variance):準確高且穩定,建構模型時追求的目標 * 高偏差高變異(High Bias,High Variance):準確低且不穩定,最不想看到的情況 * 低偏差高變異(Low Bias,High Variance):準確高但不穩定,通常是因為模型過於複雜、過度擬合 * 高偏差低變異(High Bias,Low Variance):準確低但穩定,多次預測差異不大,模型低度擬合 低偏差與低變異是衝突的,加入更多的特徵或增加訓練週期,可使模型準確率提高、降低偏差,但預測變異會變大,反之,使用特徵選取或萃取,減少特徵或減少訓練週期,可簡化模型,使模型變異降低,需要在偏差和變異間取得平衡。 正則化是防止過度擬合,降低不平衡資料的偏差手段。 ###### tags: `machine learning` `linear regression`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully