numpy - 将 scipy.stats.percentileofscore 应用于 xarray 重采样减少函数
问题描述
我有以下名为foo
.
<xarray.DataArray (time: 4, lat: 3, lon: 2)>
array([[[0.061686, 0.434164],
[0.642003, 0.78744 ],
[0.068701, 0.526546]],
[[0.53612 , 0.549919],
[0.172044, 0.118106],
[0.381638, 0.736584]],
[[0.688589, 0.173351],
[0.03593 , 0.833743],
[0.667719, 0.890957]],
[[0.712785, 0.04725 ],
[0.132689, 0.938043],
[0.681481, 0.67986 ]]])
Coordinates:
* time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
* lat (lat) <U2 'IA' 'IL' 'IN'
* lon (lon) <U2 '00' '22'
在进行 48 小时重新采样时,我需要scipy.stats.percentileofscore
沿维度应用该函数。time
from scipy import stats
foo.resample(time='48H').reduce(stats.percentileofscore, dim='time', score=0.1)
我收到以下错误:
\variable.py", line 1354, in reduce
axis=axis, **kwargs)
TypeError: percentileofscore() got an unexpected keyword argument 'axis'
解决方案
繁殖数据:
import xarray as xa
array = np.array([[[0.061686, 0.434164],
[0.642003, 0.78744 ],
[0.068701, 0.526546]],
[[0.53612 , 0.549919],
[0.172044, 0.118106],
[0.381638, 0.736584]],
[[0.688589, 0.173351],
[0.03593 , 0.833743],
[0.667719, 0.890957]],
[[0.712785, 0.04725 ],
[0.132689, 0.938043],
[0.681481, 0.67986 ]]])
lat = ['IA','IL','IN']
lon = ['00','22']
times = pd.date_range('2000-01-01', periods=4, freq='H') #Hours
foo = xr.DataArray(array, coords=[times, lat, lon], dims=['time', 'lat', 'lon'])
您需要的功能:
from scipy import stats
import numpy as np
def func(x, axis, score):
out = np.apply_along_axis(stats.percentileofscore, axis, x, *[score])
return out
res = foo.resample(time='2H').reduce(func, **{'score':0.2}) #Each 2 hours
输出:
<xarray.DataArray (time: 2, lat: 3, lon: 2)>
array([[[ 50., 0.],
[ 50., 50.],
[ 50., 0.]],
[[ 0., 100.],
[100., 0.],
[ 0., 0.]]])
Coordinates:
* time (time) datetime64[ns] 2000-01-01 2000-01-01T02:00:00
* lat (lat) <U2 'IA' 'IL' 'IN'
* lon (lon) <U2 '00' '22'
解释
函数的期望及其输出:
def func2(x, axis): #Expect a function with axis argument (error reason)
print(x) #to see the output that our function receive as input
return x #not relevant
foo.resample(time='2H').reduce(func2)
#Input of our func2 (two new arrays with shape (2,3,2))
a = np.array([[[0.061686, 0.434164],
[0.642003, 0.78744 ],
[0.068701, 0.526546]],
[[0.53612, 0.549919],
[0.172044, 0.118106],
[0.381638, 0.736584]]])
b = np.array([[[0.688589, 0.173351],
[0.03593, 0.833743],
[0.667719, 0.890957]],
[[0.712785, 0.04725 ],
[0.132689, 0.938043],
[0.681481, 0.67986 ]]])
所以,你正在做的是:
stats.percentileofscore(a, score=0.2) #200 #Reduce over lon and lat
stats.percentileofscore(b, score=0.2) #200 #This raise another error
这就是为什么您需要一个通过轴(例如np.mean(a, axis=None, ...)
)运行的函数来np.apply_along_axis
帮助我们完成这项任务的原因:
#reduce through hours 1-2 and hours 3-4 (axis=0)
np.apply_along_axis(stats.percentileofscore, 0, a, **{'score':0.2})
np.apply_along_axis(stats.percentileofscore, 0, b, **{'score':0.2})
推荐阅读
- c++ - 添加 _AFXDLL 宏和 clr 支持后找不到 InterlockedAdd64
- symfony - Symfony 5 无效的防火墙“api”:找不到用户提供程序“app_user_provider”
- javascript - render.js 中的电子属性无法访问
- vue.js - Vue保护路径免受编辑的本地存储
- autodesk-forge - 如何使用 Revit API 读取所有数据?
- javascript - 使 PartialView 适合引导模式 (MVC)
- python - 如何自定义 LSTM 损失函数以仅考虑给定的预测索引范围和目标序列?
- parsley.js - 在选择选项下拉列表中选择特定值时触发 Parsley 错误
- mysql - MySQL - 多个 TABLES 上的多个 GROUP_CONCAT(服务器错误)
- flutter - 在没有用户能够更改的情况下,有什么可靠的方法可以让时间颤抖?