简体   繁体   English

如何摆脱循环并使我的相关矩阵函数更有效

[英]How can I get rid of the loops and make my Correlation matrix function more efficient

I have this function that computes the correlation matrix and works as expect however I am trying to make it more efficient and get rid of the loops but I'm having trouble doing so. 我有一个计算相关矩阵并按预期工作的函数,但是我试图使其更高效并摆脱循环,但是这样做很麻烦。 My function below: 我的功能如下:

def correlation(X):
    N = X.shape[0]  # num of rows
    D = X.shape[1]  # num of cols

    covarianceMatrix = np.cov(X)  # start with covariance matrix

    # use covarianceMatrix to create size of M
    M = np.zeros([covarianceMatrix.shape[0], covarianceMatrix.shape[1]])

    for i in range(covarianceMatrix.shape[0]):
        for j in range(covarianceMatrix.shape[1]):

           corr = covarianceMatrix[i, j] / np.sqrt(np.dot(covarianceMatrix[i, i], covarianceMatrix[j, j]))
           M[i,j]  = corr

    return M

What would be a more efficient way to perform this computation using numpy and not using its built it functions such as corrcoef(). 什么是使用numpy而不使用其内置函数(例如corrcoef())执行此计算的更有效方法。

Once you have the covariance matrix you simply need to multiply by the product of the diagonal inverse square root. 一旦有了协方差矩阵,您只需要乘以对角反平方根的乘积即可。 Using bits of your code as a starting point: 以您的代码为起点:

covarianceMatrix = np.cov(X)
tmp = 1.0 / np.sqrt(np.diag(covarianceMatrix))

corr = covarianceMatrix.copy()
corr *= tmp[:, None]
corr *= tmp[None, :]

A bit more difficult if you have complex values, and you should probably clip between -1 and 1 via: 如果您具有复杂的值,则要困难一些,您可能应该通过以下方式将-1和1夹在中间:

np.clip(corr, -1, 1, out=corr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM