Ella
  • NEW!
    NEW!  Connect Ideas Across Notes
    Save time and share insights. With Paragraph Citation, you can quote others’ work with source info built in. If someone cites your note, you’ll see a card showing where it’s used—bringing notes closer together.
    Got it
      • Create new note
      • Create a note from template
        • 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
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Write
          • Only me
          • Signed-in users
          • Everyone
          Only me 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 No publishing access yet

        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.

        Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Explore these features while you wait
        Complete general settings
        Bookmark and like published notes
        Write a few more notes
        Complete general settings
        Write a few more notes
        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
      • Save as template
      • 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 Create Help
    Create Create new note Create a note from template
    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
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    # Linear Models (線性模型) 在**監督式機器學習**中,我們常常會用一條線(或一個平面)來預測資料,這就是所謂的「**線性模型**」。 ### **Linear Regression** 是一種用「直線」來預測數值的方法 當我們有許多資料(藍點),我們希望在這群資料中找到一條直線(紅線),用來說明這群資料(找到他們的規律),用這條線代表預測的值。 ![image](https://hackmd.io/_uploads/B1DYeO6Mex.png) **要怎麼找出這條線?** 假設這條紅線的方程式是 $y = ax+b$ (最簡單的線,只有一個變量) 只要得到這條線的係數 a(斜率)與 b(截距)就可以畫出這條線。而這不是一條隨便畫的線,我們希望這條線能**盡量貼近所有的資料點**(藍點),這個點我們用 $(x_i, y_i)$ 來表示。 > 現在我們有了目標:**找出這條線**,並且這條線是**唯一**最能代表這群資料的線 > > 這其實就是機器學習中的經典概念: > 定義一個**目標函數(loss function)**,然後找出能最小化它的參數組合。 **具體該怎麼做呢?** 透過 OLS「最小平方法」找出這條線! 讓所有資料點與預測線之間的距離(垂直誤差)平方總和越小越好。 讓每個點 $(x_i, y_i)$ 的預測值 $\hat{y}_i = ax_i + b$ 越接近實際值 $y_i$ <img src="https://hackmd.io/_uploads/HyHCL_Tfex.png" width="300"> ## Ordinary Least Squares(最小平方法) 這條線找出來的方式在數學上叫「最小平方法」(Ordinary Least Squares) OLS 是一個經典的線性回歸找出係數的方法,是 Scikit-learn 的LinearRegression() 預設採用的解法 回顧一下我們的目標: > 找出唯一一條「**最能代表資料的直線**」: > > 找到一組係數 $a$(斜率)與 $b$(截距), > 讓所有點與這條線之間的 **垂直距離平方總和最小**。 #### 怎麼做呢?— Ordinary Least Squares 的步驟 **1️⃣ 定義資料** 假設我們有 $n$ 筆資料,每筆資料是 $(x_i, y_i)$ **2️⃣ 定義預測函數** 我們假設資料是根據以下模型產生的: $\hat{y}_i = ax_i + b$ 其中: * $\hat{y}_i$ 是預測值 * $y_i$ 是實際觀察值 **3️⃣ 定義損失函數(Loss Function)** 定義我們想要最小化的誤差平方總和(SSE, Sum of Squared Errors): $$ \text{SSE}(a, b) = \sum_{i=1}^n (y_i - \hat{y}_i)^2 = \sum_{i=1}^n (y_i - (ax_i + b))^2 $$ > **SSE 是什麼?** > 在簡單線性回歸中,我們用模型 $\hat{y}_i = a x_i + b$ 來預測每個資料點的目標值 $y_i$。 > 實際值 $y_i$ 與預測值 $\hat{y}_i$ 之間的差距稱為「**殘差(residual)**」,也就是誤差(error): > > $$ > \text{殘差(residual 或 error)} = y_i - \hat{y}_i > $$ > > SSE(Sum of Squared Errors,誤差平方和)則是把每個殘差的平方加總起來: > > $$ > \text{SSE} = \sum_{i=1}^n (y_i - \hat{y}_i)^2 > $$ > > 這個指標量化了模型預測與實際資料的差距大小,且因為平方的關係,較大的誤差會被放大,使得 SSE 對異常點特別敏感。 > 我們的目標就是調整參數 $a, b$,使 SSE 最小化,也就是讓模型預測值與真實值的誤差平方和盡可能小。 而 **誤差平方總和** 就是這些殘差的平方總和: $$ \text{SSE} = \sum_{i=1}^n (y_i - \hat{y}_i)^2 = \sum_{i=1}^n (y_i - (ax_i + b))^2 $$ #### 為什麼要平方?為什麼是總和? * ✅ **平方**:讓正負誤差不會互相抵消,同時加重「大誤差」的懲罰。 * ✅ **總和**:整體來看全部點的表現,而不只是單點好壞。 > 線性回歸的目標就是: > 找出一組 $a$、$b$,使得 RSS 最小 > $\Rightarrow$ 最小化這條線與所有資料點的**垂直距離平方總和** > 這就叫作 **最小平方法(Ordinary Least Squares, OLS)**。 紅線是預測線 藍點是實際資料點 垂直虛線就是 $y_i - \hat{y}_i$,也就是 **誤差** 這些垂直虛線的「平方總和」越小,紅線就越好! **4️⃣ 求解** 將上式對 $a$ 與 $b$ 進行偏微分,令其為 0: $$ \frac{\partial \text{RSS}}{\partial a} = 0, \quad \frac{\partial \text{RSS}}{\partial b} = 0 $$ 這樣就能解出唯一解: $$ a = \frac{\sum(x_i - \bar{x})(y_i - \bar{y})}{\sum(x_i - \bar{x})^2}, \quad b = \bar{y} - a\bar{x} $$

    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
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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