numpy covariance matrix

前端 未结 10 1699
半阙折子戏
半阙折子戏 2021-01-01 13:10

Suppose I have two vectors of length 25, and I want to compute their covariance matrix. I try doing this with numpy.cov, but always end up with a 2x2 matrix.



        
10条回答
  •  死守一世寂寞
    2021-01-01 13:48

    What you got (2 by 2) is more useful than 25*25. Covariance of X and Y is an off-diagonal entry in the symmetric cov_matrix.

    If you insist on (25 by 25) which I think useless, then why don't you write out the definition?

    x=np.random.normal(size=25).reshape(25,1) # to make it 2d array.
    y=np.random.normal(size=25).reshape(25,1)
    
    cov =  np.matmul(x-np.mean(x), (y-np.mean(y)).T) / len(x)
    

提交回复
热议问题