issues: 357629971
This data as json
id | node_id | number | title | user | state | locked | assignee | milestone | comments | created_at | updated_at | closed_at | author_association | active_lock_reason | draft | pull_request | body | reactions | performed_via_github_app | state_reason | repo | type |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
357629971 | MDU6SXNzdWUzNTc2Mjk5NzE= | 2401 | DataArray.mean() produces wrong result for float32 arrays of particular shapes | 16033750 | closed | 0 | 1 | 2018-09-06T12:21:57Z | 2018-09-06T13:17:13Z | 2018-09-06T13:15:17Z | NONE | Code Sample```python import numpy as np import xarray as xr np.random.seed(42) dims = ('a', 'b', 'c', 'd') shape = (10, 10, 500, 500) coords = {d: np.arange(s) for d, s in zip(dims, shape)} Using data with non-normal distributiondata = np.random.lognormal(size=shape) data = data.astype(np.float32) da = xr.DataArray(data, coords=coords, dims=dims) Numpy method gives the correct valueprint(da.values.mean()) Explicitly specifying all axis gives the correct valueprint(da.mean(axis=(0, 1, 2, 3))) Default DataArray mean method gives incorrect valueprint(da.mean()) # <- Problem arise here float64 arrays produce the correct valueprint(da.astype(np.float64).mean())
<xarray.DataArray ()> array(1.648908, dtype=float32) <xarray.DataArray ()> array(1.517693) <xarray.DataArray ()> array(1.648907) ``` Problem descriptionWrong mean value calculated by DataArray.mean() method with default arguments. I have only observed the problem for float32 arrays. It appears to be sensitive to the shape of the array, e.g. a shape of (10, 10, 10, 10) seems to be fine. Expected OutputThis is the output I expect for the sample above: ``` 1.6489075 <xarray.DataArray ()> array(1.648908, dtype=float32) <xarray.DataArray ()> array(1.648908, dtype=float32) <xarray.DataArray ()> array(1.648907) ```
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.6.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-33-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
xarray: 0.10.8
pandas: 0.23.4
numpy: 1.15.1
scipy: 1.1.0
netCDF4: 1.4.1
h5netcdf: 0.6.2
h5py: 2.8.0
Nio: None
zarr: None
bottleneck: 1.2.1
cyordereddict: None
dask: 0.19.0
distributed: 1.23.0
matplotlib: 2.2.3
cartopy: 0.16.0
seaborn: 0.9.0
setuptools: 40.2.0
pip: 18.0
conda: 4.5.11
pytest: 3.7.4
IPython: 6.5.0
sphinx: None
|
{ "url": "https://api.github.com/repos/pydata/xarray/issues/2401/reactions", "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
completed | 13221727 | issue |