scipy/callable-metric
| Rule | callable-metric |
|---|---|
| Pack | SciPy |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
Passing a metric function such as euclidean to cdist or pdist calls it once for every pair in Python. Pass the metric's name as a string, which runs it in C and was over 1,000 times faster.
D = cdist(X, Y, distance.euclidean)D = cdist(X, Y, "euclidean")What the rule reports
Section titled “What the rule reports”The rule reports a metric function of scipy.spatial.distance passed to cdist or pdist as
the metric, by position or as metric=. The metric's name as a string runs the same metric in C, and
on 1,000 by 1,000 points it ran 1,100 to 2,100 times faster.
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 the metric's name as a string. cosine and correlation differ in the last bits, and complex input is cast to float.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
Passing $F as a function makes $CD call it once per pair in Python. Pass the name '$F', which runs the same metric in C and ran 1,100 to 2,100 times faster on 1,000,000 pairs.Browse the other rules of the SciPy pack.