Reputation: 357
I have:
array = [1, 2, 3, 4, 5, 6, 7, 8];
I need to find numpy.argmax
only for last 4 elements in array.
This does not works, because index is losted:
>>> array = [1, 2, 3, 4, 5, 6, 7, 8];
>>> print (array[4:8]);
[5, 6, 7, 8]
>>> print (np.argmax(array[4:8]) );
3
The result must be 7
Upvotes: 2
Views: 4143
Reputation: 4318
Store your start slice in variable:
import numpy as np
array = [1, 2, 3, 4, 5, 6, 7, 8];
s=4
print (array[s:8]);
print s+(np.argmax(array[s:8]) );
Output:
[5, 6, 7, 8]
7
Upvotes: 1
Reputation: 53688
You can simply add your start
index to your maximum.
import numpy as np
array = [1, 2, 3, 4, 5, 6, 7, 8]
start = 4
end = 8
max_ = start + np.argmax(array[start:end])
print(max_)
# 7
Upvotes: 1
Reputation: 650
The simple approach would be to, I dunno, just add a 4 to the output? Assuming it isn't always 4, you could always do this:
print np.argmax(array[x : 8]) + x
Upvotes: 4