tony selcuk
tony selcuk

Reputation: 687

Slicing and getting the max value with Numpy Python

Im trying to write a code with numpy where it outputs the maximum value between indexes. I think using argmax could be usable. However I do not know how I can use slices without using a for loop in python. If there is a pandas function for this it could be useable too. I want to make the computation as fast as possible.

list_ = np.array([9887.89, 9902.99, 9902.99, 9910.23, 9920.79, 9911.34, 9920.01, 9927.51, 9932.3, 9932.33, 9928.87, 9929.22, 9929.22, 9935.24, 9935.24, 9935.26, 9935.26, 9935.68, 9935.68, 9940.5])
indexes = np.array([0, 5, 10, 19])

Expected result:

Max number between index(0 - 5):  9920.79 at index 5
Max number between index(5 - 10): 9932.33 at index 10
Max number between index(10 - 19): 9940.5 at index 19

Upvotes: 0

Views: 667

Answers (2)

Ehsan
Ehsan

Reputation: 12407

You can use reduceat directly yo your array without the need to splice/split it:

np.maximum.reduceat(list_,indexes[:-1])

output:

array([9932.33, 9929.22, 9940.5 ])

Upvotes: 1

Ananda
Ananda

Reputation: 3272

Assuming that the first (zero) index and the last index is specified in the indexes array,

import numpy as np

list_ = np.array([9887.89, 9902.99, 9902.99, 9910.23, 9920.79, 9911.34, 9920.01, 9927.51, 9932.3, 9932.33, 9928.87, 9929.22, 9929.22, 9935.24, 9935.24, 9935.26, 9935.26, 9935.68, 9935.68, 9940.5])
indexes = np.array([0, 5, 10, 19])

chunks = np.split(list_, indexes[1:-1])
print([c.max() for c in chunks])

max_ind = [c.argmax() for c in chunks]
print(max_ind + indexes[:-1])

It's not necessary that each chunk will have the same size with an arbitrary specification of indices. So The vectorization benefits of numpy is going to be lost in there one way or another (Since you can't have a numpy array where each element is of a different size in memory which also has all the benefits of vectorization).

At least one for loop is going to be necessary, I think. However, you can use split, to make the splitting a numpy-optimized operation.

Upvotes: 1

Related Questions