correlation matrix in python

后端 未结 4 736
孤街浪徒
孤街浪徒 2020-12-08 20:08

How do I calculate correlation matrix in python? I have an n-dimensional vector in which each element has 5 dimension. For example my vector looks like

[
 [0.1, .         


        
4条回答
  •  忘掉有多难
    2020-12-08 21:08

    As I almost missed that comment by @Anton Tarasenko, I'll provide a new answer. So given your array:

    a = np.array([[0.1, .32, .2,  0.4, 0.8], 
                 [.23, .18, .56, .61, .12], 
                 [.9,   .3,  .6,  .5,  .3],  
                 [.34, .75, .91, .19, .21]]) 
    

    If you want the correlation matrix of your dimensions (columns), which I assume, you can use numpy (note the transpose!):

    import numpy as np
    print(np.corrcoef(a.T))
    

    Or if you have it in Pandas anyhow:

    import pandas as pd
    print(pd.DataFrame(a).corr())
    

    Both print

    array([[ 1.        , -0.03783885,  0.34905716,  0.14648975, -0.34945863],
          [-0.03783885,  1.        ,  0.67888519, -0.96102583, -0.12757741],
          [ 0.34905716,  0.67888519,  1.        , -0.45104803, -0.80429469],
          [ 0.14648975, -0.96102583, -0.45104803,  1.        , -0.15132323],
          [-0.34945863, -0.12757741, -0.80429469, -0.15132323,  1.        ]])
    

提交回复
热议问题