ML Foundations

Preserve first-seen label order with NumPy unique

Reorder unique labels and remap inverse indices without breaking reconstruction.

Example results for Preserve first-seen label order with NumPy unique. sorted labels: 10, 20, 30
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

Sorted labels are not encounter order

A label encoder may need IDs assigned in the order labels first appear. By default, NumPy unique returns sorted unique values. Its inverse indices reconstruct the input by indexing that sorted array. Sorting or rearranging the values alone changes the meaning of those indices. [1]

The desired policy here is first occurrence in a single one-dimensional integer sequence. This differs from counting sparse label IDs: the problem is keeping an encoding and its decoding table aligned after reordering.

References: [1], [2]

Reorder both the table and its indices

Run python examples/numpy_unique_first_seen.py. The return_index output identifies the first position of each sorted unique label. Sorting those positions gives a permutation from first-seen positions to sorted-label positions. Applying that permutation to the values creates the first-seen table. [1, 2]

The inverse array still refers to the old sorted table. Build the reverse mapping by assigning new positions into remap at the old positions. Indexing remap with the original inverse produces the corresponding IDs in first-seen order. The assertion checks that indexing the new table with those IDs reconstructs the original sequence.

This matters when the table is saved alongside encoded data: persist both the first-seen table and the matching remapped IDs. A table reordered without remapping can silently decode to different labels.

Executed with Python 3.12.14 and local NumPy; stdout and checksum are in private evidence.

import numpy as np

labels = np.array([30, 10, 30, 20, 10])
values, first, inverse = np.unique(labels, return_index=True, return_inverse=True)
order = np.argsort(first)
first_seen = values[order]
remap = np.empty_like(order)
remap[order] = np.arange(len(order))
first_seen_inverse = remap[inverse]
assert np.array_equal(first_seen[first_seen_inverse], labels)
assert first_seen.tolist() == [30, 10, 20]
assert first_seen_inverse.tolist() == [0, 1, 0, 2, 1]
print("sorted labels:", values.tolist())
print("first seen:", first_seen.tolist())
print("remapped inverse:", first_seen_inverse.tolist())
print("reconstruction:", first_seen[first_seen_inverse].tolist())

Recorded output

sorted labels: [10, 20, 30]
first seen: [30, 10, 20]
remapped inverse: [0, 1, 0, 2, 1]
reconstruction: [30, 10, 30, 20, 10]

References: [1], [2]

Know the scope of the mapping

The example uses one-dimensional integer data and a single fixed input order. A different input order can give different assigned IDs even when it contains the same labels. Reuse a stored table for later batches when IDs must be consistent.

The fixture does not define treatment for new labels in later data, multidimensional axis handling, or NaN equality. Those choices need their own contract. It makes no performance claim. [1]

References: [1], [2]

Sources

  1. NumPy unique
  2. NumPy argsort