Вы можете обернуть функцию режима Сципи с помощью Xarray's apply_ufunc
. Дополнительные примеры использования apply_ufunc
можно найти здесь .
def _mode(*args, **kwargs):
vals = scipy.stats.mode(*args, **kwargs)
# only return the mode (discard the count)
return vals[0].squeeze()
def mode(obj, None):
# note: apply always moves core dimensions to the end
# usually axis is simply -1 but scipy's mode function doesn't seem to like that
# this means that this version will only work for DataArray's (not Datasets)
assert isinstance(obj, xr.DataArray)
axis = obj.ndim - 1
return xr.apply_ufunc(_mode, obj,
input_core_dims=[[dim]],
kwargs={'axis': axis})
Быстрый пример использования обучающих наборов данных xarray:
ds = xr.tutorial.load_dataset('air_temperature')
mode(ds, dim='time')
выходы:
<xarray.Dataset>
Dimensions: (lat: 25, lon: 53)
Coordinates:
* lat (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
* lon (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
Data variables:
air (lat, lon) float32 271.5 272.4 272.5 272.1 ... 296.9 296.9 296.79