ML Foundations

Get row and coordinate indices from a NumPy condition

Separate rows with any match from the coordinates of every matching element.

Example results for Get row and coordinate indices from a NumPy condition. rows with a match: 0, 2
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.

A row match and a cell match answer different questions

For a two-dimensional boolean mask, ask first whether you need rows that contain at least one True value or the coordinates of every True cell. A row-level result has one index per matching row. A coordinate result has one pair per matching element, so a row with two matches appears twice. [1, 2]

Reduce over the column axis with any(mask, axis=1) to get one Boolean per row. flatnonzero converts those True positions into row indices. This avoids confusing a row-selection task with a cell-location task.

References: [1]

Keep the per-axis indices paired

nonzero returns a tuple containing one index array for each dimension. For a two-dimensional mask, the first array holds row indices and the second holds column indices. Values at the same position in those arrays describe one coordinate. Pair them by position or use argwhere, whose rows group the coordinates for each nonzero element. [1, 2]

Run python examples/numpy_condition_row_and_coordinate_indices.py. The fixture has three scores at or above 0.7, across two rows. The row result contains indices zero and two, while the coordinates are (0, 1), (2, 0), and (2, 2). The repeated row index is expected because row 2 has two qualifying cells.

Executed with Python 3.12.14; exact stdout and SHA-256 recorded in private evidence.

import numpy as np

scores = np.array([[0.2, 0.9, 0.1], [0.3, 0.4, 0.5], [0.9, 0.2, 0.7]])
mask = scores >= 0.7
row_indices = np.flatnonzero(mask.any(axis=1))
rows_by_axis, columns_by_axis = np.nonzero(mask)
coordinate_pairs = np.argwhere(mask)

print("rows with a match:", row_indices.tolist())
print("selected rows:", scores[row_indices].tolist())
print("nonzero axis arrays:", rows_by_axis.tolist(), columns_by_axis.tolist())
print("coordinate pairs:", coordinate_pairs.tolist())
print("coordinate shape:", coordinate_pairs.shape)
assert row_indices.tolist() == [0, 2]
assert coordinate_pairs.tolist() == [[0, 1], [2, 0], [2, 2]]
assert coordinate_pairs.shape == (3, 2)

Recorded output

rows with a match: [0, 2]
selected rows: [[0.2, 0.9, 0.1], [0.9, 0.2, 0.7]]
nonzero axis arrays: [0, 2, 2] [1, 0, 2]
coordinate pairs: [[0, 1], [2, 0], [2, 2]]
coordinate shape: (3, 2)

References: [1], [2]

Use the result that matches the next operation

Use the row indices to select complete records with scores[row_indices]. Use coordinate pairs when an operation needs a particular cell location. Keep the per-axis arrays paired by position, and do not flatten them if the operation needs row and column separately. [1, 2]

This example covers a nonempty two-dimensional numeric array and a threshold mask. It does not cover masked arrays, scalar arrays, empty shapes or performance comparisons. The original question is historical evidence of a recurring reader problem, not evidence of a current trend or search volume.

References: [1], [2]

Sources

  1. NumPy nonzero
  2. NumPy argwhere