简体   繁体   中英

How find minimum point of array of lists in python

    def get_minima(array):

sdiff = np.diff(np.sign(np.diff(array)))
rising_1 = (sdiff == 2) 
rising_2 = (sdiff[:-1] == 1) & (sdiff[1:] == 1) 
rising_all = rising_1 
rising_all[1:] = rising_all[1:] | rising_2 
min_ind = np.where(rising_all)[0] + 1 
minima = list(zip(min_ind, array[min_ind]))

return sorted(minima, key=lambda x: x[1])

by running this code with the array of data i have, it yields:

[(59, 7.958373616052042e-10),
 (69, 6.5364637051479655e-09),
 (105, 1.0748381102806489e-08),
 (88, 2.953895857338913e-07),
 (27, 9.083111768048306e-07)]

which is great - its all the minimums in my dataset. But i need to store only the minimum - which is the (59, 7.958373616052042e-10) point in this particular example. I cant figure out how to do this. I Tried some stuff with using np.amin and doing a boolean comparison but i got quite confused with the notation and syntax, since now its an array of list and i havent ever really worked with that before.
Appreciate any help!

Instead of sorting all the minimums, you can just get the lowest pair:

def get_minima(array):
    sdiff = np.diff(np.sign(np.diff(array)))
    rising_1 = (sdiff == 2) 
    rising_2 = (sdiff[:-1] == 1) & (sdiff[1:] == 1) 
    rising_all = rising_1 
    rising_all[1:] = rising_all[1:] | rising_2 
    min_ind = np.where(rising_all)[0] + 1 
    minima = list(zip(min_ind, array[min_ind]))
    return min(minima, key=lambda pair: pair[1])

For example:

minima = [(59, 7.958373616052042e-10),
 (69, 6.5364637051479655e-09),
 (105, 1.0748381102806489e-08),
 (88, 2.953895857338913e-07),
 (27, 9.083111768048306e-07)]

minimum = min(minima, key=lambda pair: pair[1])
print(minimum)

>>> (59, 7.958373616052042e-10)

Well it looks too simple but did you tried it?

minima = list(zip(min_ind, array[min_ind]))
minima.sort(key=lambda x: x[1])
return minima[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM