Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Nearest-Neighbor Label Transfer

Cell-Type Annotation and Its Uncertainty · Tutorial 330 of 700

Nearest-Neighbor Label Transfer

See how to define a feature-space neighborhood, transfer its majority label, and diagnose how distance choices and neighborhood size shape the result.

Intermediate 10 min read

What You'll Learn

  • Define nearest-neighbor label transfer using a specified feature space and distance.
  • Calculate a query cell’s nearest reference cells and majority-vote label.
  • Explain how changing neighborhood size can change an assignment.
  • Recognize the effects of feature scaling, ties, and uneven local class composition.
  • Diagnose when a nearest-neighbor label is poorly supported by the reference geometry.

Why Transfer Labels by Neighborhood?

A reference atlas can provide cell-type labels for cells whose identities have already been curated. Label transfer uses those labeled cells to assign labels to cells in a query data set. One direct approach is to represent both sets of cells in the same feature space, find the reference cells nearest to each query cell, and use their labels to make an assignment. This is a local rule: it relies on the labels of cells near the particular query, rather than fitting one global boundary for all cells.

The method is simple, but its result depends on what “near” means. The feature representation, distance measure, number of neighbors, and labels available in the local neighborhood all matter. The earlier material on harmonizing reference labels across atlases is relevant here: the label categories must have compatible meanings before a vote across reference cells is meaningful.

Definition: Let the labeled reference cells be \(r_1,\ldots,r_n\), with feature vectors \(z_1,\ldots,z_n\) in a specified feature space and labels \(y_1,\ldots,y_n\) from a common label set \(\mathcal{C}\). Let a query cell have feature vector \(z_q\) in that same space. For a chosen distance \(d\) and neighborhood size \(k\), the \(k\) nearest neighbors are the \(k\) reference cells with smallest distances \(d(z_q,z_i)\). Nearest-neighbor label transfer assigns the query the label occurring most often among those neighbors.

Distance, Feature Space, and the Voting Rule

A feature vector might contain selected gene-expression measurements or coordinates from a dimension-reduction representation. The method does not decide which representation is biologically appropriate: that choice must be made before finding neighbors. Query and reference features must be constructed comparably, with the same feature definitions and compatible preprocessing. Otherwise, a distance between their vectors may not represent meaningful similarity.

For numeric features, Euclidean distance is a common choice. If each cell has \(p\) features, the distance between the query and reference cell \(i\) is

$$ d(z_q,z_i)=\sqrt{\sum_{j=1}^{p}(z_{qj}-z_{ij})^2}. $$

The features’ scales affect this distance. A feature with a much larger numerical range can dominate the sum of squared differences, even if that feature is not more relevant to cell identity. Scaling or choosing a representation can address this, but the choice should be justified and applied consistently to query and reference cells.

Write \(\mathcal{N}_k(q)\) for the set of indices of the \(k\) nearest reference cells to query \(q\). The unweighted majority-vote rule is

$$ \hat{y}_q=\underset{c\in\mathcal{C}}{\arg\max}\; \sum_{i\in\mathcal{N}_k(q)} I(y_i=c), $$

where \(I(y_i=c)\) equals 1 if reference cell \(i\) has label \(c\), and 0 otherwise. Thus the method counts labels; it does not infer a new label or use cells outside the selected neighborhood in the vote. If two or more labels have the same largest count, the vote is tied. A tie rule should be set in advance, such as reporting the tie for review or applying a stated deterministic rule. An arbitrary tie-break does not create evidence favoring the selected label.

Worked Example: A Three-Neighbor Majority Vote

Suppose a query cell has coordinates \(z_q=(0,0)\) in a specified two-feature space. Five hypothetical reference cells have the coordinates and harmonized labels below. Use Euclidean distance and \(k=3\).

Reference cellCoordinatesLabel
R1(0.2, 0.1)Type T
R2(0.6, 0)Type T
R3(0, 0.8)Type M
R4(1, 1)Type M
R5(-1, 0)Type T

