猿问

查找满足条件的 xarray 指数

我想获取满足某些条件的 xarray 数据数组的索引。在相关线程(此处)中提供的有关如何查找最大值位置的答案也不适合我。就我而言,我也想找出其他类型的条件的位置,而不仅仅是最大值。这是我尝试过的:

h=xr.DataArray(np.random.randn(3,4))

h.where(h==h.max(),drop=True).squeeze()


# This is the output I got:

<xarray.DataArray ()>

array(1.66065694)

这不会返回我链接到的示例中所示的位置,即使我正在执行相同的命令。我不知道为什么。


江户川乱折腾
浏览 224回答 1
1回答

蓝山帝景

我更新了链接的示例以更清晰地显示索引。由于 xarray 不再添加默认索引,因此前面的示例会查找最大位置,但不显示索引。抄写如下:In [17]: da = xr.DataArray(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;np.random.rand(2,3),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dims=list('ab'),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;coords=dict(a=list('xy'), b=list('ijk'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)In [18]: da.where(da==da.max(), drop=True).squeeze()Out[18]:<xarray.DataArray ()>array(0.96213673)Coordinates:&nbsp; &nbsp; a&nbsp; &nbsp; &nbsp; &nbsp; <U1 'x'&nbsp; &nbsp; b&nbsp; &nbsp; &nbsp; &nbsp; <U1 'j'
随时随地看视频慕课网APP

相关分类

Python
我要回答