A robot controller often reduces several collision or anomaly scores to one candidate index. NumPy's `argmax` is convenient for that reduction, but its tie rule is part of the control contract: when the maximum appears more than once, it returns the first occurrence along the selected axis. That deterministic choice is useful only when sensor order is meaningful. If the order is an incidental transport detail, a tied alarm can be routed to the wrong diagnostic owner even though the score itself is correct.
This article uses small invented score arrays to expose the rule. The numbers are a software fixture, not a collision probability or a production safety threshold. The goal is to preserve the score, the selected axis, the tie set, and the policy used after selection.
Start with a two-row score table
Suppose four channels report a dimensionless anomaly score for two time windows:
scores = np.array([
[0.82, 0.95, 0.95, 0.77],
[0.95, 0.95, 0.88, 0.91],
])
Columns are channel IDs in the documented order C0, C1, C2, and C3. Row zero has a tie between C1 and C2. Row one has a tie between C0 and C1. The values are deliberately repeated so the selection rule is visible.
The corresponding maximum values are:
np.max(scores, axis=1) -> [0.95, 0.95]
np.argmax(scores, axis=1) -> [1, 0]
The maxima are correct, but each returned index is the first maximum in its row. A later equal value does not replace the earlier index. This is the behavior documented for `numpy.argmax`, and it remains true even when the reduction is performed across a chosen axis rather than on a flattened array.
Why first occurrence is not a safety policy
`argmax` answers an indexing question: where is a maximum located? It does not know whether C1 is safer to inspect than C2, whether a lower channel ID has better calibration, or whether tied channels should trigger a group response. Treating the returned index as a complete alarm policy silently couples business meaning to array order.
A robust pipeline keeps the two stages separate. First compute the maximum score. Then compute the tie set with an explicit comparison:
row = scores[0]
best = np.max(row)
tied = np.flatnonzero(row == best) # [1, 2]
The tie set is evidence that two channels share the same score. The controller can then apply a declared rule: notify both owners, choose the channel with the freshest timestamp, or defer to a calibrated priority table. If the product requirement truly is “first channel wins,” record that requirement next to the channel ordering rather than letting it remain an undocumented side effect.
Axis and flattening mistakes
Calling `np.argmax(scores)` without an axis flattens the matrix in row-major order and returns a single index. In this fixture the flattened index is 1, which points to row zero, channel C1. That number is not a channel ID by itself. Use `np.unravel_index` with the original shape when a flattened reduction is intentional, or select `axis=1` to obtain one channel index per row.
For batched telemetry, `keepdims=True` retains the reduced dimension. The result of `np.argmax(scores, axis=1, keepdims=True)` has shape (2, 1) and values [[1], [0]]. That shape can broadcast against the score table when gathering the winning values. The shape is useful, but it does not alter tie semantics.
Keep values and indices together
A reviewable alarm record should store the row or timestamp, the maximum score, the selected index, the full tie set, and the array-order version. A later schema change that reorders channels can then be detected. If the record stores only “winner = 1,” a reader cannot tell whether C1 won alone or tied with C2, and cannot know whether the index was flattened.
The compact fixture check is:
values = np.max(scores, axis=1)
winners = np.argmax(scores, axis=1)
ties0 = np.flatnonzero(scores[0] == values[0])
ties1 = np.flatnonzero(scores[1] == values[1])
assert np.array_equal(values, [0.95, 0.95])
assert np.array_equal(winners, [1, 0])
assert np.array_equal(ties0, [1, 2])
assert np.array_equal(ties1, [0, 1])
The assertions distinguish the numeric reduction from the tie policy. They also protect against a future change that accidentally calls `argmax` on the flattened table or changes channel order.
What to do with NaN scores
`argmax` and ordinary maximum reductions need an explicit missing-data policy. A NaN can affect a reduction in ways that are inappropriate for alarm routing. If missing scores are expected, decide whether they should be imputed, masked, or excluded, and test that decision separately. `numpy.nanargmax` is a different function with its own contract: it ignores NaNs but raises `ValueError` for an all-NaN slice, and it warns that slices containing only NaNs and negative infinity are not trustworthy. Do not switch functions silently; record the missing-data rule in the article or telemetry schema.
Tie behavior is deterministic, which is valuable for replay and auditing. Determinism does not make the default rule semantically correct. The safe pattern is to preserve the original score order, compute the maximum and first index for reproducibility, enumerate every equal maximum, and then apply a domain rule that is visible in the record. In a robot collision monitor, that separation turns an incidental array detail into a deliberate, reviewable decision.
When scores are close but not exactly equal, use a tolerance policy separately from the exact-tie policy. Floating-point calculations can produce 0.9500000 and 0.9499999 for channels that are practically indistinguishable. Replacing equality with `np.isclose` can create a near-tie set, but the tolerance must be tied to the score scale and documented. Do not let a display rounding rule decide the controller's winner: two values printed as 0.95 may still differ in the stored array. A useful record keeps the raw values, the display precision, and the tolerance used to group near-equal candidates.
The same distinction applies when scores are aggregated over time. If a windowed maximum returns an index inside each window, map that local index back to the original timestamp before publishing an alarm. A row index identifies a position in the current array, not necessarily a physical sensor or a global time. Preserve the channel lookup table and window offset so a replay can reconstruct why a particular owner was notified.
Sources
[1] NumPy, numpy.argmax: https://numpy.org/doc/stable/reference/generated/numpy.argmax.html
[2] NumPy, numpy.max: https://numpy.org/doc/stable/reference/generated/numpy.max.html