kirohi
    • 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
    ## NLP100本ノック ### 第6章 機械学習 - ロジスティック回帰って何? - 目的は分類すること - 2値の分類をしたい場合に使う(範囲は0から1) - 線形分類なので,いい感じに分けれる直線(二次元なら)を見つけて分類したいってことですかね - 0~1の確率で分類する - ロジスティック回帰の数式の意味が未だちゃんと分かっていません - 僕もわかりません. - 参考Ref: [ロジスティック回帰分析の例や説明変数を解説!](https://ai-trend.jp/basic-study/regression/logistic_regression/) - ロジスティック回帰モデルの学習方法 - fitメソッドに渡すデータの形式がわからないと難しい (以下の部分) - 疎行列と密行列なるものがあるらしい - ただ,sklearnはどちらでも受け取ってくれる. ```python #print(type(X_train)) => ベクトル化された特徴量データ #print(type(Y_train)) => 正解ラベル lr = LogisticRegression() # ロジスティック回帰モデルのインスタンスを作成 lr.fit(X_train, Y_train) # ロジスティック回帰モデルの重みを学習 ``` - 疎行列と蜜行列とはなんですか? - 疎行列は,0を省いた行列 - 蜜行列は,0を含めた行列(甘そう,美味しそう) - vectorizerでベクトル化するときに,何千次元とかできても,1つの文で使われるのは数個しかなくて,基本0ばかりなので無駄にデータが増えます.今回のパターンだとトレーニングデータの特徴量をテキストファイルで保存すると3GBくらい.なので0を省いた行列を使った方がおトク - 疎行列(sparse matrix) <img src="https://i.imgur.com/BKsWbZF.png" width="300"> - 蜜行列 <img src="https://i.imgur.com/xKjlXg6.png" width="500"> - fitとtransformの関係性 - fit - Vectorizer - fit()は使われている語句すべてを登録する. - データを変換するために必要な統計情報を計算する - ベクトル化するためのルールを構築する - 全部のデータを渡さなくてOK - あくまで訓練データで作成する - モデルの構築は訓練データで閉じているべき - transform - transform()で引数として渡されたデータを,fit()で得られた情報を用いてベクトル化する. - Vectorizer - fitの結果を用いて,実際にデータを変換する(ベクトル化する) - fit_transform - CountVectorizer - fitとtransformをまとめて行う - CountVectorizerは引数一つで渡せる => なんで? - やってることはfitしてtransorm - 正解率の出し方が分からん - コード的な話?概念的な話? - 両方です 1. 作ったロジスティック回帰のモデルでテストデータで予測をかける 2. ```y_pred = lr.predict(x_test)``` 3. 予測結果(y_pred)と正解データ(y_test)の一致を一つ一つ確かめて正解数をカウントする 4. カウントした正解数をデータ総数で割ると正解率が得られる - 正解率 = Accuracy - 真の正解に対してシステムが正解と返したものの数: TP - 真の正解に対してシステムが不正解と返してしまったものの数:FN - 真の不正解に対してシステムが正解と返してしまったものの数:FP - 真の不正解に対してシステムが不正解と返したものの数:TN - [sklearn.metrics.accuracy_score](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html) $$ Accuracy = \frac{TP + TN}{TP + FN + FP + TN} $$ ```python from sklearn.metrics import confusion_matrix y_predict = clf.predict(X_test) tn, fp, fn, tp = confusion_matrix(y_true, y_predict).ravel() accuracy = (tp + tn) / (tn + fp + fn + tp) ``` $$ Macro-Precision = \frac{1}{2} (\frac{TP}{TP + FP} + \frac{FN}{FN + TN}) $$ $$ Micro-Precision = \frac{TP + TN}{TP+FP+FN+TN} $$ $$ Macro-Recall = \frac{1}{2} (\frac{TP}{TP + FN} + \frac{TN}{FP + TN}) $$ $$ Micro-Recall = \frac{TP + TN}{TP+FN+FP+TN} $$ | | ガン | ガンじゃない | 予測の合計| | -------- | -------- | -------- |-------- | | ガンだと予測 | 8 | 90 | 98 | ガンじゃないと予測 | 2 | 900 | 902 |結果の合計|10|990| - 適合率 - Ref: [【入門者向け】機械学習の分類問題評価指標解説(正解率・適合率・再現率など)](https://qiita.com/FukuharaYohei/items/be89a99c53586fa4e2e4) - 混同行列(Confusion Matrix)ってなんですか? - クラス分類問題の結果を「実際のクラス」と「予測したクラス」を軸にしてまとめたもの。(https://note.nkmk.me/python-sklearn-confusion-matrix-score/) - 適合率,再現率,F1スコアってそもそもなんですか? - 適合率:Aだと予測したもののうち,本当にAだったものの確率.偽陽性を低く抑えたい時に重視する.`Precision = 8/(8+90) = 0.082` - 再現率:見つけるべきもののうち、正しく見つけることができたものの割合。Sensitivity(敏感度)ともいう。偽陰性を低く抑えたい時に見る。`Recall = 8/(8+2) = 0.80` - F1スコア: https://qiita.com/niship2/items/903588692d5764cdbf17 評価指標など参考 https://www.haya-programming.com/entry/2018/03/14/112454 - マイクロ平均とは? - N セットのテストをする.iセット目のテストはni回のテストで構成されるとする. n=∑ni回のテストを合計してから,評価値を計算するのがマイクロ平均. - 各クラスの適合率or再現率を算出せずに,混合行列から求める - 使い所,意味は? - マクロ平均とは? - 各クラスごとの適合率or再現率を算出してから平均を取る方法 - 使い所 - クラスの件数に偏りがあり,そういったクラスの指標の良し悪しを全体的な指標として反映させたいときに使うと良い. ## コードサンプル ```python a = 200 lr.predict(y_test) ``` numpy.argsort() 0 1 2 3 4 [2,6,7,4,6]->[0,3,1,4,2] [2,4,6,6,7] ## 正規化(Normalization) - ## 正則化(Regularization) - 目的: 過学習の回避 - 予測関数: $$ f(x) = w_1x_1 + w_2x_2 + ... + w_nx_n = wx $$ - L1正則化 - ラッソ回帰 - 誤差関数(L1) $$ Loss = \sum |y_i - f(x_i)|^2 + ||w||_{l=1} = \sum |y_i - f(x_i)|^2 +C\sum|w_i| $$ - L2正則化 - リッジ回帰 - 誤差関数(L2) $$ Loss = \sum |y_i - f(x_i)|^2 + ||w||_{l=2} = \sum |y_i - f(x_i)|^2 +C\sum|w_i|^2 $$ 参考にした http://ailaby.com/logistic_reg/#id3 Ref: https://research.miidas.jp/2019/01/%E3%81%AD%E3%81%87python%E3%80%81%E6%AD%A3%E5%89%87%E5%8C%96%E3%81%A3%E3%81%A6%E4%BD%95%EF%BC%9Fpart-1/

    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