77
88import numpy as np
99
10- # This is the version in the book:
11-
10+ def all_correlations (y , X ):
11+ from scipy import spatial
12+ y = np .atleast_2d (y )
13+ sp = spatial .distance .cdist (X , y , 'correlation' )
14+ # The "correlation distance" is 1 - corr(x,y); so we invert that to obtain the correlation
15+ return 1 - sp .ravel ()
1216
17+ # This is the version in the book (1st Edition):
1318def all_correlations_book_version (bait , target ):
1419 '''
1520 corrs = all_correlations(bait, target)
@@ -21,9 +26,7 @@ def all_correlations_book_version(bait, target):
2126 for c in target ])
2227
2328# This is a faster, but harder to read, implementation:
24-
25-
26- def all_correlations (y , X ):
29+ def all_correlations_fast_no_scipy (y , X ):
2730 '''
2831 Cs = all_correlations(y, X)
2932
@@ -42,12 +45,4 @@ def all_correlations(y, X):
4245
4346 return (xy - x_ * y_ * n ) / n / xs_ / ys_
4447
45- # If you have scipy installed, then you can compute correlations with
46- # scipy.spatial.cdist:
4748
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