使用 xarray 滚动分位数
Rolling quantile with xarray
是否有 xArray
计算 DataArray.rolling
window 上的分位数的方法?列出的可用方法包括 mean
或 median
,但 quantiles/percentiles 上没有。我想知道即使没有直接的方法,这是否可以以某种方式完成。
目前,我正在本地将 xArray
数据迁移到 pandas.DataFrame
,我在其中应用 rolling().quantile()
序列。之后,我采用新的 DataFrame
的值并从中构建一个 xArray.DataArray
。可重现代码:
import xarray as xr
import pandas as pd
import numpy as np
times = np.arange(0, 30)
locs = ['A', 'B', 'C', 'D']
signal = xr.DataArray(np.random.rand(len(times), len(locs)),
coords=[times, locs], dims=['time', 'locations'])
window = 5
df = pd.DataFrame(data=signal.data)
roll = df.rolling(window=window, center=True, axis=0).quantile(.25).dropna()
window_array = xr.DataArray(roll.values,
coords=[np.arange(0, signal.time.shape[0] - window + 1), signal.locations],
dims=['time', 'locations'])
欢迎提供尽可能坚持 xArray
的线索。
让我们考虑同样的问题,只是规模较小(10 个时间实例,2 个位置)。
这里是第一种方法的输入(通过pandas
):
<xarray.DataArray (time: 8, locations: 2)>
array([[0.404362, 0.076203],
[0.353639, 0.076203],
[0.387167, 0.102917],
[0.525404, 0.298231],
[0.755646, 0.298231],
[0.460749, 0.414935],
[0.104887, 0.498813],
[0.104887, 0.420935]])
Coordinates:
* time (time) int32 0 1 2 3 4 5 6 7
* locations (locations) <U1 'A' 'B'
请注意,'time' 维度较小,因为在滚动对象上调用 dropna()
。新维度大小基本是len(times) - window + 1
。现在,所提出方法的输出(通过 construct
):
<xarray.DataArray (time: 10, locations: 2)>
array([[0.438426, 0.127881],
[0.404362, 0.076203],
[0.353639, 0.076203],
[0.387167, 0.102917],
[0.525404, 0.298231],
[0.755646, 0.298231],
[0.460749, 0.414935],
[0.104887, 0.498813],
[0.104887, 0.420935],
[0.112651, 0.60338 ]])
Coordinates:
* time (time) int32 0 1 2 3 4 5 6 7 8 9
* locations (locations) <U1 'A' 'B'
好像维度还是(time, locations)
,前者的大小等于10,不是8。这里的例子,因为center=True
,如果您删除第二个数组中的第一行和最后一行。 DataArray
不应该有一个新维度 tmp
吗?
此外,这种方法(安装了 bottleneck
)比最初通过 pandas
提出的方法花费更多。例如,在 1000 times
x 2 locations
的案例研究中,pandas
运行 需要 0.015 秒,而 construct
需要 1.25 秒。
您可以使用construct
method滚动对象,它会生成一个新的DataArray
滚动尺寸。
signal.rolling(time=window, center=True).construct('tmp').quantile(.25, dim='tmp')
在上面,我构建了一个具有附加 tmp
维度的 DataArray 并沿该维度计算分位数。
是否有 xArray
计算 DataArray.rolling
window 上的分位数的方法?列出的可用方法包括 mean
或 median
,但 quantiles/percentiles 上没有。我想知道即使没有直接的方法,这是否可以以某种方式完成。
目前,我正在本地将 xArray
数据迁移到 pandas.DataFrame
,我在其中应用 rolling().quantile()
序列。之后,我采用新的 DataFrame
的值并从中构建一个 xArray.DataArray
。可重现代码:
import xarray as xr
import pandas as pd
import numpy as np
times = np.arange(0, 30)
locs = ['A', 'B', 'C', 'D']
signal = xr.DataArray(np.random.rand(len(times), len(locs)),
coords=[times, locs], dims=['time', 'locations'])
window = 5
df = pd.DataFrame(data=signal.data)
roll = df.rolling(window=window, center=True, axis=0).quantile(.25).dropna()
window_array = xr.DataArray(roll.values,
coords=[np.arange(0, signal.time.shape[0] - window + 1), signal.locations],
dims=['time', 'locations'])
欢迎提供尽可能坚持 xArray
的线索。
让我们考虑同样的问题,只是规模较小(10 个时间实例,2 个位置)。
这里是第一种方法的输入(通过pandas
):
<xarray.DataArray (time: 8, locations: 2)>
array([[0.404362, 0.076203],
[0.353639, 0.076203],
[0.387167, 0.102917],
[0.525404, 0.298231],
[0.755646, 0.298231],
[0.460749, 0.414935],
[0.104887, 0.498813],
[0.104887, 0.420935]])
Coordinates:
* time (time) int32 0 1 2 3 4 5 6 7
* locations (locations) <U1 'A' 'B'
请注意,'time' 维度较小,因为在滚动对象上调用 dropna()
。新维度大小基本是len(times) - window + 1
。现在,所提出方法的输出(通过 construct
):
<xarray.DataArray (time: 10, locations: 2)>
array([[0.438426, 0.127881],
[0.404362, 0.076203],
[0.353639, 0.076203],
[0.387167, 0.102917],
[0.525404, 0.298231],
[0.755646, 0.298231],
[0.460749, 0.414935],
[0.104887, 0.498813],
[0.104887, 0.420935],
[0.112651, 0.60338 ]])
Coordinates:
* time (time) int32 0 1 2 3 4 5 6 7 8 9
* locations (locations) <U1 'A' 'B'
好像维度还是(time, locations)
,前者的大小等于10,不是8。这里的例子,因为center=True
,如果您删除第二个数组中的第一行和最后一行。 DataArray
不应该有一个新维度 tmp
吗?
此外,这种方法(安装了 bottleneck
)比最初通过 pandas
提出的方法花费更多。例如,在 1000 times
x 2 locations
的案例研究中,pandas
运行 需要 0.015 秒,而 construct
需要 1.25 秒。
您可以使用construct
method滚动对象,它会生成一个新的DataArray
滚动尺寸。
signal.rolling(time=window, center=True).construct('tmp').quantile(.25, dim='tmp')
在上面,我构建了一个具有附加 tmp
维度的 DataArray 并沿该维度计算分位数。