0 votes

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])
mask=np.concatenate(([True],np.diff(g)!=0))
v[mask]``````

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,397 views

## 1 answer to this question.

0 votes

You can use np.maximum.reduceat:

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

0 votes
1 answer

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

If you're already normalizing the inputs to ...READ MORE

0 votes
1 answer

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

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

0 votes
2 answers

## How can I get the count of a list in Python?

n=[1,2,3,4,5,6,7,8,9] print(len(n)) =9 READ MORE

0 votes
1 answer

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

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

0 votes
1 answer

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

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

+1 vote
1 answer

## 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

0 votes
1 answer

## how do i get anova table of split plot design or factorial experimental design in python?

Hello @ kapil, For your query you can refer ...READ MORE

0 votes
1 answer

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

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

0 votes
1 answer

## How do I obtain the index list in a NumPy Array of all the NaN values present using Python?

Hi, it is pretty simple, to be ...READ MORE

+1 vote
1 answer

## How do I find out the sum of digits of a number in Python?

Hi, good question. If you are considering ...READ MORE