Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork11.9k
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
mattip merged 24 commits intonumpy:mainfromsoundappraisal:ENH_Add_mean_keyword_to_std_and_varJul 7, 2023
Uh oh!
There was an error while loading.Please reload this page.
Merged
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.
RonaldAJ3ba2ccc Add releae note for mean keyword to std and var functions.
RonaldAJd52c0b7 Update release note with PR number
RonaldAJ3c9e4d3 Address lint issue.
RonaldAJf78e793 Align nan signatures with new signatures.
RonaldAJ3bb28dd Address lint issue.
RonaldAJ13161f1 Correct version numbers on keywords.
RonaldAJd93630d Put backticks on keyword argument in documentation string.
RonaldAJ6e966a1 Cleanuup assert statements in tests
RonaldAJfe32fd1 Move comparison of in and out arrays closer to the function call.
RonaldAJa15af41 Remove clutter from example code in release note.
RonaldAJ1a10e70 Add test for nanstd and fix error in nanvar
RonaldAJef7484a haqndle "mean" keyword for var and std on MaskedArrays.
RonaldAJccc5258 Address lint issues.
RonaldAJe150370 update the dispatchers according to suggestions by Marten van Kerkwijk:
RonaldAJbbdeed4 Move and adjust example from release note to doc-strings. Reflow doc-…
RonaldAJ201034a Improve doc-string. Shorter sentences and add type and label mean arg…
RonaldAJ71998c8 Remove some of these pesky trailing white spaces
RonaldAJ0f553ff Make extra white lines more consistent.
RonaldAJaa861b3 Make sure code examples execute without Jupyter magic.
RonaldAJ020257a Fold lines to pass linter.
RonaldAJf8d8399 Update doc-string nanstd and nanvar.
RonaldAJ4f5cc4c Try to satisfy linter and apple requirements at the same time. Making…
RonaldAJb891190 Make doctest skip resource dependent output
RonaldAJFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletionsdoc/release/upcoming_changes/24126.change.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
67 changes: 54 additions & 13 deletionsnumpy/core/fromnumeric.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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: | ||
| @@ -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 | ||
| dimension ``a.ndim - len(axis)``. | ||
| See Also | ||
| @@ -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 | ||
| dimension ``a.ndim - len(axis)``. | ||
| See Also | ||
| @@ -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]) | ||
| @@ -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, 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, mean=np._NoValue): | ||
| """ | ||
| Compute the standard deviation along the specified axis. | ||
| @@ -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 | ||
RonaldAJ marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| 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. | ||
| @@ -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 | ||
| @@ -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, 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, mean=np._NoValue): | ||
| """ | ||
| Compute the variance along the specified axis. | ||
| @@ -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 | ||
RonaldAJ marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| 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 | ||
| @@ -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: | ||
| @@ -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 | ||
| # are for reference purposes only. Wherever possible, | ||
| # avoid using them. | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.