# Active learning with uncertainty sampling: least confidence, margin and entropy compared

Labels cost money, so active learning lets the model choose what to label next. Uncertainty sampling asks for the examples it is least sure about, but there are three common ways to measure being unsure, and they do not agree. Here you compare them on real predictions.

<div data-sim="specx" data-spec="{&#34;head&#34;:&#34;Active learning: which flowers should a human label next? Least confidence, margin and entropy&#34;,&#34;sub&#34;:&#34;A model trained on six labelled iris flowers predicts 24 unlabelled ones. Pick an uncertainty measure and a bar: the flowers above it go to the annotator. See which ones each measure picks and how many mistakes it catches.&#34;,&#34;params&#34;:[{&#34;id&#34;:&#34;strat&#34;,&#34;label&#34;:&#34;Measure (0 = least confidence, 1 = margin, 2 = entropy)&#34;,&#34;min&#34;:0,&#34;max&#34;:2,&#34;step&#34;:1,&#34;value&#34;:2},{&#34;id&#34;:&#34;qmin&#34;,&#34;label&#34;:&#34;Send to a human when uncertainty is at least&#34;,&#34;min&#34;:0.2,&#34;max&#34;:1,&#34;step&#34;:0.01,&#34;value&#34;:0.75,&#34;note&#34;:&#34;All three measures are scaled to 0 (sure) to 1 (as unsure as possible)&#34;}],&#34;data&#34;:{&#34;columns&#34;:[&#34;flower&#34;,&#34;p1&#34;,&#34;p2&#34;,&#34;correct&#34;],&#34;rows&#34;:[[1,0.783,0.198,1],[2,0.64,0.344,1],[3,0.837,0.15,1],[4,0.744,0.241,1],[5,0.605,0.358,1],[6,0.611,0.202,1],[7,0.761,0.224,1],[8,0.926,0.072,1],[9,0.504,0.412,0],[10,0.504,0.465,1],[11,0.936,0.063,1],[12,0.583,0.352,0],[13,0.554,0.314,1],[14,0.882,0.106,1],[15,0.868,0.123,1],[16,0.855,0.139,1],[17,0.796,0.181,1],[18,0.857,0.133,1],[19,0.515,0.403,1],[20,0.571,0.395,0],[21,0.481,0.454,1],[22,0.869,0.126,1],[23,0.803,0.186,1],[24,0.625,0.266,1]],&#34;note&#34;:&#34;Computed with scikit-learn on the iris dataset: logistic regression trained on 6 labelled flowers (2 per species). p1 and p2 are its two highest class probabilities for an unlabelled flower, correct whether its guess is right.&#34;},&#34;row&#34;:[{&#34;id&#34;:&#34;p3&#34;,&#34;label&#34;:&#34;third probability&#34;,&#34;expr&#34;:&#34;max(1 - p1 - p2, 1e-9)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;u_lc&#34;,&#34;label&#34;:&#34;least confidence&#34;,&#34;expr&#34;:&#34;1.5*(1 - p1)&#34;,&#34;tex&#34;:&#34;u_{LC} = \\frac{1 - p_{(1)}}{1 - 1/3}&#34;},{&#34;id&#34;:&#34;u_mg&#34;,&#34;label&#34;:&#34;margin uncertainty&#34;,&#34;expr&#34;:&#34;1 - (p1 - p2)&#34;,&#34;tex&#34;:&#34;u_{M} = 1 - \\big(p_{(1)} - p_{(2)}\\big)&#34;},{&#34;id&#34;:&#34;u_en&#34;,&#34;label&#34;:&#34;entropy&#34;,&#34;expr&#34;:&#34;-(p1*ln(p1) + p2*ln(max(p2, 1e-12)) + p3*ln(p3))/ln(3)&#34;,&#34;tex&#34;:&#34;u_{H} = -\\frac{1}{\\ln 3}\\sum_{c} p_c \\ln p_c&#34;},{&#34;id&#34;:&#34;score&#34;,&#34;label&#34;:&#34;uncertainty used&#34;,&#34;expr&#34;:&#34;ifelse(ge(0.5, strat), u_lc, ifelse(ge(1.5, strat), u_mg, u_en))&#34;},{&#34;id&#34;:&#34;query&#34;,&#34;label&#34;:&#34;send to annotator&#34;,&#34;expr&#34;:&#34;ge(score, qmin)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;err&#34;,&#34;label&#34;:&#34;model wrong&#34;,&#34;expr&#34;:&#34;1 - correct&#34;,&#34;fmt&#34;:&#34;i&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;caught&#34;,&#34;label&#34;:&#34;wrong and sent&#34;,&#34;expr&#34;:&#34;query*(1 - correct)&#34;,&#34;fmt&#34;:&#34;i&#34;,&#34;hide&#34;:true}],&#34;derived&#34;:[{&#34;id&#34;:&#34;nq&#34;,&#34;label&#34;:&#34;Flowers sent for labelling&#34;,&#34;expr&#34;:&#34;sum(query)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;nerr&#34;,&#34;label&#34;:&#34;Wrong guesses in the pool&#34;,&#34;expr&#34;:&#34;sum(err)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;ncaught&#34;,&#34;label&#34;:&#34;Wrong guesses among those sent&#34;,&#34;expr&#34;:&#34;sum(caught)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;errsent&#34;,&#34;label&#34;:&#34;Error rate among those sent&#34;,&#34;expr&#34;:&#34;sum(caught)/max(sum(query), 1)&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;\\frac{\\#\\{\\text{sent and wrong}\\}}{\\#\\{\\text{sent}\\}}&#34;},{&#34;id&#34;:&#34;errbase&#34;,&#34;label&#34;:&#34;Error rate of a random pick&#34;,&#34;expr&#34;:&#34;mean(err)&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;mlc&#34;,&#34;label&#34;:&#34;Highest least-confidence score&#34;,&#34;expr&#34;:&#34;amax(u_lc)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;mmg&#34;,&#34;label&#34;:&#34;Highest margin uncertainty&#34;,&#34;expr&#34;:&#34;amax(u_mg)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;men&#34;,&#34;label&#34;:&#34;Highest entropy&#34;,&#34;expr&#34;:&#34;amax(u_en)&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;corr_mh&#34;,&#34;label&#34;:&#34;Correlation of margin and entropy&#34;,&#34;expr&#34;:&#34;corr(u_mg, u_en)&#34;}],&#34;row2&#34;:[{&#34;id&#34;:&#34;t_lc&#34;,&#34;label&#34;:&#34;top by least confidence&#34;,&#34;expr&#34;:&#34;ge(u_lc, mlc)*flower&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;t_mg&#34;,&#34;label&#34;:&#34;top by margin&#34;,&#34;expr&#34;:&#34;ge(u_mg, mmg)*flower&#34;,&#34;hide&#34;:true},{&#34;id&#34;:&#34;t_en&#34;,&#34;label&#34;:&#34;top by entropy&#34;,&#34;expr&#34;:&#34;ge(u_en, men)*flower&#34;,&#34;hide&#34;:true}],&#34;post&#34;:[{&#34;id&#34;:&#34;pick_lc&#34;,&#34;label&#34;:&#34;First pick by least confidence&#34;,&#34;expr&#34;:&#34;amax(t_lc)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;pick_mg&#34;,&#34;label&#34;:&#34;First pick by margin&#34;,&#34;expr&#34;:&#34;amax(t_mg)&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;pick_en&#34;,&#34;label&#34;:&#34;First pick by entropy&#34;,&#34;expr&#34;:&#34;amax(t_en)&#34;,&#34;fmt&#34;:&#34;i&#34;}],&#34;chart&#34;:{&#34;title&#34;:&#34;Uncertainty of each flower against the model&#39;s top probability&#34;,&#34;x&#34;:{&#34;var&#34;:&#34;t&#34;,&#34;label&#34;:&#34;Top class probability p(1)&#34;,&#34;from&#34;:0.33,&#34;to&#34;:1},&#34;y&#34;:{&#34;from&#34;:-0.05,&#34;to&#34;:1.05},&#34;series&#34;:[{&#34;label&#34;:&#34;Least confidence&#34;,&#34;expr&#34;:&#34;1.5*(1 - t)&#34;},{&#34;label&#34;:&#34;Entropy if the rest is split evenly&#34;,&#34;expr&#34;:&#34;-(t*ln(t) + (1 - t)*ln(max((1 - t)/2, 1e-12)))/ln(3)&#34;},{&#34;label&#34;:&#34;Bar for sending to a human&#34;,&#34;expr&#34;:&#34;qmin&#34;,&#34;dash&#34;:true}],&#34;points&#34;:{&#34;x&#34;:&#34;p1&#34;,&#34;y&#34;:&#34;correct&#34;,&#34;label&#34;:&#34;Guess right (1) or wrong (0)&#34;},&#34;fit&#34;:{&#34;x&#34;:&#34;p1&#34;,&#34;y&#34;:&#34;score&#34;,&#34;label&#34;:&#34;Uncertainty used, per flower&#34;}},&#34;level&#34;:&#34;intermediate&#34;,&#34;bars&#34;:{&#34;title&#34;:&#34;Uncertainty of each flower under the chosen measure&#34;,&#34;y&#34;:&#34;score&#34;,&#34;label&#34;:&#34;flower&#34;},&#34;steps&#34;:[{&#34;title&#34;:&#34;Train on what is labelled&#34;,&#34;text&#34;:&#34;Six labelled flowers, two per species, train the model. It then predicts the 24 unlabelled ones; {nerr} of its guesses are wrong, an error rate of {errbase:p}.&#34;,&#34;show&#34;:[&#34;nerr&#34;,&#34;errbase&#34;]},{&#34;title&#34;:&#34;Score uncertainty three ways&#34;,&#34;text&#34;:&#34;Least confidence looks only at the top probability, margin at the gap between the top two, entropy at all three. Margin and entropy correlate at {corr_mh}, yet they disagree on the order.&#34;,&#34;show&#34;:[&#34;corr_mh&#34;]},{&#34;title&#34;:&#34;Compare first picks&#34;,&#34;text&#34;:&#34;The most uncertain flower is number {pick_lc} by least confidence, {pick_mg} by margin and {pick_en} by entropy.&#34;,&#34;show&#34;:[&#34;pick_lc&#34;,&#34;pick_mg&#34;,&#34;pick_en&#34;]},{&#34;title&#34;:&#34;Send the uncertain ones to a human&#34;,&#34;text&#34;:&#34;With measure {strat} and a bar of {qmin}, {nq} flowers go to the annotator.&#34;,&#34;show&#34;:[&#34;nq&#34;]},{&#34;title&#34;:&#34;Check what the queries catch&#34;,&#34;text&#34;:&#34;{ncaught} of the {nerr} wrong guesses are among them: the error rate in the queried set is {errsent:p}, against {errbase:p} for a random pick. Label them, retrain and score the pool again.&#34;,&#34;show&#34;:[&#34;ncaught&#34;,&#34;errsent&#34;]}]}"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## What you are looking at

