Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ENH: add mean keyword to std and var#24126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from19 commits
Commits
Show all changes
24 commits
Select commitHold shift + click to select a range
5d07178
Add mean keyword to std and var functions.
RonaldAJJul 5, 2023
3ba2ccc
Add releae note for mean keyword to std and var functions.
RonaldAJJul 5, 2023
d52c0b7
Update release note with PR number
RonaldAJJul 5, 2023
3c9e4d3
Address lint issue.
RonaldAJJul 5, 2023
f78e793
Align nan signatures with new signatures.
RonaldAJJul 5, 2023
3bb28dd
Address lint issue.
RonaldAJJul 5, 2023
13161f1
Correct version numbers on keywords.
RonaldAJJul 6, 2023
d93630d
Put backticks on keyword argument in documentation string.
RonaldAJJul 6, 2023
6e966a1
Cleanuup assert statements in tests
RonaldAJJul 6, 2023
fe32fd1
Move comparison of in and out arrays closer to the function call.
RonaldAJJul 6, 2023
a15af41
Remove clutter from example code in release note.
RonaldAJJul 6, 2023
1a10e70
Add test for nanstd and fix error in nanvar
RonaldAJJul 6, 2023
ef7484a
haqndle "mean" keyword for var and std on MaskedArrays.
RonaldAJJul 6, 2023
ccc5258
Address lint issues.
RonaldAJJul 6, 2023
e150370
update the dispatchers according to suggestions by Marten van Kerkwijk:
RonaldAJJul 6, 2023
bbdeed4
Move and adjust example from release note to doc-strings. Reflow doc-…
RonaldAJJul 6, 2023
201034a
Improve doc-string. Shorter sentences and add type and label mean arg…
RonaldAJJul 6, 2023
71998c8
Remove some of these pesky trailing white spaces
RonaldAJJul 6, 2023
0f553ff
Make extra white lines more consistent.
RonaldAJJul 6, 2023
aa861b3
Make sure code examples execute without Jupyter magic.
RonaldAJJul 6, 2023
020257a
Fold lines to pass linter.
RonaldAJJul 6, 2023
f8d8399
Update doc-string nanstd and nanvar.
RonaldAJJul 6, 2023
4f5cc4c
Try to satisfy linter and apple requirements at the same time. Making…
RonaldAJJul 6, 2023
b891190
Make doctest skip resource dependent output
RonaldAJJul 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletionsdoc/release/upcoming_changes/24126.change.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
Add mean keyword to var and std function
----------------------------------------
Often when the standard deviation is needed the mean is also needed. The same
holds for the variance and the mean. Until now the mean is then calculated twice,
the change introduced here for the var and std functions allows for passing in a
precalculated mean as an keyword argument. See the doc-strings for details and an
example illustrating the speed-up.
47 changes: 25 additions & 22 deletionsnumpy/core/_methods.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -134,7 +134,7 @@ def _mean(a, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
return ret

def _var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *,
where=True):
where=True, mean=None):
arr = asanyarray(a)

rcount = _count_reduce_items(arr, axis, keepdims=keepdims, where=where)
Expand All@@ -147,26 +147,29 @@ def _var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *,
if dtype is None and issubclass(arr.dtype.type, (nt.integer, nt.bool_)):
dtype = mu.dtype('f8')

