2

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

emesday
  • 6,078
  • 3
  • 29
  • 46
peroksid
  • 357
  • 3
  • 14

3 Answers3

3

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

vinit_ivar
  • 610
  • 6
  • 16
1

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
Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
1

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
venpa
  • 4,268
  • 21
  • 23