peroksid
peroksid

Reputation: 357

How to find numpy.argmax() on part of list and save index?

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

Answers (3)

venpa
venpa

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

Ffisegydd
Ffisegydd

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

vinit_ivar
vinit_ivar

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

Related Questions