MCPcopy Create free account
hub / github.com/numpy/numpy / var

Method var

numpy/ma/core.py:5398–5459  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 15

stdMethod · 0.95
test_attributesMethod · 0.45
test_ticket_1434Method · 0.45
_varFunction · 0.45
test_python_typeMethod · 0.45
test_var_axis_errorMethod · 0.45
test_var_whereMethod · 0.45
test_subclassMethod · 0.45
test_varMethod · 0.45
test_basicMethod · 0.45
test_scalarsMethod · 0.45

Calls 12

countMethod · 0.95
meanMethod · 0.95
iscomplexobjFunction · 0.90
divideFunction · 0.85
mask_orFunction · 0.85
getmaskFunction · 0.85
MaskErrorClass · 0.85
__setmask__Method · 0.80
_update_fromMethod · 0.80
viewMethod · 0.45
sumMethod · 0.45
allMethod · 0.45

Tested by 15

test_attributesMethod · 0.36
test_ticket_1434Method · 0.36
_varFunction · 0.36
test_python_typeMethod · 0.36
test_var_axis_errorMethod · 0.36
test_var_whereMethod · 0.36
test_subclassMethod · 0.36
test_varMethod · 0.36
test_basicMethod · 0.36
test_scalarsMethod · 0.36
test_ddof1Method · 0.36