kezhang
    • 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
    # 多类别分类中Micro-averaging与Macro-averaging的区别 The difference between the Micro averaging evaluation metrics and the Macro averaging evaluation metrics in Multiclass classification ## 背景 >某位小朋友最近在复现一篇论文的实验时,遇到了一些关于Micro(微观)和Macro (宏观)评价指标之间的困惑,之前自己对于这些指标只是拿来主义,并没有理解与体会,随在这位小朋友的帮助下,加深了对于评价指标的理解,以此记录。 ## 评价指标: **Precision、Recall、F-score** >以一个二分类任务为例: 假设输入是$x$,其对应的真实标签(ground truth label)是$y$,而某二分类分类器预测$x$对应的标签是$y^{prediction}$。$\{y, y^{prediction}\}\in \{0,1\}$。 由此,可将此预测结果,根据真实标签$y$和分类器预测标签$y^{prediction}$的不同,划分为: >(1) 真正例(True Positive, TP):真实类别为正例,预测类别为正例; (2) 假正例(False Positive, FP):真实类别为负例,预测类别为正例; (3) 假负例(False Negative, FN):真实类别为正例,预测类别为负例; (4) 真负例(True Negative, TN):真实类别为负例,预测类别为负例。 此四类之间的关系,可由混淆矩阵(Confusion Matrix)表示: <table> <tr> <th rowspan="2">真实类别</th><th colspan="2">预测类别</th> </tr> <tr> <td >正例</td><td>反例</td> </tr> <tr> <td>正例</td><td>TP</td><td>FN</td> </tr> <tr> <td>反例</td><td>FP</td><td>TN</td> </tr> </table> 则: >* 查准率 *Precision* :*P* = $\frac{TP}{TP+FP}$ 反映出模型对于真正例查的准不准,即预测的结果中,预测到的正例占总体预测结果的比例; >* 查全率 *Recall* :*R* = $\frac{TP}{TP+FN}$ 反映出模型对于真正例查的全不全,即预测的结果中,预测到的正例占真实总体正例的比例; >* F值 *F-score* :*F-score* = $\frac{2PR}{P+R}$ 反映出模型对于查准率和查全率之间的平衡,好的模型需要兼顾查准率与查全率。 此外还有我们常说的准确率(Accuracy,Acc):Acc= $\frac{TP+TN}{TP + TN + FP + FN}$ 当只有一个二分类任务时,直接使用上面的评价指标即可。但若有n个二分类任务时,为了综合得出总体的评价结果,需要对每个二分类的指标结果,进行Micro 或者 Macro 平均。 ## 宏平均 Macro averaging >Macro averaging是先在每个二分类上分别计算各类的指标,然后取平均值。 >例如有4类: Class A: 1 TP and 1 FP Class B: 10 TP and 90 FP Class C: 1 TP and 1 FP Class D: 1 TP and 1 FP 可知:$P_A$=$P_C$=$P_D$=0.5, 而 $P_B$=0.1. >则宏平均的结果为: > $P_{Macro}=\frac{0.5+0.1+0.5+0.5}{4}=0.4$ > 其他指标的计算类似。 ## 微平均 Micro averaging >Micro averaging则先计算总TP值,其次算总FP值,然后按指标公式计算。 >同样以上面的4类为例,则微平均的结果为: > $P_{Micro }=\frac{TP}{TP+FP}=\frac{1+10+1+1}{2+100+2+2}=0.123$ > 其他指标的计算类似。 ## Macro averaging 和 Micro averaging 之间的差异 1. 两者关注的点不同 >* Macro averaging 是在分别计算了每一类的指标后,求其算术平均值,对每一类都是等同而视的。 虽然多分类任务中常有类别样本不均衡的现象,如上述的4分类中,class B有100个样本,远超其他3类,但Macro averaging的结果并没有因此完全倾向于class B的结果($P_B$=0.1.),而是客观的照顾了其他类的结果。 可以说,在类别样本数目分布不均衡时,Macro会给予样本数目较少的类别与样本数据较大的类别同等的重视程度。 >* Micro averaging 是详细统计了多分类中,每一个样本的预测结果,然后再计算相应指标的,重视的是每一个样本的结果。 在Micro averaging中,其实已经不存在多分类的区别了,所有的类都成了一个类。 重点关注每一个样本的结果,而弱化了类别的区别,对于整体数据集的结果来说,其实是更趋近于客观的结果。如上述的例子中,$P_{Micro }=0.123$而不是0.4。 但着重关于细节带来的后果是,会被样本数目较多的类别的结果影响对于整体的判断。在实际的类别不均衡时,会被大类的结果所影响,而忽略了小类的影响。 2. 类别数目不均衡时,Micro average 的结果一定比 Macro average 的结果好吗? >很多文章建议当label imbalance时,采用Micro average的指标,且大多数情况下,Micro average 的结果似乎好于 Macro average 的结果,但这是一定的吗? ><br>通过1中的分析,可知:Micro average关注的是每一个样本本身的结果,而消除了类别的观念。Macro average则是坚固的对每一个类,不管样本数目多少的,都给予公平的对待,强调了类的观念。所以,使用哪一种评价指标,应该视我们的任务而定。如果任务需要探索类之间的差异,则用Macro average;如果任务只是看模型对于数据集整体的或对每个样本的分类结果,则用Micro average。 ><br>至于对于同一结果,使用两种关注点不同的评价指标带来数值上的差异,孰高孰低,则并不一定。 >例如上例中,$P_{Micro }=0.123<P_{Macro }=0.4$ >但若Class B: 90 TP and 10 FP,则$P_{Micro }=\frac{TP}{TP+FP}=\frac{1+90+1+1}{2+100+2+2}=0.877$,$P_{Macro}=\frac{0.5+0.9+0.5+0.5}{4}=0.6$,则$P_{Micro }>P_{Macro }$。 ><br>这也正说明了类别不均衡时,大类对于最终指标结果的影响。 >当$P_{Micro }<P_{Macro }$时,说明模型对于主类的分类精确性较差,而对于小类的结果好,所以当各个类别同等重要时,较差的主类的结果就别较好的小类的结果带起来了。而当重点考虑每一个样本的结果时,由于主类含有更多的样本数,所以整体的结果就被较差的主类中的预测错误的样本拉下去了。 >当$P_{Micro }>P_{Macro }$时,则说明模型对于主类的分类精确性较好,而对于小类的结果差,刚好与上面的分析相反。 ## Reference 1. https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html 2. https://datascience.stackexchange.com/questions/15989/micro-average-vs-macro-average-performance-in-a-multiclass-classification-settin

    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