I have an array of values, said `v`, (e.g. `v=[1,2,3,4,5,6,7,8,9,10]`) and an array of indexes, say `g`(e.g. `g=[0,0,0,0,1,1,1,1,2,2]`).

I know, for instance, how to take the first element of each group, in a very numpythonic way, doing:

``````import numpy as np
v=np.array([1,2,3,4,74,73,72,71,9,10])
g=np.array([0,0,0,0,1,1,1,1,2,2])

returns:

``array([1, 74, 9])``

Is there any `numpy`thonic way (avoiding explicit loops) to get the maximum of each subset?

Nov 9, 2018 in Python 1,347 views

## 1 answer to this question.

You can use np.maximum.reduceat:

```>>> _, idx = np.unique(g, return_index=True)
>>> np.maximum.reduceat(v, idx)
array([ 4, 74, 10])```
• 3,560 points

## How do you get the logical xor of two variables in Python?

What i found is that you can use ...READ MORE

## how do I check the length of an array in a python program?

lets say we have a list mylist = ...READ MORE

## how do i get the list of all the keys in my dictionary?

my_dict = {'one': 'first', 'two': 'second', 'three': ...READ MORE

+1 vote

## how do i get a optimal cutoff value(directly in the form of numeric) from roc curves?

The optimal CutOff value is the point ...READ MORE

## How do I get the number of elements in a list?

Hello, The len() function can be used with several different ...READ MORE