Estimate the covariance matrix. Except for the handling of missing data this function does the same as `numpy.cov`. For more details and examples, see `numpy.cov`. By default, masked values are recognized as such. If `x` and `y` have the same shape, a common mask is allocated:
(x, y=None, rowvar=True, bias=False, allow_masked=True, ddof=None)
| 1578 | |
| 1579 | |
| 1580 | def cov(x, y=None, rowvar=True, bias=False, allow_masked=True, ddof=None): |
| 1581 | """ |
| 1582 | Estimate the covariance matrix. |
| 1583 | |
| 1584 | Except for the handling of missing data this function does the same as |
| 1585 | `numpy.cov`. For more details and examples, see `numpy.cov`. |
| 1586 | |
| 1587 | By default, masked values are recognized as such. If `x` and `y` have the |
| 1588 | same shape, a common mask is allocated: if ``x[i,j]`` is masked, then |
| 1589 | ``y[i,j]`` will also be masked. |
| 1590 | Setting `allow_masked` to False will raise an exception if values are |
| 1591 | missing in either of the input arrays. |
| 1592 | |
| 1593 | Parameters |
| 1594 | ---------- |
| 1595 | x : array_like |
| 1596 | A 1-D or 2-D array containing multiple variables and observations. |
| 1597 | Each row of `x` represents a variable, and each column a single |
| 1598 | observation of all those variables. Also see `rowvar` below. |
| 1599 | y : array_like, optional |
| 1600 | An additional set of variables and observations. `y` has the same |
| 1601 | shape as `x`. |
| 1602 | rowvar : bool, optional |
| 1603 | If `rowvar` is True (default), then each row represents a |
| 1604 | variable, with observations in the columns. Otherwise, the relationship |
| 1605 | is transposed: each column represents a variable, while the rows |
| 1606 | contain observations. |
| 1607 | bias : bool, optional |
| 1608 | Default normalization (False) is by ``(N-1)``, where ``N`` is the |
| 1609 | number of observations given (unbiased estimate). If `bias` is True, |
| 1610 | then normalization is by ``N``. This keyword can be overridden by |
| 1611 | the keyword ``ddof`` in numpy versions >= 1.5. |
| 1612 | allow_masked : bool, optional |
| 1613 | If True, masked values are propagated pair-wise: if a value is masked |
| 1614 | in `x`, the corresponding value is masked in `y`. |
| 1615 | If False, raises a `ValueError` exception when some values are missing. |
| 1616 | ddof : {None, int}, optional |
| 1617 | If not ``None`` normalization is by ``(N - ddof)``, where ``N`` is |
| 1618 | the number of observations; this overrides the value implied by |
| 1619 | ``bias``. The default value is ``None``. |
| 1620 | |
| 1621 | Raises |
| 1622 | ------ |
| 1623 | ValueError |
| 1624 | Raised if some values are missing and `allow_masked` is False. |
| 1625 | |
| 1626 | See Also |
| 1627 | -------- |
| 1628 | numpy.cov |
| 1629 | |
| 1630 | Examples |
| 1631 | -------- |
| 1632 | >>> import numpy as np |
| 1633 | >>> x = np.ma.array([[0, 1], [1, 1]], mask=[0, 1, 0, 1]) |
| 1634 | >>> y = np.ma.array([[1, 0], [0, 1]], mask=[0, 0, 1, 1]) |
| 1635 | >>> np.ma.cov(x, y) |
| 1636 | masked_array( |
| 1637 | data=[[--, --, --, --], |