使用 Numpy Python 获取最小值的索引 [英] getting the index of min values with 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 获取最小值的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!