Skip to content

Commit 2326b12

Browse files
committed
MIN Add a scipy based version
1 parent 2905bdf commit 2326b12

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

‎ch08/all_correlations.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,13 @@ def all_correlations(y, X):
4141
xs_ += 1e-5 # Handle zeros in x
4242

4343
return (xy - x_ * y_ * n) / n / xs_ / ys_
44+
45+
# If you have scipy installed, then you can compute correlations with
46+
# scipy.spatial.cdist:
47+
48+
def all_correlations_scipy(y, X):
49+
from scipy import spatial
50+
y = np.atleast_2d(y)
51+
sp = spatial.distance.cdist(X, y, 'correlation')
52+
# The "correlation distance" is 1 - corr(x,y); so we invert that to obtain the correlation
53+
return 1 - sp.ravel()

0 commit comments

Comments
 (0)