# Compute the mean.
# Note that if dtype is not of inexact type then arraymean will
# not be either.
arrmean = umr_sum(arr, axis, dtype, keepdims=True, where=where)
# The shape of rcount has to match arrmean to not change the shape of out
# in broadcasting. Otherwise, it cannot be stored back to arrmean.
if rcount.ndim == 0:
# fast-path for default case when where is True
div = rcount
if mean is not None:
arrmean = mean
else:
# matching rcount to arrmean when where is specified as array
div = rcount.reshape(arrmean.shape)
if isinstance(arrmean, mu.ndarray):
with _no_nep50_warning():
arrmean = um.true_divide(arrmean, div, out=arrmean,
casting='unsafe', subok=False)
elif hasattr(arrmean, "dtype"):
arrmean = arrmean.dtype.type(arrmean / rcount)
else:
arrmean = arrmean / rcount
# Compute the mean.
# Note that if dtype is not of inexact type then arraymean will
# not be either.
arrmean = umr_sum(arr, axis, dtype, keepdims=True, where=where)
# The shape of rcount has to match arrmean to not change the shape of
# out in broadcasting. Otherwise, it cannot be stored back to arrmean.
if rcount.ndim == 0:
# fast-path for default case when where is True
div = rcount
else:
# matching rcount to arrmean when where is specified as array
div = rcount.reshape(arrmean.shape)
if isinstance(arrmean, mu.ndarray):
with _no_nep50_warning():
arrmean = um.true_divide(arrmean, div, out=arrmean,
casting='unsafe', subok=False)
elif hasattr(arrmean, "dtype"):
arrmean = arrmean.dtype.type(arrmean / rcount)
else:
arrmean = arrmean / rcount

# Compute sum of squared deviations from mean
# Note that x may not be inexact and that we need it to be an array,
Expand DownExpand Up@@ -203,9 +206,9 @@ def _var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *,
return ret

def _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *,
where=True):
where=True, mean=None):
ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
keepdims=keepdims, where=where)
keepdims=keepdims, where=where, mean=mean)

if isinstance(ret, mu.ndarray):
ret = um.sqrt(ret, out=ret)
Expand Down
67 changes: 54 additions & 13 deletionsnumpy/core/fromnumeric.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -240,7 +240,7 @@ def reshape(a, newshape, order='C'):
-----
It is not always possible to change the shape of an array without copying
the data.

The `order` keyword gives the index ordering both for *fetching* the values
from `a`, and then *placing* the values into the output array.
For example, let's say you have an array:
Expand DownExpand Up@@ -2741,7 +2741,7 @@ def max(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,
max : ndarray or scalar
Maximum of `a`. If `axis` is None, the result is a scalar value.
If `axis` is an int, the result is an array of dimension
``a.ndim - 1``. If `axis` is a tuple, the result is an array of
``a.ndim - 1``. If `axis` is a tuple, the result is an array of
dimension ``a.ndim - len(axis)``.

See Also
Expand DownExpand Up@@ -2884,7 +2884,7 @@ def min(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,
min : ndarray or scalar
Minimum of `a`. If `axis` is None, the result is a scalar value.
If `axis` is an int, the result is an array of dimension
``a.ndim - 1``. If `axis` is a tuple, the result is an array of
``a.ndim - 1``. If `axis` is a tuple, the result is an array of
dimension ``a.ndim - len(axis)``.

See Also
Expand DownExpand Up@@ -3072,7 +3072,7 @@ def prod(a, axis=None, dtype=None, out=None, keepdims=np._NoValue,
array([ 2., 12.])
>>> np.prod(a, axis=0)
array([3., 8.])

Or select specific elements to include:

>>> np.prod([1., np.nan, 3.], where=[True, False, True])
Expand DownExpand Up@@ -3506,13 +3506,13 @@ def mean(a, axis=None, dtype=None, out=None, keepdims=np._NoValue, *,


def _std_dispatcher(a, axis=None, dtype=None, out=None, ddof=None,
keepdims=None, *, where=None):
return (a, where, out)
keepdims=None, *, where=None, mean=None):
return (a, where, out, mean)


@array_function_dispatch(_std_dispatcher)
def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
where=np._NoValue):
where=np._NoValue, mean=np._NoValue):
"""
Compute the standard deviation along the specified axis.

Expand DownExpand Up@@ -3554,13 +3554,20 @@ def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
`ndarray`, however any non-default value will be. If the
sub-class' method does not implement `keepdims` any
exceptions will be raised.

where : array_like of bool, optional
Elements to include in the standard deviation.
See `~numpy.ufunc.reduce` for details.

.. versionadded:: 1.20.0

mean : array like, optional
Provide the mean to prevent its recalculation. The mean should have
a shape as if it was calculated with ``keepdims=True``.
The axis for the calculation of the mean should be the same as used in
the call to this std function.

.. versionadded:: 1.26.0

Returns
-------
standard_deviation : ndarray, see dtype parameter above.
Expand DownExpand Up@@ -3628,12 +3635,26 @@ def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
>>> np.std(a, where=[[True], [True], [False]])
2.0

Using the mean keyword to save computation time:
>>> a = np.array([[14, 8, 11, 10], [7, 9, 10, 11], [10, 15, 5, 10]])
>>> mean = np.mean(a, axis=1, keepdims=True)
>>>
>>> %timeit std = np.std(a, axis=1, mean=mean)
>>> %timeit std = np.std(a, axis=1)
14.1 µs ± 50.1 ns per loop (mean ± std. dev. of 7 runs,
100,000 loops each)
20.3 µs ± 104 ns per loop (mean ± std. dev. of 7 runs,
10,000 loops each)

"""
kwargs = {}
if keepdims is not np._NoValue:
kwargs['keepdims'] = keepdims
if where is not np._NoValue:
kwargs['where'] = where
if mean is not np._NoValue:
kwargs['mean'] = mean

if type(a) is not mu.ndarray:
try:
std = a.std
Expand All@@ -3647,13 +3668,13 @@ def std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,


def _var_dispatcher(a, axis=None, dtype=None, out=None, ddof=None,
keepdims=None, *, where=None):
return (a, where, out)
keepdims=None, *, where=None, mean=None):
return (a, where, out, mean)