A logistic regression was trained with scikit-learn on only six labelled flowers from the iris dataset, two of each species. It then predicted the species of 24 unlabelled flowers. For each one the table keeps its two highest class probabilities, p1 and p2 (the third is what is left), and whether its guess is actually right, which in real life you would only learn after a human labels it. All numbers come from that run.

On the chart each flower sits at its top probability. The solid curves show least confidence, which depends only on p1, and the entropy a flower would have if the rest of its probability were split evenly between the other two species. The fitted markers are each flower's uncertainty under the measure you picked, and the dashed line is the bar above which a flower goes to the annotator. The dots at 1 and 0 mark right and wrong guesses. The bars show the same uncertainty flower by flower.

## The algorithm

Pool-based active learning repeats one loop: train on what is labelled, score every unlabelled example, send the most informative ones to a human, add their labels and train again. modAL, a scikit-learn-style active learning library, ships the three classic uncertainty scores:

1. **Least confidence:** 1 − p(1), one minus the top probability. modAL calls this classifier uncertainty. It ignores how the remaining probability is spread.
2. **Margin:** p(1) − p(2), the gap between the top two classes; a small margin means the model is torn between two answers. Here it is turned into an uncertainty, 1 − margin.
3. **Entropy:** −Σ p ln p over all classes, highest when every class is equally likely.

