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)
| 5396 | return self - expand_dims(m, axis) |
| 5397 | |
| 5398 | def var(self, axis=None, dtype=None, out=None, ddof=0, |
| 5399 | keepdims=np._NoValue): |
| 5400 | """ |
| 5401 | Returns the variance of the array elements along given axis. |
| 5402 | |
| 5403 | Masked entries are ignored, and result elements which are not |
| 5404 | finite will be masked. |
| 5405 | |
| 5406 | Refer to `numpy.var` for full documentation. |
| 5407 | |
| 5408 | See Also |
| 5409 | -------- |
| 5410 | numpy.ndarray.var : corresponding function for ndarrays |
| 5411 | numpy.var : Equivalent function |
| 5412 | """ |
| 5413 | kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims} |
| 5414 | |
| 5415 | # Easy case: nomask, business as usual |
| 5416 | if self._mask is nomask: |
| 5417 | ret = super().var(axis=axis, dtype=dtype, out=out, ddof=ddof, |
| 5418 | **kwargs)[()] |
| 5419 | if out is not None: |
| 5420 | if isinstance(out, MaskedArray): |
| 5421 | out.__setmask__(nomask) |
| 5422 | return out |
| 5423 | return ret |
| 5424 | |
| 5425 | # Some data are masked, yay! |
| 5426 | cnt = self.count(axis=axis, **kwargs) - ddof |
| 5427 | danom = self - self.mean(axis, dtype, keepdims=True) |
| 5428 | if iscomplexobj(self): |
| 5429 | danom = umath.absolute(danom) ** 2 |
| 5430 | else: |
| 5431 | danom *= danom |
| 5432 | dvar = divide(danom.sum(axis, **kwargs), cnt).view(type(self)) |
| 5433 | # Apply the mask if it's not a scalar |
| 5434 | if dvar.ndim: |
| 5435 | dvar._mask = mask_or(self._mask.all(axis, **kwargs), (cnt <= 0)) |
| 5436 | dvar._update_from(self) |
| 5437 | elif getmask(dvar): |
| 5438 | # Make sure that masked is returned when the scalar is masked. |
| 5439 | dvar = masked |
| 5440 | if out is not None: |
| 5441 | if isinstance(out, MaskedArray): |
| 5442 | out.flat = 0 |
| 5443 | out.__setmask__(True) |
| 5444 | elif out.dtype.kind in 'biu': |
| 5445 | errmsg = "Masked data information would be lost in one or "\ |
| 5446 | "more location." |
| 5447 | raise MaskError(errmsg) |
| 5448 | else: |
| 5449 | out.flat = np.nan |
| 5450 | return out |
| 5451 | # In case with have an explicit output |
| 5452 | if out is not None: |
| 5453 | # Set the data |
| 5454 | out.flat = dvar |
| 5455 | # Set the mask if needed |