Correlation coefficients and p values for all pairs of rows of a matrix

前端 未结 5 1199
北荒
北荒 2020-12-30 03:32

I have a matrix data with m rows and n columns. I used to compute the correlation coefficients between all pairs of rows using np.corrcoef:

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-30 04:13

    If you do not have to use pearson correlation coefficient, you can use the spearman correlation coefficient, as it returns both the correlation matrix and p-values (note that the former requires that your data is normally distributed, whereas the spearman correlation is a non-parametric measure, thus not assuming the normal distribution of your data). An example code:

    from scipy import stats
    import numpy as np
    
    data = np.array([[0, 1, -1], [0, -1, 1], [0, 1, -1]])
    print 'np.corrcoef:', np.corrcoef(data)
    cor, pval = stats.spearmanr(data.T)
    print 'stats.spearmanr - cor:\n', cor
    print 'stats.spearmanr - pval\n', pval
    

提交回复
热议问题