Skip to content

Commit fad9dba

Browse files
committed
implemented nanmean
1 parent 693b257 commit fad9dba

7 files changed

Lines changed: 5592 additions & 15 deletions

File tree

docs/api-assorted.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Assorted functions
2+
3+
```{eval-rst}
4+
.. currentmodule:: array_api_extra
5+
.. autosummary::
6+
:nosignatures:
7+
:toctree: generated
8+
9+
angle
10+
apply_where
11+
argpartition
12+
at
13+
atleast_nd
14+
broadcast_shapes
15+
cov
16+
create_diagonal
17+
default_dtype
18+
deg2rad
19+
diag_indices
20+
expand_dims
21+
isclose
22+
isin
23+
kron
24+
nan_to_num
25+
nanmax
26+
nanmean
27+
nanmin
28+
nansum
29+
nunique
30+
one_hot
31+
pad
32+
partition
33+
rad2deg
34+
searchsorted
35+
setdiff1d
36+
sinc
37+
tril_indices
38+
triu_indices
39+
union1d
40+
unravel_index
41+
```

src/array_api_extra/__init__.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
11
"""Extra array functions built on top of the array API standard."""
22

33
from . import testing
4-
from ._agnostic._elementwise import angle, apply_where
5-
from ._agnostic._inspection import default_dtype
6-
from ._at import at
7-
from ._creation import create_diagonal, one_hot
8-
from ._elementwise import deg2rad, isclose, nan_to_num, rad2deg, sinc
9-
from ._indexing import diag_indices, tril_indices, triu_indices, unravel_index
10-
from ._lazy import lazy_apply
11-
from ._linalg import kron
12-
from ._manipulation import atleast_nd, broadcast_shapes, expand_dims, pad
13-
from ._searching import searchsorted
14-
from ._set import isin, nunique, setdiff1d, union1d
15-
from ._sorting import argpartition, partition
16-
from ._statistical import cov, nanmax, nanmin, nansum
4+
from ._delegation import (
5+
argpartition,
6+
atleast_nd,
7+
broadcast_shapes,
8+
cov,
9+
create_diagonal,
10+
deg2rad,
11+
diag_indices,
12+
expand_dims,
13+
isclose,
14+
isin,
15+
kron,
16+
nan_to_num,
17+
nanmax,
18+
nanmean,
19+
nanmin,
20+
nansum,
21+
nunique,
22+
one_hot,
23+
pad,
24+
partition,
25+
rad2deg,
26+
searchsorted,
27+
setdiff1d,
28+
sinc,
29+
tril_indices,
30+
triu_indices,
31+
union1d,
32+
unravel_index,
33+
)
34+
from ._lib._at import at
35+
from ._lib._funcs import (
36+
angle,
37+
apply_where,
38+
default_dtype,
39+
)
40+
from ._lib._lazy import lazy_apply
1741

1842
__version__ = "0.11.2.dev0"
1943

@@ -37,6 +61,7 @@
3761
"lazy_apply",
3862
"nan_to_num",
3963
"nanmax",
64+
"nanmean",
4065
"nanmin",
4166
"nansum",
4267
"nunique",

src/array_api_extra/_agnostic/_statistical.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .._lib._typing import Array, ArrayNamespace
99
from . import _manipulation
1010

11-
__all__ = ["cov", "nanmax", "nanmin", "nansum"]
11+
__all__ = ["cov", "nanmax", "nanmean", "nanmin", "nansum"]
1212

1313

1414
def cov(
@@ -154,3 +154,29 @@ def nansum( # numpydoc ignore=PR01,RT01
154154
"""See docstring in `array_api_extra._statistical`."""
155155
mask = xp.isnan(a)
156156
return xp.sum(xp.where(mask, xp.zeros_like(a), a), axis=axis)
157+
158+
159+
def nanmean( # numpydoc ignore=PR01,RT01
160+
a: Array,
161+
/,
162+
*,
163+
axis: int | tuple[int, ...] | None,
164+
xp: ArrayNamespace,
165+
) -> Array:
166+
"""See docstring in `array_api_extra._statistical`."""
167+
mask = xp.isnan(a)
168+
sum_ = nansum(a, axis=axis, xp=xp)
169+
count = xp.count_nonzero(~mask, axis=axis)
170+
safe_count = xp.astype(
171+
xp.where(count == 0, xp.ones_like(count), count),
172+
sum_.dtype,
173+
copy=False,
174+
)
175+
result = sum_ / safe_count
176+
if xp.any(count == 0):
177+
result = xp.where(
178+
count == 0,
179+
xp.full_like(result, xp.nan),
180+
result,
181+
)
182+
return result

0 commit comments

Comments
 (0)