scipy/per-value-scipy-call
| Rule | per-value-scipy-call |
|---|---|
| Pack | SciPy |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
Calling a SciPy function that takes whole arrays once for every value was 7 to 23 times slower. Pass the whole array to the function.
p = np.array([stats.norm.cdf(v) for v in x])p = stats.norm.cdf(x)What the rule reports
Section titled “What the rule reports”The rule reports a comprehension or generator whose element is one SciPy call with the loop name
as an argument. A filter with if, a second for, and an element that does more than the call
are left alone.
| Element of the comprehension | One call on the array was faster by |
|---|---|
a scipy.special ufunc |
7 to 23 times |
a scipy.stats distribution method, such as norm.cdf |
92 to 730 times |
a distribution frozen from the loop name, such as norm(m, 2.0).pdf(x) |
16 to 17 times |
stats.scoreatpercentile(x, p) over 1,000 percentiles, which sorts the data on every call |
2.1 to 3.2 times |
How to fix it
Section titled “How to fix it”xpd has no automatic fix, so make the change by hand.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
This calls $F once per value of $IT, and $F is a NumPy ufunc that takes the whole array. Write $F with np.asarray($IT) in place of $X, which ran 7 to 23 times faster on 1,000,000 values.Browse the other rules of the SciPy pack.