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

时间:2019-07-23 18:39:14

标签: python python-3.x numpy

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

1 个答案:

答案 0 :(得分:2)

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

.custom-nav{
  color: #ffffff;
  font-size: larger;
  font-family: Impact;

  a {
    color: white;
    font-family: Calibri;
    font-size: 17px;
  }

  li {
    margin-left: 20px;
  }

}

.dropdown-item:hover{
  color: black;
}

因此,您可以通过以下方式使他们同意:

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.