Efficient columnwise correlation coefficient calculation
问题 Original question I am correlating row P of size n to every column of matrix O of size n×m. I crafted the following code: import numpy as np def ColumnWiseCorrcoef(O, P): n = P.size DO = O - (np.sum(O, 0) / np.double(n)) DP = P - (np.sum(P) / np.double(n)) return np.dot(DP, DO) / np.sqrt(np.sum(DO ** 2, 0) * np.sum(DP ** 2)) It is more efficient than the naive approach: def ColumnWiseCorrcoefNaive(O, P): return np.corrcoef(P,O.T)[0,1:O[0].size+1] Here are the timings I get with numpy-1.7.1