diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2013-06-20 20:44:54 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2013-08-12 22:33:55 -0600 |
commit | fcb0fef5c673ed0a5442b18bcd8c391907b4f9a7 (patch) | |
tree | 24726ff3fbb7a167a8fdf89ac5cb74792c9cc6e7 /numpy/core/_methods.py | |
parent | 777b6453e166df252298a47ef4f0e867614ac94a (diff) | |
download | numpy-fcb0fef5c673ed0a5442b18bcd8c391907b4f9a7.tar.gz |
MAINT: Separate nan functions into their own module.
New files lib/nanfunctions.py and lib/tests/test_nanfunctions.py are
added and both the previous and new nan functions and tests are moved
into them.
The existing nan functions moved from lib/function_base are:
nansum, nanmin, nanmax, nanargmin, nanargmax
The added nan functions moved from core/numeric are:
nanmean, nanvar, nanstd
Diffstat (limited to 'numpy/core/_methods.py')
-rw-r--r-- | numpy/core/_methods.py | 84 |
1 files changed, 1 insertions, 83 deletions
diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py index c317358e1..51731b9c2 100644 --- a/numpy/core/_methods.py +++ b/numpy/core/_methods.py @@ -7,7 +7,7 @@ from __future__ import division, absolute_import, print_function from numpy.core import multiarray as mu from numpy.core import umath as um -from numpy.core.numeric import array, asanyarray, isnan, issubdtype +from numpy.core.numeric import asanyarray, isnan, issubdtype from numpy.core import numerictypes as nt def _amax(a, axis=None, out=None, keepdims=False): @@ -63,29 +63,6 @@ def _mean(a, axis=None, dtype=None, out=None, keepdims=False): ret = ret / float(rcount) return ret -def _nanmean(a, axis=None, dtype=None, out=None, keepdims=False): - # Using array() instead of asanyarray() because the former always - # makes a copy, which is important due to the copyto() action later - arr = array(a, subok=True) - mask = isnan(arr) - - # Cast bool, unsigned int, and int to float64 - if dtype is None and (issubdtype(arr.dtype, nt.integer) or - issubdtype(arr.dtype, nt.bool_)): - ret = um.add.reduce(arr, axis=axis, dtype='f8', - out=out, keepdims=keepdims) - else: - mu.copyto(arr, 0.0, where=mask) - ret = um.add.reduce(arr, axis=axis, dtype=dtype, - out=out, keepdims=keepdims) - rcount = (~mask).sum(axis=axis) - if isinstance(ret, mu.ndarray): - ret = um.true_divide(ret, rcount, - out=ret, casting='unsafe', subok=False) - else: - ret = ret / float(rcount) - return ret - def _var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): arr = asanyarray(a) @@ -127,55 +104,6 @@ def _var(a, axis=None, dtype=None, out=None, ddof=0, return ret -def _nanvar(a, axis=None, dtype=None, out=None, ddof=0, - keepdims=False): - # Using array() instead of asanyarray() because the former always - # makes a copy, which is important due to the copyto() action later - arr = array(a, subok=True) - mask = isnan(arr) - - # First compute the mean, saving 'rcount' for reuse later - if dtype is None and (issubdtype(arr.dtype, nt.integer) or - issubdtype(arr.dtype, nt.bool_)): - arrmean = um.add.reduce(arr, axis=axis, dtype='f8', keepdims=True) - else: - mu.copyto(arr, 0.0, where=mask) - arrmean = um.add.reduce(arr, axis=axis, dtype=dtype, - keepdims=True) - rcount = (~mask).sum(axis=axis, keepdims=True) - if isinstance(arrmean, mu.ndarray): - arrmean = um.true_divide(arrmean, rcount, - out=arrmean, casting='unsafe', subok=False) - else: - arrmean = arrmean / float(rcount) - - # arr - arrmean - x = arr - arrmean - mu.copyto(x, 0.0, where=mask) - - # (arr - arrmean) ** 2 - if issubdtype(arr.dtype, nt.complex_): - x = um.multiply(x, um.conjugate(x), out=x).real - else: - x = um.multiply(x, x, out=x) - - # add.reduce((arr - arrmean) ** 2, axis) - ret = um.add.reduce(x, axis=axis, dtype=dtype, out=out, - keepdims=keepdims) - - # add.reduce((arr - arrmean) ** 2, axis) / (n - ddof) - if not keepdims and isinstance(rcount, mu.ndarray): - rcount = rcount.squeeze(axis=axis) - rcount -= ddof - if isinstance(ret, mu.ndarray): - ret = um.true_divide(ret, rcount, - out=ret, casting='unsafe', subok=False) - else: - ret = ret / float(rcount) - - return ret - - def _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof, keepdims=keepdims) @@ -187,13 +115,3 @@ def _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): return ret -def _nanstd(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False): - ret = _nanvar(a, axis=axis, dtype=dtype, out=out, ddof=ddof, - keepdims=keepdims) - - if isinstance(ret, mu.ndarray): - ret = um.sqrt(ret, out=ret) - else: - ret = um.sqrt(ret) - - return ret |