The distances from the query are \(d(q,R1)=\sqrt{0.2^2+0.1^2}=0.2236\), \(d(q,R2)=0.6000\), \(d(q,R3)=0.8000\), \(d(q,R4)=\sqrt{2}=1.414\), and \(d(q,R5)=1.000\). The three smallest are for R1, R2, and R3. Their labels are Type T, Type T, and Type M, so the counts are 2 for Type T and 1 for Type M. The transferred label is therefore Type T.

The conclusion is a majority among these three neighbors, not a claim that the query is certainly Type T. In particular, one of the three nearest reference cells has a different label. The vote summarizes local label composition under the chosen feature space, distance, and \(k\).

How Neighborhood Size Changes the Assignment

The choice of \(k\) controls how local the vote is. A small neighborhood is more sensitive to the closest reference cells; a larger neighborhood incorporates more reference labels and can smooth local irregularities. Neither effect guarantees better annotations. A small \(k\) can be sensitive to a mislabeled or atypical nearest cell. A large \(k\) can include cells from other parts of the feature space and overwhelm a locally appropriate label.

Worked Example: The Majority Changes as \(k\) Grows

Consider a hypothetical one-dimensional feature space with query coordinate 0. Six reference cells have the following coordinates and labels. Because the space is one-dimensional, Euclidean distance is the absolute difference from 0.

CoordinateLabelDistance to query
0.10Type T0.10
-0.12Type M0.12
0.18Type M0.18
-0.22Type M0.22
0.35Type T0.35
-0.40Type T0.40

With \(k=1\), the only neighbor is at 0.10 and the assignment is Type T. With \(k=3\), the first three neighbors have labels T, M, M, so the assignment is Type M by a 2-to-1 vote. With \(k=5\), the labels are T, M, M, M, T, so Type M wins 3 to 2. With \(k=6\), there are three cells of each label, producing a tie. This example shows why a reported assignment should include the neighborhood size: changing \(k\) changes which labels contribute to the decision.

Why the Majority Vote Is a Local Decision Rule

The majority rule can be justified as the label that makes the fewest disagreements with the observed labels in the selected neighborhood. For any proposed label \(c\), the number of neighbors whose labels differ from \(c\) is

$$ L(c)=\sum_{i\in\mathcal{N}_k(q)} I(y_i\ne c) =k-\sum_{i\in\mathcal{N}_k(q)} I(y_i=c). $$

Since \(k\) is the same for every candidate label, minimizing \(L(c)\) is equivalent to maximizing the count of neighbors labeled \(c\). Thus the majority label minimizes the number of disagreements with the observed labels among the \(k\) neighbors. This is a justification for the local voting rule, not a guarantee that the selected label is correct for the query.

This justification assumes that the chosen distance captures a useful notion of cell similarity, that the reference labels are sufficiently reliable and comparable, and that nearby reference cells provide relevant information about the query. It also assumes the query and reference vectors are expressed in the same feature space. If these conditions fail, accurately executing the vote does not rescue the biological interpretation.

Worked Example: Feature Scaling Can Reverse the Nearest Neighbor

Suppose a hypothetical query is \(z_q=(0,0)\) and there are two labeled reference cells: A at \((2,0)\), labeled Type A, and B at \((0,1)\), labeled Type B. In the unscaled two-feature space, the Euclidean distances are \(d(q,A)=\sqrt{2^2+0^2}=2\) and \(d(q,B)=\sqrt{0^2+1^2}=1\). With \(k=1\), the transferred label is Type B.

Now suppose the first feature is multiplied by 0.1 for every cell, while the second feature is unchanged. The transformed query is still \((0,0)\); transformed A is \((0.2,0)\), and transformed B is \((0,1)\). Their distances are 0.2 and 1, respectively. The nearest cell is now A, and the \(k=1\) assignment becomes Type A.

The arithmetic is straightforward, but the interpretation is important: a distance-based label can change when features are rescaled. Scaling should reflect a defensible representation of similarity, not be selected after seeing which label it produces.

Diagnostics, Alternatives, and Limitations

Before interpreting transferred labels, inspect the conditions that make the neighborhood meaningful. Check that the reference and query use matching features and preprocessing; identify features whose scales could dominate distance; and inspect whether the nearest cells are close in a meaningful sense. A query whose nearest reference cells are all far away from it in the chosen representation is being assigned by relative proximity, even if those cells are not close in an absolute sense. The method always finds neighbors when reference cells exist; this fact alone does not establish that the query is well represented.

