简体   繁体   English

沿第三维查找 xarray DataArray 的最高值。

[英]Finding highest values of xarray DataArray along the third dimension.

I have a 3 dimensional xarray DataArray, where two dimensions represent a xy grid and the third one the number of grid-layers "stacked" on each other.我有一个 3 维 xarray DataArray,其中两个维度表示一个 xy 网格,第三个维度表示彼此“堆叠”的网格层数。 The empty DataArray looks like this:空的 DataArray 看起来像这样:

import xarray as xr
import numpy as np

data = np.zeros(shape=(layers,y,x))
dims=['layer','y_Axis', 'x_Axsis']
dataArray = xr.DataArray(data,dims=dims)

Within a routine I am filling up the grids layer by layer with values between -1 and 1.在一个例程中,我用 -1 和 1 之间的值逐层填充网格。

My task now is to stack all layers together into one selecting to highest values of all grids combined.我现在的任务是将所有层堆叠在一起,选择所有网格组合的最高值。 So when for instance 5 layers are compared with each other the highest elements within the grid will be saved in a 2d numpy array.因此,当例如 5 层相互比较时,网格内的最高元素将保存在 2d numpy 数组中。

I could solve this by looping through every layer comparing the grid values of the current layer with a created 2d max_val_grid and thus filtering through the third dimension.我可以通过遍历每一层来解决这个问题,将当前层的网格值与创建的 2d max_val_grid 进行比较,从而过滤第三维。 However this sounds very inefficient.然而,这听起来非常低效。

Has anyone of you an idea how to solve this using internal xarray or numpy functions without looping?你们有没有人知道如何使用内部 xarray 或 numpy 函数而不循环来解决这个问题?

thanks alot!多谢!

You can use numpy.amax as follows您可以使用numpy.amax如下

numpy.amax(your_3D_array, axis=2)

This will select the maximum values of your_3D_array on the last axis of the 3D data and return a 2D array.这将在 3D 数据的最后一个轴上选择your_3D_array的最大值并返回一个 2D 数组。 Here is a quick test case:这是一个快速测试用例:

import numpy as np

x = np.arange(10)
y = np.arange(10, 20)
z = np.arange(20, 30)

x, y, z = np.meshgrid(x, y, z)

print(np.amax(z, axis=2))

xarray 中的max()方法完全符合您的要求,例如dataArray.max('layer')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM