检索np.ndarray中的最小值和最大值的索引(Retrieve indexes of min and max values in np.ndarray)

编程入门 行业动态 更新时间:2024-10-18 10:27:01
检索np.ndarray中的最小值和最大值的索引(Retrieve indexes of min and max values in np.ndarray)

我正在研究一些tif文件,我必须根据.tif文件绘制温度和vegatation索引之间的依赖关系。 这只是FYI。 现在我的编程问题。 我正在使用python 2.7(x64)。 我有大的ndarray形式NumPy lib,包含temerature和第二(相同大小)植被idex的值。 mergedmask是我的掩码(与其他数组一样大小),其中False值表示它是有效数据。

maxTS = np.amax(toa[mergedmask==False]) minTS = np.amin(toa[mergedmask==False]) maxVI = np.amax(ndvi1[mergedmask==False]) minVi = np.amin(ndvi1[mergedmask==False])

在上部变量中,我具有TS (温度)和VI (植被指数)的最小值和最大值。 一切都好。 我很开心。 现在我必须在toa和ndvi1数组中找到coords。 所以我使用这个:

ax,ay = np.unravel_index(ndvi1[mergedmask==False].argmin(),ndvi1.shape)

为了简化我的消息,我只关注minVI 。 上线返回2个索引。 然后:

newMinVi = ndvi1[ax][ay]

应该分配给newMinVi与newMinVi相同的值。 但事实并非如此。 我检查了附近的索引,如ax-1, ax+1, ay-1,ay+1 ,所有这些minVi都不接近我的minVi值。 你有什么想法来获得我的minVi值的协调。

i am working on some tif files and i have to plot dependecies between temperature and vegatation index based on .tif file. It was just FYI. Now my programming problem. I'm using python 2.7 (x64). I have big ndarray form NumPy lib, contains values of temerature and second (same size) with vegetation idex. mergedmask is my mask (same size like other arrays) where False value mean it is valid data.

maxTS = np.amax(toa[mergedmask==False]) minTS = np.amin(toa[mergedmask==False]) maxVI = np.amax(ndvi1[mergedmask==False]) minVi = np.amin(ndvi1[mergedmask==False])

In upper variables i have minimum and maximum values of TS (temperature) and VI (vegetation index). Everything is ok. I am happy. Now i have to find coords in toa and ndvi1 arrays. So i am using this:

ax,ay = np.unravel_index(ndvi1[mergedmask==False].argmin(),ndvi1.shape)

To simplify my msg i just focus only on minVI. Upper line return 2 indexes. Then:

newMinVi = ndvi1[ax][ay]

should assign to newMinVi same value as minVi. But it doesn't. I check near indexes like ax-1, ax+1, ay-1,ay+1 and all of them is not even close to my minVi value. Have you any ideas to get coord of my minVi value.

最满意答案

ndvi1[mergedmask==False].argmin()将为您提供ndvi1[mergedmask==False]中的最小索引,即新数组的索引,对应于mergedmask为False 。

这里的问题是ndvi1[mergedmask==False]实际上并不是一个掩码 。 它选择满足条件的ndvi1值,并将这些值组装成新的1D数组。 例如,检查ndvi1[mergedmask==False].size是什么,并将其与ndvi1.size进行比较。

您可能想要做的是创建一个真正的蒙版数组:

ndvi1_masked = np.ma.masked_array(ndvi1, (mergedmask==False)) ax, ay = np.unravel_index(ndvi1_masked.argmin(), ndvi1.shape)

希望这可以帮助!

ndvi1[mergedmask==False].argmin() will give you the index of the minimum in ndvi1[mergedmask==False], i.e., the index into a new array, corresponding to the places where mergedmask is False.

The problem here is that ndvi1[mergedmask==False] isn't really a mask. It selects those values of ndvi1 which meets the condition, and assembles those values into a new 1D array. For instance, check what ndvi1[mergedmask==False].size is, and compare it to ndvi1.size.

What you probably want to be doing is to create a real masked array:

ndvi1_masked = np.ma.masked_array(ndvi1, (mergedmask==False)) ax, ay = np.unravel_index(ndvi1_masked.argmin(), ndvi1.shape)

Hope this helps!

更多推荐

本文发布于:2023-08-01 05:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最大值   索引   最小值   ndarray   np

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!