Tutorials › Statistical Learning (Graduate) › Pitfalls in Label Encodings and Structured Decoding

Multiclass and Structured Losses · Tutorial 590 of 989

Pitfalls in Label Encodings and Structured Decoding

Diagnose encoding, feasibility, and loss-alignment errors that can change a classifier’s or structured predictor’s decision.

Advanced 11 min read

What You'll Learn

  • Distinguish nominal class labels from numeric measurements and explain when recoding changes a decision.
  • Prove why a Bayes classifier under zero-one loss is invariant to a permutation of class names.
  • Identify how coordinatewise decisions can produce an output outside the valid structured set.
  • Derive the Bayes action for a specified evaluation loss and conditional distribution.
  • Check whether a training objective and decoder target the evaluation loss of interest.

Why Encoding and Decoding Choices Matter

A class name is often just a name. Replacing “case,” “control,” and “other” with different symbols should not change which class is most probable or which decision minimizes a stated loss. But an algorithm can treat labels as numbers, and numbers carry order and distance whether or not the labels have any meaningful order or distance. That can make an arbitrary coding choice affect the fitted rule.

A related issue arises in structured prediction. A model may produce sensible predictions for each component separately, yet the assembled output may violate constraints such as “exactly one label is active” or “the sequence must follow a valid transition.” Finally, a training loss and an evaluation loss need not prefer the same decision. These are distinct failure modes: a representation can introduce unintended geometry, a decoder can ignore feasibility, and an objective can target the wrong notion of error.

The central diagnostic question is: what is the set of allowable decisions, and which loss is the decision supposed to minimize? Once these are explicit, the effects of an encoding or decoding choice can be checked rather than assumed.

Nominal Labels Are Not Measurements

Let \(\mathcal{C}=\{c_1,\ldots,c_K\}\) be a finite set of classes. For a feature value \(x\), write \(p_k(x)=P(Y=c_k\mid X=x)\). Under zero-one loss, an incorrect class costs one and a correct class costs zero, so the conditional risk of predicting \(c_k\) is \(1-p_k(x)\). The Bayes decision is therefore any class attaining the largest conditional probability.

Definition: A nominal class encoding assigns symbols or codes to class names without claiming that differences between codes have statistical meaning. A procedure is invariant to class relabeling if consistently permuting the class names and their corresponding probabilities does not change the decision except for that same permutation.

This invariance is a property of the zero-one Bayes decision, not a guarantee that every numerical algorithm will have it. For example, if labels \(0,1,2\) are treated as real-valued responses and squared error is minimized, the conditional mean is used. That operation regards the code differences as meaningful. Decoding the resulting mean to the nearest code then imposes a further geometry on the classes.

Worked Example: Arbitrary Codes Change a Classification Decision

Suppose the posterior probabilities for three nominal classes are \(P(A\mid x)=0.40\), \(P(B\mid x)=0.35\), and \(P(C\mid x)=0.25\). Under zero-one loss, the most probable class is \(A\), since \(0.40\) is largest.

Now encode \(A,B,C\) as \(0,1,10\), respectively, fit a scalar prediction by squared error, and decode by choosing the nearest code. The conditional squared-error risk for a scalar prediction \(t\) is \(0.40(t-0)^2+0.35(t-1)^2+0.25(t-10)^2\). Its minimizer is the conditional mean of the codes: \(t=0.40(0)+0.35(1)+0.25(10)=2.85\). The distances to the three codes are \(|2.85-0|=2.85\), \(|2.85-1|=1.85\), and \(|2.85-10|=7.15\). The decoded class is \(B\), not \(A\).

Recode the same classes as \(A=0,B=10,C=1\). The conditional mean becomes \(0.40(0)+0.35(10)+0.25(1)=3.75\). Its distances to the codes for \(A,B,C\) are \(3.75,6.25,2.75\), respectively, so the decoded class is now \(C\). The class probabilities did not change; only the arbitrary codes changed. Squared error on these codes is not a relabeling-invariant classification objective.

Theorem: For a finite class set and zero-one loss, the Bayes classifier is invariant to any bijective relabeling of the classes, provided the class probabilities are relabeled with them. Its set of Bayes decisions consists exactly of the classes maximizing their conditional probabilities.

