ML Foundations
Keep all score columns in top-k accuracy
Use the complete class label set when a test split omits a model class.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
An absent true label can still win
A model can assign the highest score to a class that happens not to appear among the true labels in a particular test split. Removing that class column changes the set of competitors and can make a wrong prediction look correct. Top-k accuracy asks whether the true label is among the highest scoring classes. [1]
For a multiclass score matrix, keep the complete ordered class list alongside the columns. The scikit-learn metric requires labels when the true labels do not include every class. Score-column order must match the specified label order. [1]
References: [1]
Compare full and filtered scores
Run python examples/top_k_accuracy_labels.py. The test labels are 0, 1 and 2, while the model has four classes. In the second row the missing class 3 scores highest, so the full model prediction is wrong. Removing its column changes that prediction to class 1 and raises the apparent accuracy from two thirds to one.
The example calculates the top-1 case with NumPy and has no tied maximum. It shows a real change in predictions, rather than a correction to model quality. NumPy argmax returns positions, which are mapped back through the complete class list. [2]
Executed with local Python and NumPy; stdout and checksum are recorded in private evidence.
import numpy as np
labels = np.array([0, 1, 2, 3])
truth = np.array([0, 1, 2])
scores = np.array([[0.7, 0.1, 0.1, 0.1], [0.1, 0.5, 0.1, 0.8], [0.1, 0.1, 0.7, 0.2]])
winners = labels[np.argmax(scores, axis=1)]
full_score = float(np.mean(winners == truth))
filtered = labels[:3][np.argmax(scores[:, :3], axis=1)]
filtered_score = float(np.mean(filtered == truth))
assert np.isclose(full_score, 2 / 3) and filtered_score == 1.0
print("labels:", labels.tolist())
print("full winners:", winners.tolist())
print("full top-1 accuracy:", round(full_score, 4))
print("filtered top-1 accuracy:", round(filtered_score, 4))
Recorded output
labels: [0, 1, 2, 3] full winners: [0, 3, 2] full top-1 accuracy: 0.6667 filtered top-1 accuracy: 1.0
Call the metric with an explicit mapping
For this multiclass fixture, pass the ordered labels 0, 1, 2 and 3 and k=1 with the complete score matrix to top_k_accuracy_score. A separate local scikit-learn check returned 0.6667. Do not invent extra columns or change their order to match a test subset. [1]
This small example is unweighted and single-label. It does not cover binary score conventions, ties, or every choice of k. Missing test examples also mean that the split cannot measure recall for the absent class. [1]
References: [1]