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

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

    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
    # Nadaraya-Watson推定 と k近傍法(後編) この記事は[古川研究室 Workout calendar](https://qiita.com/flab5420/private/aee38a24be700d146817) 7日目の記事です. 本記事は古川研究室の学生が学習の一環として書いたものです. 一回ほとんど書いてた記事が全部消えて夜中に吠えました. 下書き保存ちゃんと押しましょう. # 前回のおさらい 前回の記事 [Nadaraya-Watson推定 と k近傍法(前編)](https://qiita.com/kpasso1015/items/50e62ca52b4a19bfb782)では 回帰の問題設定を述べ, その回帰を解く手法の一つとしてk近傍法を解説しました。 ## 回帰の問題設定 given: $D$次元ベクトルの観測データ$\mathbf{x} \in \mathbb R^D$とラベル$y \in \mathbb R$のペア $\{ ( \mathbf{x}_n,y_n )\} _{n=1}^N$ estimate: $y \simeq f(\mathbf{x})$となるような写像 $f$ ## k近傍法 真の関数(緑線)にノイズが加わった入力(青点)が得られている状況で, 4個の近傍のラベルの平均として得られた写像 $f$ (オレンジ線)が以下の図になります。 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154814/272d05ae-9647-fb55-a447-3ddeb8917ab6.png) 着目点(赤点線: $x=1.8$)とそこの近傍の範囲(赤線) ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154814/ab44fe7a-7076-0fc6-9c09-7ce559269ea3.png) k近傍法は重みや基底関数を使わずに写像を推定していますが, その推定した写像は**不連続**な写像になっており**微分ができません**. # Nadaraya-Watson推定 Nadaraya-Watson推定は回帰手法の一つです. k近傍では近傍に含むデータ点に等しい重みを付与して(平均)していますが, Nadaraya-Watson推定では着目点とデータの距離に応じて滑らかに減少する重みを与えます. Nadaraya-Watson重み付きカーネル平均はカーネル関数を使って以下の式で与えられます. $f(\mathbf{x}) = \frac{\sum_{i=1}^N K_\lambda(\mathbf{x},\mathbf{x}_i)y_i}{\sum_{j=1}^N K_\lambda(\mathbf{x},\mathbf{x}_j)}$ 今回 カーネル関数$K_\lambda(\mathbf{x},\mathbf{x_j})$は次式で与えます. $K_\lambda(\mathbf{x},\mathbf{x_j}) = \exp(\frac{-1}{2\sigma^2}||\mathbf{x} - \mathbf{x_j}||^2)$ この式はガウス関数の式と等価です. 他のカーネル関数としてはイパネクニコフ関数や短形3次関数が一般的です. 「カーネル関数, 」と続けますと[本一冊](https://www.amazon.co.jp/%E3%82%AB%E3%83%BC%E3%83%8D%E3%83%AB%E5%A4%9A%E5%A4%89%E9%87%8F%E8%A7%A3%E6%9E%90%E2%80%95%E9%9D%9E%E7%B7%9A%E5%BD%A2%E3%83%87%E3%83%BC%E3%82%BF%E8%A7%A3%E6%9E%90%E3%81%AE%E6%96%B0%E3%81%97%E3%81%84%E5%B1%95%E9%96%8B-%E3%82%B7%E3%83%AA%E3%83%BC%E3%82%BA%E7%A2%BA%E7%8E%87%E3%81%A8%E6%83%85%E5%A0%B1%E3%81%AE%E7%A7%91%E5%AD%A6-%E8%B5%A4%E7%A9%82-%E6%98%AD%E5%A4%AA%E9%83%8E/dp/4000069713)書くのにちょうどいい分量になるので省略し, ここではカーネル関数 $K_\lambda(\mathbf{a},\mathbf{b})$は$\mathbf{a}=\mathbf{b}$のとき最大値の1をとること, $\mathbf{a}$ と$\mathbf{b}$の近さを表す値になっているということだけを押さえましょう. ##実装 k近傍法と同様の状況です. ### 近傍半径: 0.2のとき ```python def NW(sigma=0.2): global H, R, Y Delta = x[:,None] - x_star[None,:] #データ点と任意のx_starを全ての距離をnumpyのBroad castで計算している Dist = np.square(Delta) H = np.exp(-0.5/sigma * Dist) #近傍半径が大きいほど、距離をあまり考慮しなくなるのがわかる G = np.sum(H, axis=0)[None,:] R = H/G check_sum = np.sum(R, axis=0) # print(check_sum) Y = R.T@t #(任意のx_starの数,データ数) x_star (データ数, ラベルの次元(今回は1)) sigma = 0.2 #近傍半径 NW(sigma) ``` #### カーネル関数: $f(\mathbf{x}) = K_\lambda(\mathbf{x},\mathbf{x}_i)$: 各データ点を中心とした近傍半径0.2のガウス関数 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154814/41eb32a4-d167-37b1-6dac-1edf2f001568.png) #### 重み付きカーネル関数: $f(\mathbf{x}) = \frac{K_\lambda(\mathbf{x},\mathbf{x}_i)}{\sum_{j=1}^N K_\lambda(\mathbf{x},\mathbf{x}_j)}$ : 任意の$x$において合計が1となるように正規化をします. 着目点(青点線 $x=4$)のときを見ていただければ, 総和が1になっていることがわかりやすいと思います. ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154814/2ad009b9-1019-9cf9-f0ea-2379c8bf6e87.png) #### 重み付きカーネル平均: $f(\mathbf{x}) = \frac{\sum_{i=1}^N K_\lambda(\mathbf{x},\mathbf{x}_i)y_i}{\sum_{j=1}^N K_\lambda(\mathbf{x},\mathbf{x}_j)}$ 先ほどのカーネル関数でそれぞれのラベルに重みをつけ, 総和をとったもの(総和が1なので重み付き平均)が推定した写像(オレンジ線)です. ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154814/d7de7db4-3487-fda6-5376-1ac9533d391e.png) k近傍法と同様にデータの**内分点**を通り, 得られた写像は滑らかな写像になっていることが確認できます. またデータの端点以降($x > -3.0, 3.3 < x$)の写像は端点のラベルに収束しています. ### 近傍半径: 2.0のとき 近傍半径を大きくした場合, 任意の$x$ではより遠くのデータの影響を受けやすくなります. ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154814/540b96db-782a-8ba7-5603-16b81d27aa36.png) ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154814/95af30b1-7a26-3535-0f0a-5aec7bf24378.png) 限りなく近傍半径を大きくした場合写像は単純にラベルの平均となります. #### 近傍半径: 10000.0のとき ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154814/6f920d1b-bad0-27c7-439d-533322fed78a.png) (そういえばk近傍法も$k=n$のときラベルの平均になったなあ....あれれ??) ### 近傍半径: 0.001のとき 近傍半径を小さくした場合, 任意の$x$で他のデータの影響を受けにくくなります. ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154814/adaccf75-da06-30e5-2dac-342116f85c05.png) カーネル関数はデルタ関数のようになります. ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154814/696a357e-9dd4-d95b-3456-8ef8579b768c.png) 任意の$x$では, ほぼ一つの点にしか重みがついてないことがわかります. ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154814/a5aaec12-39c5-f7b7-00ae-b2672ce93ba5.png) #### k近傍法(k=1) ここで近傍法との関わりを見ていきましょう. 近傍の数が1の時の結果は以下のようになります. ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/154814/6222c1b4-b6eb-1e70-37be-182ea46b1be4.png) 近傍半径の大きさを限りなく小さくした時と同じような結果になりました. k近傍法をあえてカーネル回帰として捉えるならば, 近傍が変わるまでの長さが一律ではないことから, カーネル幅がカーネル関数(データ)ごとによって変わるデジタル波形のようなカーネル関数をもつカーネル回帰といえることがわかります. ## 参考文献 Trevor Hastie・Robert Tibshirani・Jerome Friedman著, 杉山 将・井出 剛 ら監訳 [「統計的学習の基礎」](https://www.amazon.co.jp/%E7%B5%B1%E8%A8%88%E7%9A%84%E5%AD%A6%E7%BF%92%E3%81%AE%E5%9F%BA%E7%A4%8E-%E2%80%95%E3%83%87%E3%83%BC%E3%82%BF%E3%83%9E%E3%82%A4%E3%83%8B%E3%83%B3%E3%82%B0%E3%83%BB%E6%8E%A8%E8%AB%96%E3%83%BB%E4%BA%88%E6%B8%AC%E2%80%95-Trevor-Hastie/dp/432012362X/ref=sr_1_1?__mk_ja_JP=%E3%82%AB%E3%82%BF%E3%82%AB%E3%83%8A&keywords=%E7%B5%B1%E8%A8%88%E7%9A%84%E5%AD%A6%E7%BF%92%E3%81%AE%E5%9F%BA%E7%A4%8E&qid=1563430788&s=gateway&sr=8-1) $\S 6$ p.220~

    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