Returns the variance of the array elements along given axis. Masked entries are ignored, and result elements which are not finite will be masked. Refer to `numpy.var` for full documentation. See Also -------- numpy.ndarray.var : correspondi
(self, axis=None, dtype=None, out=None, ddof=0,
keepdims=np._NoValue, mean=np._NoValue)
| 5468 | return self - expand_dims(m, axis) |
| 5469 | |
| 5470 | def var(self, axis=None, dtype=None, out=None, ddof=0, |
| 5471 | keepdims=np._NoValue, mean=np._NoValue): |
| 5472 | """ |
| 5473 | Returns the variance of the array elements along given axis. |
| 5474 | |
| 5475 | Masked entries are ignored, and result elements which are not |
| 5476 | finite will be masked. |
| 5477 | |
| 5478 | Refer to `numpy.var` for full documentation. |
| 5479 | |
| 5480 | See Also |
| 5481 | -------- |
| 5482 | numpy.ndarray.var : corresponding function for ndarrays |
| 5483 | numpy.var : Equivalent function |
| 5484 | """ |
| 5485 | kwargs = {} |
| 5486 | |
| 5487 | if keepdims is not np._NoValue: |
| 5488 | kwargs['keepdims'] = keepdims |
| 5489 | |
| 5490 | # Easy case: nomask, business as usual |
| 5491 | if self._mask is nomask: |
| 5492 | |
| 5493 | if mean is not np._NoValue: |
| 5494 | kwargs['mean'] = mean |
| 5495 | |
| 5496 | ret = super().var(axis=axis, dtype=dtype, out=out, ddof=ddof, |
| 5497 | **kwargs)[()] |
| 5498 | if out is not None: |
| 5499 | if isinstance(out, MaskedArray): |
| 5500 | out.__setmask__(nomask) |
| 5501 | return out |
| 5502 | return ret |
| 5503 | |
| 5504 | # Some data are masked, yay! |
| 5505 | cnt = self.count(axis=axis, **kwargs) - ddof |
| 5506 | |
| 5507 | if mean is not np._NoValue: |
| 5508 | danom = self - mean |
| 5509 | else: |
| 5510 | danom = self - self.mean(axis, dtype, keepdims=True) |
| 5511 | |
| 5512 | if iscomplexobj(self): |
| 5513 | danom = umath.absolute(danom) ** 2 |
| 5514 | else: |
| 5515 | danom *= danom |
| 5516 | dvar = divide(danom.sum(axis, **kwargs), cnt).view(type(self)) |
| 5517 | # Apply the mask if it's not a scalar |
| 5518 | if dvar.ndim: |
| 5519 | dvar._mask = mask_or(self._mask.all(axis, **kwargs), (cnt <= 0)) |
| 5520 | dvar._update_from(self) |
| 5521 | elif getmask(dvar): |
| 5522 | # Make sure that masked is returned when the scalar is masked. |
| 5523 | dvar = masked |
| 5524 | if out is not None: |
| 5525 | if isinstance(out, MaskedArray): |
| 5526 | out.flat = 0 |
| 5527 | out.__setmask__(True) |