numpy/reduce-last
| Rule | reduce-last |
|---|---|
| Pack | NumPy |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
np.cumsum(x)[-1] builds every running total to read the last one, which was 8 times slower and adds rounding. Call np.sum(x).
total = np.cumsum(amounts)[-1]total = np.sum(amounts)What the rule reports
Section titled “What the rule reports”The rule reports np.cumsum(x)[-1] and np.cumprod(x)[-1].
How to fix it
Section titled “How to fix it”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.sum(x), np.prod(x).
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
This builds every running total to read the last one, which ran 8 times slower on 1,000,000 values and adds up more rounding. Write $NP.sum($X).Browse the other rules of the NumPy pack.