Proof. At a fixed \(x\), predicting class \(a\) incurs conditional loss \(0\) when \(Y=a\) and \(1\) otherwise. Thus its conditional risk is \(R(a\mid x)=0\cdot P(Y=a\mid x)+1\cdot P(Y\ne a\mid x)=1-p_a(x)\). Minimizing \(1-p_a(x)\) over \(a\) is equivalent to maximizing \(p_a(x)\). Let \(\pi\) be a bijection on the class names and define the relabeled probabilities by \(p'_{\pi(a)}(x)=p_a(x)\). Then \(p_a(x)\ge p_b(x)\) exactly when \(p'_{\pi(a)}(x)\ge p'_{\pi(b)}(x)\). Hence the maximizers are mapped to the corresponding relabeled maximizers, including any ties. \(\square\)

This result concerns the target Bayes rule. A particular training algorithm may still depend on label representation because of its parameterization, regularization, optimization, or a loss that assigns numerical distances to codes. The theorem does not say that every fitted model is automatically invariant; it identifies the behavior that a nominal-class decision under zero-one loss should have.

Valid Outputs and Componentwise Decoding

Structured prediction has an additional requirement: predictions must belong to the allowed output set. Let \(\mathcal{Y}\) be the set of valid outputs. For example, a structured label might be a sequence with allowed transitions, a matching, a tree, or a binary vector satisfying a cardinality constraint. A decoder that chooses components independently can return an element of a larger unconstrained space rather than an element of \(\mathcal{Y}\).

Marginal decoding can be appropriate for Hamming loss when each output coordinate is assessed separately. But that fact does not remove feasibility constraints. In particular, if marginal probabilities tie, different coordinatewise tie-breaking rules can yield different assembled outputs, some of which may be invalid.

Worked Example: Marginal Ties Produce an Invalid Output

Suppose the only valid outputs are \(\mathcal{Y}=\{(1,0),(0,1)\}\): exactly one of two labels must be active. Given \(x\), let the conditional distribution put probability \(0.5\) on each valid output.

For the first coordinate, the marginal probability of a 1 is \(0.5\); for the second coordinate it is also \(0.5\). Under coordinatewise Hamming decoding, each coordinate is a tie between predicting 0 and predicting 1. If the tie rule chooses 1 in both coordinates, the decoded output is \((1,1)\), which is not in \(\mathcal{Y}\). If it chooses 0 in both, the result \((0,0)\) is also invalid.

The issue is not an incorrect calculation of either marginal. Both marginals are correct. The issue is that independent decisions do not enforce the joint constraint. A decoder that must produce a valid output needs to optimize over \(\mathcal{Y}\); for this distribution, the two feasible outputs are tied under Hamming risk, so either is a valid minimum-risk choice.

A useful distinction is between estimating components and selecting an output. Marginal probabilities describe the separate components, while a structured decoder maps those probabilities or other model scores to one joint output. If the task requires feasibility, validity must be enforced by the decoder or by a representation whose outputs are guaranteed to be valid. It should not be assumed to follow automatically from accurate marginal estimation.

Training Loss and Evaluation Loss

A prediction rule is judged by its evaluation loss, not merely by the objective used to fit its parameters. In a finite decision problem, let \(\mathcal{A}\) be the allowed actions, \(\mathcal{Y}\) the possible truths, and \(L(a,y)\) the evaluation loss for taking action \(a\) when the truth is \(y\). Given \(X=x\), the conditional risk of action \(a\) is the posterior average loss.

Definition: For a finite action set and finite outcome set, the Bayes action under loss \(L\) at feature value \(x\) is any action minimizing \(R_L(a\mid x)=\sum_{y\in\mathcal{Y}}L(a,y)P(Y=y\mid X=x)\) over \(a\in\mathcal{A}\). A training procedure is aligned with this decision target only if its fitted prediction and decoder minimize, or consistently approximate, risk for the evaluation loss of interest.
Theorem: Suppose \(\mathcal{A}\) and \(\mathcal{Y}\) are finite, \(L\) is a finite nonnegative loss, and the conditional probabilities \(P(Y=y\mid X)\) are measurable. Among measurable decision rules \(g(X)\in\mathcal{A}\), a rule that minimizes \(R_L(a\mid X)\) at each \(X\) minimizes expected evaluation loss \(E[L(g(X),Y)]\).

Proof. For any measurable rule \(g\), conditioning on \(X\) gives \(E[L(g(X),Y)]=E[E[L(g(X),Y)\mid X]]=E[R_L(g(X)\mid X)]\). Because the action and outcome sets are finite and the conditional risks are measurable, fix an ordering of \(\mathcal{A}\) and choose the first action attaining the minimum at each \(X\). This produces a measurable rule \(g^*\). For every \(X\), by its construction, \(R_L(g^*(X)\mid X)\le R_L(g(X)\mid X)\). Taking expectations preserves the inequality, so \(E[L(g^*(X),Y)]\le E[L(g(X),Y)]\). Thus the pointwise Bayes action minimizes expected evaluation loss. \(\square\)

Worked Example: Zero-One Training Decisions Miss Asymmetric Costs

Let \(Y\in\{0,1\}\) with \(P(Y=1\mid x)=0.40\) and \(P(Y=0\mid x)=0.60\). Under zero-one loss, predicting 0 has conditional risk \(0.40\), while predicting 1 has conditional risk \(0.60\). The zero-one Bayes decision is 0.

Now suppose the evaluation loss charges 2 for predicting 0 when \(Y=1\), charges 1 for predicting 1 when \(Y=0\), and charges 0 for a correct prediction. The evaluation risk of action 0 is \(R_L(0\mid x)=2(0.40)+0(0.60)=0.80\). The evaluation risk of action 1 is \(R_L(1\mid x)=0(0.40)+1(0.60)=0.60\). Thus action 1 is the evaluation Bayes decision, even though it is not the zero-one Bayes decision.

A model that estimates the probability \(0.40\) accurately can still lead to the wrong evaluation decision if its decoder uses the default threshold \(0.50\). The probability estimate and the decision rule are separate parts of the procedure: the threshold must reflect the evaluation costs.

The distinction also applies to structured problems. A surrogate loss can be useful for fitting, but the final decision should be checked against the task loss and the feasible output set. Minimizing a surrogate objective does not, by itself, establish that the decoded prediction minimizes task risk. The connection requires conditions relating surrogate risk to task risk and a decoder that respects the decision problem.

Common Mistakes and Practical Checks

  • Wrong reasoning: “The labels are numbers, so their differences are meaningful.” Correct the interpretation first: a code is only a name unless the problem supplies a meaningful order or distance. For nominal classes, check whether changing the code assignment changes predictions.
  • Wrong reasoning: “Every marginal prediction is correct, so the assembled output must be valid.” Coordinatewise correctness does not enforce a joint constraint. Verify that each decoded output lies in \(\mathcal{Y}\), including outputs produced at ties.
  • Wrong reasoning: “The model was trained with a loss, so its predictions minimize the evaluation loss.” Training and evaluation objectives can rank actions differently. Write down the conditional evaluation risks and compare them for the candidate actions.
  • Wrong reasoning: “A well-calibrated probability model needs no task-specific decoder.” Probabilities describe uncertainty; a decision requires a loss and an action set. Use the evaluation costs and feasibility constraints when mapping probabilities to actions.
Key takeaway: Treat class codes as arbitrary unless their geometry is substantively justified, treat structured feasibility as a decoding requirement, and derive decisions from the evaluation loss rather than assuming the training loss selects them automatically.

Check Your Understanding

For each question, identify the representation, feasibility, or loss issue that matters.

  1. Why does the zero-one Bayes classifier change only by the corresponding relabeling when class names are permuted?
  2. For posterior probabilities \(0.40,0.35,0.25\), what class is selected under zero-one loss, and why can scalar squared-error coding select another class?
  3. If the only valid binary outputs are \((1,0)\) and \((0,1)\), what can go wrong when both marginal probabilities equal \(0.5\)?
  4. In the asymmetric-cost example, calculate the evaluation risks of actions 0 and 1 and state which action is preferred.
  5. Give one check that would reveal whether a decoder respects a structured output constraint.
Clinical Biostats