1

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])

Functions:

#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)

Output

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

Expected Output:

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

1 Answer 1

1

Redefine the indexes using np.r_, then slice and zip the indexes in order to create intervals defining start and ending index of chunks, then iterate over these intervals and select the chunk in L_list corresponding to the interval, then using argmin find the index of minimum value and add this to the starting index of chunk to get the actual index in L_list

i = np.r_[index[0]-1, index[1:], len(L_list)-1] + 1
for x, y in zip(i[:-1], i[1:]):
    i_min = x + L_list[x:y].argmin()
    print('Min Value:', L_list[i_min], 'at index:', i_min)

Min Value: -5 at index: 5
Min Value: 2 at index: 7
Min Value: 4 at index: 9
Min Value: 7 at index: 12
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.