scipy/distance-per-pair
| Rule | distance-per-pair |
|---|---|
| Pack | SciPy |
| Severity | warning |
| Fix | none, edit by hand |
| Library versions | every version |
Calling a distance function once for every pair of points runs Python for each pair. Stack the points into arrays and call cdist or pdist once, which was about 1,000 times faster.
D = np.array([[distance.euclidean(a, b) for b in Y] for a in X])D = distance.cdist(X, Y, "euclidean")What the rule reports
Section titled “What the rule reports”The rule reports a metric function of scipy.spatial.distance, such as euclidean, cityblock
or cosine, called inside a comprehension, a generator or a loop body. On 1,000 by 1,000 points,
cdist ran 960 to 2,100 times faster.
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 pair of points. Stack the points into arrays and call cdist(XA, XB, '$F') once, or pdist(X, '$F') for the pairs within one array, which ran 960 to 2,100 times faster on 1,000,000 pairs.Browse the other rules of the SciPy pack.