Inspect the labels and distances in each selected neighborhood, and repeat the assignment for a few reasonable values of \(k\). If small changes in \(k\) produce different labels, the assignment is sensitive to neighborhood size. If a label wins by only one vote, that local majority is weaker than a clear majority in the same-sized neighborhood, but the vote count is not itself a calibrated probability of correctness. Investigate whether disagreements align with known related cell types, a batch or donor pattern, or a feature-space boundary. These checks diagnose potential problems; they do not turn the label transfer into a causal claim or a definitive biological identity.

A distance-weighted vote is one alternative: nearby neighbors can contribute more to a label’s score than more distant members of the \(k\)-neighborhood. For example, a rule can sum weights \(w_i\) for each label, with larger weights assigned to smaller distances. This can reduce the influence of a distant neighbor at the edge of the neighborhood, but it introduces a weight choice and does not fix an inappropriate feature space or poor reference labels. A nearest-centroid rule is another alternative: it compares a query with class summaries rather than individual reference cells. That can be simpler, but may be unsuitable when a cell type occupies several separated regions of the feature space. Marker-based evidence is also a distinct source of annotation support; neighborhood agreement and marker evidence need not coincide.

Nearest-neighbor transfer has further limitations. It can only vote for labels represented among the selected reference cells, and an unbalanced local composition can dominate the vote even when the query is biologically ambiguous. The vote does not account automatically for uncertainty in reference annotations, dependencies among cells from the same sample, or errors in the feature representation. Neighbors are also not necessarily independent pieces of evidence: many nearby reference cells may be highly similar or come from related samples. Treat the result as a procedure-dependent assignment, report the feature space, distance, \(k\), and tie handling, and use independent validation appropriate to the intended application.

Common Mistakes

  • Treating the label as a certainty. A majority vote is the label with the largest local count, not proof that the query has that identity. Report the neighborhood composition when it matters.
  • Choosing \(k\) without checking sensitivity. The example above changes from Type T to Type M as \(k\) increases. State \(k\) and assess whether plausible choices materially change the assignment.
  • Ignoring feature scales. A large-range feature can dominate Euclidean distance. Inspect and justify the representation and scaling used for both data sets.
  • Assuming the nearest available cell is close enough. Nearest is a relative ranking. If even the closest reference cells are far from the query, the vote may be poorly supported.
  • Breaking ties silently. A tie has no majority label. State the tie policy rather than presenting an arbitrary selection as an ordinary vote.
Key takeaway: Nearest-neighbor label transfer assigns a query the most frequent reference label in a specified local neighborhood. Its meaning depends on the feature space, distance, neighborhood size, reference labels, and local class composition; those choices should accompany the assignment.

Check Your Understanding

Work each problem, then compare with the solutions below.

  1. A query has three nearest reference labels M, M, and T. What label does unweighted \(k=3\) transfer assign, and what does that vote establish?
  2. In a one-dimensional space, the query is at 0 and reference cells at \(-0.1\), \(0.3\), and \(-0.5\) have labels A, B, and B, respectively. Find the assignment for \(k=1\) and \(k=3\).
  3. Two labels receive two votes each among four neighbors. Is there a majority label? Give one defensible way to handle the result.
  4. Why can multiplying one feature by a constant change the transferred label under Euclidean distance?

Solutions. 1. The assignment is M, because it receives two of the three votes. This establishes only that M is the local majority under the specified neighborhood, not that the query is certainly M. 2. The distances are 0.1, 0.3, and 0.5. For \(k=1\), the nearest label is A. For \(k=3\), A has one vote and B has two, so the assignment is B. 3. There is no majority: both labels have the same largest count. One defensible policy is to report a tie for review; any other tie rule should be stated in advance. 4. Euclidean distance sums squared feature differences, so rescaling one feature changes its contribution to the distance and can change the ordering of reference cells. If that ordering changes, the neighborhood and its vote can change as well.

Clinical Biostats