<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ronaldo Prati on Medium]]></title>
        <description><![CDATA[Stories by Ronaldo Prati on Medium]]></description>
        <link>https://medium.com/@rcprati?source=rss-931d97760224------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*F7qbjiDpEFCoVx3s.jpg</url>
            <title>Stories by Ronaldo Prati on Medium</title>
            <link>https://medium.com/@rcprati?source=rss-931d97760224------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 10:03:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rcprati/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Micro, macro and the hidden 0/0: three ways an imbalanced-data report can mislead]]></title>
            <link>https://medium.com/@rcprati/micro-macro-and-the-hidden-0-0-three-ways-an-imbalanced-data-report-can-mislead-10f92f496bca?source=rss-931d97760224------2</link>
            <guid isPermaLink="false">https://medium.com/p/10f92f496bca</guid>
            <category><![CDATA[model-evaluation]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[scikit-learn]]></category>
            <dc:creator><![CDATA[Ronaldo Prati]]></dc:creator>
            <pubDate>Tue, 03 Nov 2026 19:44:20 GMT</pubDate>
            <atom:updated>2026-11-03T19:44:20.190Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A model with 0.89 accuracy, a macro-precision that can be 0.94 or 0.75, and three metrics that are the same number.</em></p><p>Take a logistic regression on the digits data, with the classes made imbalanced: 85 test examples of class 0, down to 2 of class 9 (278 test examples in all). Accuracy is 0.89. That looks fine, and it is the kind of number that goes into a report or a manuscript.</p><p>Here are three things in that report that deserve a second look. None needs a new method, only the habit of asking what a number is made of. The numbers are computed with <a href="https://github.com/rcprati/aggmetrics">aggmetrics</a> and confirmed with scikit-learn wherever scikit-learn computes the same thing.</p><h3>Two ways to average over classes</h3><p>With more than two classes, a metric such as precision is first computed for each class, treating that class as the positive one and all the others as negative. There are then two ways to get one number out of the per-class values:</p><ul><li><strong>Macro</strong>: average the per-class values, so every class counts the same.</li><li><strong>Micro</strong>: add up the counts of true positives (TP), false positives (FP), false negatives (FN) and true negatives (TN) over all classes, and compute the metric once on the totals, so every example counts the same.</li></ul><h3>1. Micro-precision, micro-recall and micro-F1 are the same number</h3><p>On single-label data (each example has exactly one true class and one predicted class), an error is always two things at once: a false positive for the class that was predicted and a false negative for the class that was true. So, summed over the classes, the number of false positives equals the number of false negatives.</p><p>In the digits model, 248 of the 278 test examples are classified correctly and 30 are wrong. The pooled counts are therefore TP = 248, FP = 30 and FN = 30. Then</p><ul><li>micro-precision = TP / (TP + FP) = 248 / 278 = 0.8921,</li><li>micro-recall = TP / (TP + FN) = 248 / 278 = 0.8921,</li><li>micro-F1, the harmonic mean of the two, is 0.8921 as well,</li></ul><p>and 248 / 278 is also the accuracy. Three metrics, one number. scikit-learn agrees:</p><pre>from sklearn.metrics import precision_score, recall_score, f1_score<br>for fn in (precision_score, recall_score, f1_score):<br>    print(round(fn(yte, pred, average=&quot;micro&quot;), 4))<br># 0.8921, 0.8921, 0.8921</pre><p>A table with three rows of micro metrics on a multiclass problem reports one number three times.</p><p>The macro versions give each class the same weight, and say something else: macro-F1 is 0.64 and macro-recall is 0.61. Macro-recall is the average of the recall of each class, also known as balanced accuracy. The model is good on the frequent classes and bad on the rare ones, and accuracy hides it.</p><h3>2. A micro average can fall outside the range of the per-class values</h3><p>For precision, recall and F1, the micro value always lies between the smallest and the largest per-class value, which is what you would expect from an average. For some other metrics it does not, and a small example shows how.</p><p>A tiny example, with 15 examples in three classes (1 of class 0, 11 of class 1, 3 of class 2). The counts of each class against the rest, the totals, and two metrics computed from them:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*tQjNZk_I2yE5-P0Fe33inQ.png" /></figure><p>Precision behaves as you would expect. The pooled value, 0.67, lies between the smallest (0.00) and the largest (0.82) per-class value. It has to: pooled precision is 10 / (10 + 5), which is exactly the average of the per-class precisions weighted by how many predictions each class received (1, 11 and 3 predictions).</p><p>MCC (the Matthews correlation coefficient) does not. The per-class values go from -0.07 to 0.32, and the pooled value is 0.50, <strong>above every one of them</strong>. No way of weighting the classes would give 0.50, so the micro MCC is not an average of the classes in any sense. The reason is that MCC mixes all four counts in a product, and the pooled TN (25 of the 45 cells) is built by adding the true negatives of three separate one-against-rest problems, so it is much larger than any single class sees. Balanced accuracy and the G-mean are not weighted averages of the per-class values either, so they can do the same.</p><p>So a micro MCC can look better than every class it summarises. The practical rule: read a micro precision, recall or F1 as an average of the classes, but not a micro MCC.</p><p>On the digits model above this does not happen for any of the five measures I checked (MCC, balanced accuracy, G-mean, informedness, kappa). It takes a particular error pattern, so I would not call it common. In aggmetrics, the function diagnose runs this check for every measure you ask about and warns when the pooled value leaves the range.</p><h3>3. A class that is never predicted</h3><p>This one changed how I write metric names.</p><p>Precision for a class is the fraction of the examples <em>predicted</em> as that class that really belong to it. The model never predicts classes 8 and 9, so for those two classes the fraction is 0 / 0: nothing was predicted as 8 or 9, and precision is undefined. Macro-precision averages over the ten classes, so it still needs a number for them, and the choice moves the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YaILUVnewgfdbiNh3ahC4Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*2L4AcSjfs_V81D5SGnDsDQ.png" /></figure><p>The same predictions give a macro-precision of 0.94 or 0.75, a difference of 0.19, and the model did not change. In scikit-learn this is the zero_division argument, whose default of 0 (with a warning) matches the &quot;count as 0&quot; row: 0.7506 in both libraries. If you exclude the undefined classes, as some implementations do, you get the 0.94.</p><p>None of these choices is wrong. They answer different questions, and the report almost never says which one it used. The number is not “macro-precision”. It is “macro-precision with the 0/0 entries counted as zero”.</p><h3>What I would put in the report</h3><ul><li>Say the averaging operator next to every number: “macro-F1”, not “F1”.</li><li>On imbalanced data, report macro-recall or per-class recall next to accuracy, with the number of examples behind each class.</li><li>Do not report micro-precision, micro-recall and micro-F1 together on single-label data.</li><li>State how undefined entries were handled (zero_divisionin scikit-learn, convention in aggmetrics), or letdiagnose, the package&#39;s check, print the size of the difference.</li></ul><h3>Try it</h3><p>The notebook is runnable in Colab and every number above is checked against scikit-learn where scikit-learn has an equivalent:</p><p><a href="https://colab.research.google.com/github/rcprati/aggmetrics/blob/main/examples/notebooks/02_micro_macro_imbalance.ipynb">02_micro_macro_imbalance.ipynb</a></p><pre>pip install aggmetrics</pre><p>The package, its documentation and the paper that defines the operators and conventions (<a href="http://arxiv.org/abs/2610.30456">arXiv:2610.30456</a>) are linked from<a href="https://github.com/rcprati/aggmetrics">github.com/rcprati/aggmetrics</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10f92f496bca" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>