To put them on one scale the simulation divides least confidence by its maximum, 2/3, and entropy by ln 3, so all three run from 0 (certain) to 1 (as unsure as three classes allow). Libraries usually query the top-k examples by score; a bar on the score does the same job and makes the size of the batch visible.

### Worked example

Take flower 6: p = 0.611, 0.202, 0.187. Least confidence is 1.5 × (1 − 0.611) = 0.584. The margin is 0.611 − 0.202 = 0.409, an uncertainty of 0.591. Its entropy is −(0.611 ln 0.611 + 0.202 ln 0.202 + 0.187 ln 0.187) / ln 3 = 0.853, one of the highest in the pool, because the leftover probability is shared evenly. Now flower 10: p = 0.504, 0.465, 0.031. Its margin uncertainty is 0.961, but its entropy is only 0.736, because the third species is all but ruled out. With entropy and a bar of 0.75, seven flowers go to the annotator (6, 9, 12, 13, 19, 21 and 24). Two of the three wrong guesses are among them, an error rate of 28.6% in the queried set against 12.5% in the pool. The most uncertain flower is 21 by least confidence and by margin, but 13 by entropy.

## Things to try

- Switch to margin (1) at the same bar. Eight flowers are sent and all three wrong guesses are among them, an error rate of 37.5%. All three mistakes are versicolor flowers taken for virginica, the two species the dataset's description says are not linearly separable, and margin is built to find exactly that kind of two-way confusion.
- Switch to least confidence (0). At 0.75 only flower 21 passes. Lower the bar to 0.60: seven flowers, all three errors caught, 42.9% of the queried set wrong.
- With entropy, lower the bar to 0.50: fifteen flowers are sent and all three errors are caught, but the error rate in the batch falls to 20%: you pay for labels the model did not need.
- Read the correlation of margin and entropy, 0.924. Highly correlated measures can still rank the top of the list differently, and the top is all that gets labelled.
- Paste your own model's two highest probabilities for a pool and see which examples each measure would buy.

## How it works in real systems

Uncertainty sampling is the simplest and most used active learning strategy, but it has known traps. It trusts the model's probabilities, so a badly calibrated model asks the wrong questions. It can pick many near-duplicates from the same confused region in one batch, which is why batch methods add diversity, for example by clustering candidates or sampling from embeddings. And it ignores how representative an example is: the most uncertain one is sometimes just noise. The same scores drive data selection for large models today, from choosing which model outputs go to human raters to picking hard prompts for fine-tuning. When labels are cheap to guess instead of buy, the opposite move is pseudo-labelling the confident ones, shown in the [pseudo-labelling simulation](/ai-simulations/pseudo-labelling-confidence-threshold). For how probabilities turn into decisions see [precision and recall against a threshold](/ai-simulations/precision-recall-threshold), and for the model used here see [logistic regression](/ai-simulations/logistic-regression-gradient-descent).

## Sources

- [modAL: uncertainty measures and sampling strategies (uncertainty.py)](https://github.com/modAL-python/modAL/blob/master/modAL/uncertainty.py)
- [modAL README: the pool-based active learning loop](https://github.com/modAL-python/modAL/blob/master/README.md)
- [scikit-learn: the iris dataset description](https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/datasets/descr/iris.rst)