@array_function_dispatch(_var_dispatcher)
def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
where=np._NoValue):
where=np._NoValue, mean=np._NoValue):
"""
Compute the variance along the specified axis.

Expand DownExpand Up@@ -3696,13 +3717,20 @@ def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
`ndarray`, however any non-default value will be. If the
sub-class' method does not implement `keepdims` any
exceptions will be raised.

where : array_like of bool, optional
Elements to include in the variance. See `~numpy.ufunc.reduce` for
details.

.. versionadded:: 1.20.0

mean : array like, optional
Provide the mean to prevent its recalculation. The mean should have
a shape as if it was calculated with ``keepdims=True``.
The axis for the calculation of the mean should be the same as used in
the call to this var function.

.. versionadded:: 1.26.0

Returns
-------
variance : ndarray, see dtype parameter above
Expand DownExpand Up@@ -3768,12 +3796,25 @@ def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
>>> np.var(a, where=[[True], [True], [False]])
4.0

Using the mean keyword to save computation time:
>>> a = np.array([[14, 8, 11, 10], [7, 9, 10, 11], [10, 15, 5, 10]])
>>> mean = np.mean(a, axis=1, keepdims=True)
>>>
>>> %timeit var = np.var(a, axis=1, mean=mean)
>>> %timeit var = np.var(a, axis=1)
13.3 µs ± 18.3 ns per loop (mean ± std. dev. of 7 runs,
100,000 loops each)
18.6 µs ± 291 ns per loop (mean ± std. dev. of 7 runs,
100,000 loops each)

"""
kwargs = {}
if keepdims is not np._NoValue:
kwargs['keepdims'] = keepdims
if where is not np._NoValue:
kwargs['where'] = where
if mean is not np._NoValue:
kwargs['mean'] = mean

if type(a) is not mu.ndarray:
try:
Expand All@@ -3788,7 +3829,7 @@ def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *,
**kwargs)


# Aliases of other functions. Provided unique docstrings
# Aliases of other functions. Provided unique docstrings
# are for reference purposes only. Wherever possible,
# avoid using them.

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp