Skip to content

Instantly share code, notes, and snippets.

@dengemann
Created July 25, 2015 18:36
Show Gist options
  • Save dengemann/65b5f67ec1ae51b1d9d4 to your computer and use it in GitHub Desktop.
Save dengemann/65b5f67ec1ae51b1d9d4 to your computer and use it in GitHub Desktop.
# Author: [email protected]
# License: simplified BSD (3 clause)
# Note: code is based on scipy.stats.pearsonr
from scipy import stats
def compute_corr(x, y):
x = np.asarray(x)
y = np.asarray(y)
mx = x.mean(axis=-1)
my = y.mean(axis=-1)
xm, ym = x - mx[..., None], y - my[..., None]
r_num = np.add.reduce(xm * ym, axis=-1)
r_den = np.sqrt(stats.ss(xm, axis=-1) * stats.ss(ym, axis=-1))
r = r_num / r_den
return r
@endrebak
Copy link

ss has been removed from scipy, but it just did this:

def ss(a, axis):
    return np.sum(a * a, axis=axis)

Thanks for the function btw :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment