Micro F1 score in Scikit-Learn with Class imbalance












0















I have some class imbalance and a simple baseline classifier that assigns the majority class to every sample:



from sklearn.metrics import precision_score, recall_score, confusion_matrix

y_true = [0,0,0,1]
y_pred = [0,0,0,0]
confusion_matrix(y_true, y_pred)


This yields




[[3, 0],



[1, 0]]




This means TP=3, FP=1, FN=0.



So far, so good. Now I want to calculate the micro average of precision and recall.



precision_score(y_true, y_pred, average='micro') # yields 0.75
recall_score(y_true, y_pred, average='micro') # yields 0.75


I am Ok with the precision, but why is recall not 1.0? How can they ever be the same in this example, given that FP > 0 and FN == 0? I know it must have to do with the micro averaging, but I can't wrap my head around this one.










share|improve this question



























    0















    I have some class imbalance and a simple baseline classifier that assigns the majority class to every sample:



    from sklearn.metrics import precision_score, recall_score, confusion_matrix

    y_true = [0,0,0,1]
    y_pred = [0,0,0,0]
    confusion_matrix(y_true, y_pred)


    This yields




    [[3, 0],



    [1, 0]]




    This means TP=3, FP=1, FN=0.



    So far, so good. Now I want to calculate the micro average of precision and recall.



    precision_score(y_true, y_pred, average='micro') # yields 0.75
    recall_score(y_true, y_pred, average='micro') # yields 0.75


    I am Ok with the precision, but why is recall not 1.0? How can they ever be the same in this example, given that FP > 0 and FN == 0? I know it must have to do with the micro averaging, but I can't wrap my head around this one.










    share|improve this question

























      0












      0








      0








      I have some class imbalance and a simple baseline classifier that assigns the majority class to every sample:



      from sklearn.metrics import precision_score, recall_score, confusion_matrix

      y_true = [0,0,0,1]
      y_pred = [0,0,0,0]
      confusion_matrix(y_true, y_pred)


      This yields




      [[3, 0],



      [1, 0]]




      This means TP=3, FP=1, FN=0.



      So far, so good. Now I want to calculate the micro average of precision and recall.



      precision_score(y_true, y_pred, average='micro') # yields 0.75
      recall_score(y_true, y_pred, average='micro') # yields 0.75


      I am Ok with the precision, but why is recall not 1.0? How can they ever be the same in this example, given that FP > 0 and FN == 0? I know it must have to do with the micro averaging, but I can't wrap my head around this one.










      share|improve this question














      I have some class imbalance and a simple baseline classifier that assigns the majority class to every sample:



      from sklearn.metrics import precision_score, recall_score, confusion_matrix

      y_true = [0,0,0,1]
      y_pred = [0,0,0,0]
      confusion_matrix(y_true, y_pred)


      This yields




      [[3, 0],



      [1, 0]]




      This means TP=3, FP=1, FN=0.



      So far, so good. Now I want to calculate the micro average of precision and recall.



      precision_score(y_true, y_pred, average='micro') # yields 0.75
      recall_score(y_true, y_pred, average='micro') # yields 0.75


      I am Ok with the precision, but why is recall not 1.0? How can they ever be the same in this example, given that FP > 0 and FN == 0? I know it must have to do with the micro averaging, but I can't wrap my head around this one.







      scikit-learn precision-recall






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 14:16









      TobyToby

      5901719




      5901719
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Yes, its because of micro-averaging. See the documentation here to know how its calculated:




          Note that if all labels are included, “micro”-averaging in a
          multiclass setting will produce precision, recall and f-score that are all
          identical to accuracy.




          As you can see in the above linked page, both precision and recall are defined as:
          enter image description here



          where R(y, y-hat) is:



          enter image description here



          So in your case, Recall-micro will be calculated as



          R = number of correct predictions / total predictions = 3/4 = 0.75





          share|improve this answer
























          • Thanks. The paragraph you linked is weird though. The sentence "Note that if all labels are included, “micro”-averaging in a multiclass setting will produce precision, recall and that are all identical to accuracy" is in line with what you say, but that paragraph does not say what A and B actually are, and weirdly, it defines y-hat as the set of true labels and y as the set of predicted labels, which is unconventional, no? My conclusion is to not use micro-averaging for binary classification and to think some more about this.

            – Toby
            Nov 28 '18 at 8:24








          • 1





            Ah, forget the thing I said about A, B not being defined, I get it. But too late to edit my comment. Thanks!

            – Toby
            Nov 28 '18 at 8:32











          • @Toby Yes, that is unconventional. But all the following things match with this, so no worries. If you want, you may put an issue regarding this usage on scikit-learn github page

            – Vivek Kumar
            Nov 28 '18 at 8:33











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394984%2fmicro-f1-score-in-scikit-learn-with-class-imbalance%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Yes, its because of micro-averaging. See the documentation here to know how its calculated:




          Note that if all labels are included, “micro”-averaging in a
          multiclass setting will produce precision, recall and f-score that are all
          identical to accuracy.




          As you can see in the above linked page, both precision and recall are defined as:
          enter image description here



          where R(y, y-hat) is:



          enter image description here



          So in your case, Recall-micro will be calculated as



          R = number of correct predictions / total predictions = 3/4 = 0.75





          share|improve this answer
























          • Thanks. The paragraph you linked is weird though. The sentence "Note that if all labels are included, “micro”-averaging in a multiclass setting will produce precision, recall and that are all identical to accuracy" is in line with what you say, but that paragraph does not say what A and B actually are, and weirdly, it defines y-hat as the set of true labels and y as the set of predicted labels, which is unconventional, no? My conclusion is to not use micro-averaging for binary classification and to think some more about this.

            – Toby
            Nov 28 '18 at 8:24








          • 1





            Ah, forget the thing I said about A, B not being defined, I get it. But too late to edit my comment. Thanks!

            – Toby
            Nov 28 '18 at 8:32











          • @Toby Yes, that is unconventional. But all the following things match with this, so no worries. If you want, you may put an issue regarding this usage on scikit-learn github page

            – Vivek Kumar
            Nov 28 '18 at 8:33
















          1














          Yes, its because of micro-averaging. See the documentation here to know how its calculated:




          Note that if all labels are included, “micro”-averaging in a
          multiclass setting will produce precision, recall and f-score that are all
          identical to accuracy.




          As you can see in the above linked page, both precision and recall are defined as:
          enter image description here



          where R(y, y-hat) is:



          enter image description here



          So in your case, Recall-micro will be calculated as



          R = number of correct predictions / total predictions = 3/4 = 0.75





          share|improve this answer
























          • Thanks. The paragraph you linked is weird though. The sentence "Note that if all labels are included, “micro”-averaging in a multiclass setting will produce precision, recall and that are all identical to accuracy" is in line with what you say, but that paragraph does not say what A and B actually are, and weirdly, it defines y-hat as the set of true labels and y as the set of predicted labels, which is unconventional, no? My conclusion is to not use micro-averaging for binary classification and to think some more about this.

            – Toby
            Nov 28 '18 at 8:24








          • 1





            Ah, forget the thing I said about A, B not being defined, I get it. But too late to edit my comment. Thanks!

            – Toby
            Nov 28 '18 at 8:32











          • @Toby Yes, that is unconventional. But all the following things match with this, so no worries. If you want, you may put an issue regarding this usage on scikit-learn github page

            – Vivek Kumar
            Nov 28 '18 at 8:33














          1












          1








          1







          Yes, its because of micro-averaging. See the documentation here to know how its calculated:




          Note that if all labels are included, “micro”-averaging in a
          multiclass setting will produce precision, recall and f-score that are all
          identical to accuracy.




          As you can see in the above linked page, both precision and recall are defined as:
          enter image description here



          where R(y, y-hat) is:



          enter image description here



          So in your case, Recall-micro will be calculated as



          R = number of correct predictions / total predictions = 3/4 = 0.75





          share|improve this answer













          Yes, its because of micro-averaging. See the documentation here to know how its calculated:




          Note that if all labels are included, “micro”-averaging in a
          multiclass setting will produce precision, recall and f-score that are all
          identical to accuracy.




          As you can see in the above linked page, both precision and recall are defined as:
          enter image description here



          where R(y, y-hat) is:



          enter image description here



          So in your case, Recall-micro will be calculated as



          R = number of correct predictions / total predictions = 3/4 = 0.75






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 10:37









          Vivek KumarVivek Kumar

          16.3k42055




          16.3k42055













          • Thanks. The paragraph you linked is weird though. The sentence "Note that if all labels are included, “micro”-averaging in a multiclass setting will produce precision, recall and that are all identical to accuracy" is in line with what you say, but that paragraph does not say what A and B actually are, and weirdly, it defines y-hat as the set of true labels and y as the set of predicted labels, which is unconventional, no? My conclusion is to not use micro-averaging for binary classification and to think some more about this.

            – Toby
            Nov 28 '18 at 8:24








          • 1





            Ah, forget the thing I said about A, B not being defined, I get it. But too late to edit my comment. Thanks!

            – Toby
            Nov 28 '18 at 8:32











          • @Toby Yes, that is unconventional. But all the following things match with this, so no worries. If you want, you may put an issue regarding this usage on scikit-learn github page

            – Vivek Kumar
            Nov 28 '18 at 8:33



















          • Thanks. The paragraph you linked is weird though. The sentence "Note that if all labels are included, “micro”-averaging in a multiclass setting will produce precision, recall and that are all identical to accuracy" is in line with what you say, but that paragraph does not say what A and B actually are, and weirdly, it defines y-hat as the set of true labels and y as the set of predicted labels, which is unconventional, no? My conclusion is to not use micro-averaging for binary classification and to think some more about this.

            – Toby
            Nov 28 '18 at 8:24








          • 1





            Ah, forget the thing I said about A, B not being defined, I get it. But too late to edit my comment. Thanks!

            – Toby
            Nov 28 '18 at 8:32











          • @Toby Yes, that is unconventional. But all the following things match with this, so no worries. If you want, you may put an issue regarding this usage on scikit-learn github page

            – Vivek Kumar
            Nov 28 '18 at 8:33

















          Thanks. The paragraph you linked is weird though. The sentence "Note that if all labels are included, “micro”-averaging in a multiclass setting will produce precision, recall and that are all identical to accuracy" is in line with what you say, but that paragraph does not say what A and B actually are, and weirdly, it defines y-hat as the set of true labels and y as the set of predicted labels, which is unconventional, no? My conclusion is to not use micro-averaging for binary classification and to think some more about this.

          – Toby
          Nov 28 '18 at 8:24







          Thanks. The paragraph you linked is weird though. The sentence "Note that if all labels are included, “micro”-averaging in a multiclass setting will produce precision, recall and that are all identical to accuracy" is in line with what you say, but that paragraph does not say what A and B actually are, and weirdly, it defines y-hat as the set of true labels and y as the set of predicted labels, which is unconventional, no? My conclusion is to not use micro-averaging for binary classification and to think some more about this.

          – Toby
          Nov 28 '18 at 8:24






          1




          1





          Ah, forget the thing I said about A, B not being defined, I get it. But too late to edit my comment. Thanks!

          – Toby
          Nov 28 '18 at 8:32





          Ah, forget the thing I said about A, B not being defined, I get it. But too late to edit my comment. Thanks!

          – Toby
          Nov 28 '18 at 8:32













          @Toby Yes, that is unconventional. But all the following things match with this, so no worries. If you want, you may put an issue regarding this usage on scikit-learn github page

          – Vivek Kumar
          Nov 28 '18 at 8:33





          @Toby Yes, that is unconventional. But all the following things match with this, so no worries. If you want, you may put an issue regarding this usage on scikit-learn github page

          – Vivek Kumar
          Nov 28 '18 at 8:33




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394984%2fmicro-f1-score-in-scikit-learn-with-class-imbalance%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)