Skip to content

numpy/mask-per-label

Pack Severity Fix Versions
NumPy warning none, edit by hand All versions

Comparing an array with one label per pass makes one pass for every label. Compare with all labels at once, which gives one row per label.

cleared = np.array([np.where(lot_of == lot, weights, 0.0) for lot in range(lots)])
cleared = (lot_of == np.arange(lots)[:, None]) * weights

The rule reports np.array([... a == k ... for k in ...]), and a for loop over labels that selects with values[labels == k].

xpd has no automatic fix, so make the change by hand.

The comprehension compares the same array with one label per pass. Broadcasting compares it with all labels at once and gives one row per label, and on NumPy 2.5.5 the two forms were equal. For the loop, pd.DataFrame(values).groupby(labels).median() reduces every group in one pass. When the body fits a model or solves a problem for each label, loop over frame.groupby(labels) so that the frame is split once.

This compares $ARRAY with one label per pass. Compare it with all labels at once, as in $ARRAY == np.arange(n)[:, None], which gives one row per label.

See all rules in the NumPy pack.