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
Changes from1 commit
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
PrevPrevious commit
NextNext commit
Align nan signatures with new signatures.
  • Loading branch information
@RonaldAJ
RonaldAJ committedJul 5, 2023
commitf78e7932bdd6dc6632396d7197ea3540f7a3179c
42 changes: 27 additions & 15 deletionsnumpy/lib/nanfunctions.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1610,13 +1610,13 @@ def _nanquantile_1d(arr1d, q, overwrite_input=False, method="linear"):


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


@array_function_dispatch(_nanvar_dispatcher)
def nanvar(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, while ignoring NaNs.

Expand DownExpand Up@@ -1656,6 +1656,10 @@ def nanvar(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue,
where : array_like of bool, optional
Elements to include in the variance. See `~numpy.ufunc.reduce` for
details.
mean : optionally provide the mean to prevent recalculation, the mean
should have a shape as if it was calculated with keepdims = True
and the axis the same as used in the call to this var function.
For masked arrays this argument is ignored.

.. versionadded:: 1.22.0

Expand DownExpand Up@@ -1713,7 +1717,7 @@ def nanvar(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue,
arr, mask = _replace_nan(a, 0)
if mask is None:
return np.var(arr, axis=axis, dtype=dtype, out=out, ddof=ddof,
keepdims=keepdims, where=where)
keepdims=keepdims, where=where, mean=mean)

if dtype is not None:
dtype = np.dtype(dtype)
Expand All@@ -1727,15 +1731,19 @@ def nanvar(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue,
_keepdims = np._NoValue
else:
_keepdims = True
# we need to special case matrix for reverse compatibility
# in order for this to work, these sums need to be called with
# keepdims=True, however matrix now raises an error in this case, but
# the reason that it drops the keepdims kwarg is to force keepdims=True
# so this used to work by serendipity.
cnt = np.sum(~mask, axis=axis, dtype=np.intp, keepdims=_keepdims,
where=where)
avg = np.sum(arr, axis=axis, dtype=dtype, keepdims=_keepdims, where=where)
avg = _divide_by_count(avg, cnt)

if mean is not np._NoValue:
avg = mean
else:
# we need to special case matrix for reverse compatibility
# in order for this to work, these sums need to be called with
# keepdims=True, however matrix now raises an error in this case, but
# the reason that it drops the keepdims kwarg is to force keepdims=True
# so this used to work by serendipity.
cnt = np.sum(~mask, axis=axis, dtype=np.intp, keepdims=_keepdims,
where=where)
avg = np.sum(arr, axis=axis, dtype=dtype, keepdims=_keepdims, where=where)
avg = _divide_by_count(avg, cnt)

# Compute squared deviation from mean.
np.subtract(arr, avg, out=arr, casting='unsafe', where=where)
Expand DownExpand Up@@ -1771,13 +1779,13 @@ def nanvar(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue,


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


@array_function_dispatch(_nanstd_dispatcher)
def nanstd(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, while
ignoring NaNs.
Expand DownExpand Up@@ -1824,6 +1832,10 @@ def nanstd(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue,
where : array_like of bool, optional
Elements to include in the standard deviation.
See `~numpy.ufunc.reduce` for details.
mean : optionally provide the mean to prevent recalculation, the mean
should have a shape as if it was calculated with keepdims = True
and the axis the same as used in the call to this var function.
For masked arrays this argument is ignored.

.. versionadded:: 1.22.0

Expand DownExpand Up@@ -1877,7 +1889,7 @@ def nanstd(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue,

"""
var = nanvar(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
keepdims=keepdims, where=where)
keepdims=keepdims, where=where, mean=mean)
if isinstance(var, np.ndarray):
std = np.sqrt(var, out=var)
elif hasattr(var, 'dtype'):
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp