Calculating Covariance with Python and Numpy

后端 未结 2 1248
别那么骄傲
别那么骄傲 2020-12-08 00:19

I am trying to figure out how to calculate covariance with the Python Numpy function cov. When I pass it two one-dimentional arrays, I get back a 2x2 matrix of results. I

相关标签:
2条回答
  • 2020-12-08 00:45

    Thanks to unutbu for the explanation. By default numpy.cov calculates the sample covariance. To obtain the population covariance you can specify normalisation by the total N samples like this:

    Covariance = numpy.cov(a, b, bias=True)[0][1]
    print(Covariance)
    

    or like this:

    Covariance = numpy.cov(a, b, ddof=0)[0][1]
    print(Covariance)
    
    0 讨论(0)
  • 2020-12-08 00:54

    When a and b are 1-dimensional sequences, numpy.cov(a,b)[0][1] is equivalent to your cov(a,b).

    The 2x2 array returned by np.cov(a,b) has elements equal to

    cov(a,a)  cov(a,b)
    
    cov(a,b)  cov(b,b)
    

    (where, again, cov is the function you defined above.)

    0 讨论(0)
提交回复
热议问题