如何获取xarray中最大值的坐标? [英] How to get the coordinates of the maximum in xarray?

查看:472
本文介绍了如何获取xarray中最大值的坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题:我不仅想要最大值,而且还希望它在xarray DataArray中的坐标.该怎么做?

Simple question: I don't only want the value of the maximum but also the coordinates of it in an xarray DataArray. How to do that?

我当然可以编写自己的简单reduce函数,但是我想知道xarray中是否内置了什么?

I can, of course, write my own simple reduce function, but I wonder if there is anything built-in in xarray?

推荐答案

您可以使用 da.where() 以基于最大值进行过滤:

You can use da.where() to filter based on the max value:

In [17]: da = xr.DataArray(
             np.random.rand(2,3), 
             dims=list('ab'), 
             coords=dict(a=list('xy'), b=list('ijk'))
         )

In [18]: da.where(da==da.max(), drop=True).squeeze()
Out[18]:
<xarray.DataArray ()>
array(0.96213673)
Coordinates:
    a        <U1 'x'
    b        <U1 'j'

由于xarray没有默认索引,因此更新了示例以更清楚地显示索引

updated the example to show the indexes more clearly, now that xarray doesn't have default indexes

这篇关于如何获取xarray中最大值的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