Last active
September 25, 2020 16:44
-
-
Save kingjr/6d16838c145e79453dfd2fa1c8d9bb81 to your computer and use it in GitHub Desktop.
scale correlate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def scale(X): | |
m = X.mean(0, keepdims=True) | |
s = X.std(0, keepdims=True) | |
s[s == 0] = 1 # avoid nan | |
X -= m | |
X /= s | |
return X | |
def correlate(X, Y): | |
if X.ndim == 1: | |
X = X[:, None] | |
if Y.ndim == 1: | |
Y = Y[:, None] | |
out = np.zeros(max([Y.shape[1], X.shape[1]])) | |
X = X - X.mean(0) | |
Y = Y - Y.mean(0) | |
SX2 = (X ** 2).sum(0) ** 0.5 | |
SY2 = (Y ** 2).sum(0) ** 0.5 | |
SXY = (X * Y).sum(0) | |
valid = (SX2 != 0) & (SY2 != 0) | |
out[valid] = SXY[valid] / (SX2[valid] * SY2[valid]) | |
return out | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment