Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么np.var(x)和np.cov(x,y)给了我不同的值?_Python_Python 3.x_Numpy - Fatal编程技术网

Python 为什么np.var(x)和np.cov(x,y)给了我不同的值?

Python 为什么np.var(x)和np.cov(x,y)给了我不同的值?,python,python-3.x,numpy,Python,Python 3.x,Numpy,我是Python新手,想知道为什么np.var(x)给出的答案与np.cov(x,y)输出中的cov(x,x)值不同。它们不应该是一样的吗?我理解这和偏见或ddof有关,与正常化有关,但我不确定这意味着什么,也找不到任何具体回答我问题的资源。希望有人能帮忙 在numpy中,cov默认为“增量自由度”1,而var默认为ddof 0。从注释到numpy.var Notes ----- The variance is the average of the squared deviations from

我是Python新手,想知道为什么np.var(x)给出的答案与np.cov(x,y)输出中的cov(x,x)值不同。它们不应该是一样的吗?我理解这和偏见或ddof有关,与正常化有关,但我不确定这意味着什么,也找不到任何具体回答我问题的资源。希望有人能帮忙

在numpy中,cov默认为“增量自由度”1,而var默认为ddof 0。从注释到numpy.var

Notes
-----
The variance is the average of the squared deviations from the mean,
i.e.,  ``var = mean(abs(x - x.mean())**2)``.

The mean is normally calculated as ``x.sum() / N``, where ``N = len(x)``.
If, however, `ddof` is specified, the divisor ``N - ddof`` is used
instead.  In standard statistical practice, ``ddof=1`` provides an
unbiased estimator of the variance of a hypothetical infinite population.
``ddof=0`` provides a maximum likelihood estimate of the variance for
normally distributed variables.
因此,您可以通过以下方式让他们同意:

In [69]: cov(x,x)#defaulting to ddof=1
Out[69]: 
array([[ 0.5,  0.5],
       [ 0.5,  0.5]])

In [70]: x.var(ddof=1)
Out[70]: 0.5

In [71]: cov(x,x,ddof=0)
Out[71]: 
array([[ 0.25,  0.25],
       [ 0.25,  0.25]])

In [72]: x.var()#defaulting to ddof=0
Out[72]: 0.25

参考统计手册