使用 Numpy Python 获取最小值的索引

编程入门 行业动态 更新时间:2024-10-13 16:19:56
本文介绍了使用 Numpy Python 获取最小值的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面的函数将每个值分成由索引 index 和 L_list 中的值分隔的块.所以它输出索引 3-5 之间的最小值,即 -5 和值的索引.numpy_argmin_reduceat(a, b) 和 Drawdown 函数都按计划执行,但是 numpy_argmin_reduceat(a, b) 的索引输出有问题它 Drawdown 的最小值与 numpy_argmin_reduceat(a, b) 的输出索引不匹配.我该如何解决这个问题?数组:

The function below separates each value into chunks separated by indexes index with the values in L_list. So it outputs the minimum value between indexes 3-5 which is -5 and the index of the value. Both the numpy_argmin_reduceat(a, b) and the Drawdown function do as planned however the index output of the numpy_argmin_reduceat(a, b) is faulty it The minimum values of Drawdown do not match with the indexes of the outputs of numpy_argmin_reduceat(a, b).How would I be able to solve this? Arrays:

import numpy as np # indexes 0, 1, 2,3,4, 5, 6,7, 8, 9,10, 11, 12 L_list = np.array([10,20,30,0,0,-5,11,2,33, 4, 5, 68, 7]) index = np.array([3,5,7,11])

功能:

#getting the minimum values Drawdown = np.minimum.reduceat(L_list,index+1) #Getting the min Index def numpy_argmin_reduceat(a, b): n = a.max() + 1 # limit-offset id_arr = np.zeros(a.size,dtype=int) id_arr[b] = 1 shift = n*id_arr.cumsum() sortidx = (a+shift).argsort() grp_shifted_argmin = b idx =sortidx[grp_shifted_argmin] - b min_idx = idx +index return min_idx min_idx =numpy_argmin_reduceat(L_list,index+1) #printing function DR_val_index = np.array([np.around(Drawdown,1), min_idx]) DR_result = np.apply_along_axis(lambda x: print(f'Min Values: {x[0]} at index: {x[1]}'), 0, DR_val_index)

输出

Min Values: -5 at index: 4 Min Values: 2 at index: 6 Min Values: 4 at index: 8 Min Values: 7 at index: 11

预期输出:

Min Values: -5 at index: 5 Min Values: 2 at index: 7 Min Values: 4 at index: 9 Min Values: 7 at index: 12

推荐答案

如果换行

id_arr[b[1:]] = 1

id_arr[b] = 1

我认为该函数会如您所愿.

I think the function will behave as you hope.

更多推荐

使用 Numpy Python 获取最小值的索引

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

发布评论

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

>www.elefans.com

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