Skip to content

numpy/compare-reductions

Rule compare-reductions
Pack NumPy
Severity error
Fix unsafe, with xpd check --fix --unsafe-fixes
Library versions every version

a.all() == b.all() compares two single booleans, so it is True for many arrays that differ. Use np.array_equal(a, b).

same = a.all() == b.all()
same = np.array_equal(a, b)

The rule reports a.all() == b.all() and np.any(a) == np.any(b), which compare two single booleans and not the arrays.

xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.

The unsafe fix writes np.array_equal(a, b).

In an assert, assert-on-arrays then advises np.testing.assert_array_equal(a, b), which shows the elements that differ.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

This compares two single booleans, not the arrays, so it is True for many arrays that differ. Write np.array_equal($A, $B).

Browse the other rules of the NumPy pack.