0

I want to construct a 1d numpy array a, and I know each a[i] has several possible values. Of course, the numbers of the possible values of any two elements of a can be different. For each a[i], I want to set it be the minimum value of all the possible values.

For example, I have two array:

idx = np.array([0, 1, 0, 2, 3, 3, 3])
val = np.array([0.1, 0.5, 0.2, 0.6, 0.2, 0.1, 0.3])

The array I want to construct is following:

a = np.array([0.1, 0.5, 0.6, 0.1])

So does there exist any function in numpy can finish this work?

2 Answers 2

1

Here's one approach -

def groupby_minimum(idx, val):
    sidx = idx.argsort()
    sorted_idx = idx[sidx]
    cut_idx = np.r_[0,np.flatnonzero(sorted_idx[1:] != sorted_idx[:-1])+1]
    return np.minimum.reduceat(val[sidx], cut_idx)

Sample run -

In [36]: idx = np.array([0, 1, 0, 2, 3, 3, 3])
    ...: val = np.array([0.1, 0.5, 0.2, 0.6, 0.2, 0.1, 0.3])
    ...: 

In [37]: groupby_minimum(idx, val)
Out[37]: array([ 0.1,  0.5,  0.6,  0.1])

Here's another using pandas -

import pandas as pd

def pandas_groupby_minimum(idx, val):
    df = pd.DataFrame({'ID' : idx, 'val' : val})
    return df.groupby('ID')['val'].min().values

Sample run -

In [66]: pandas_groupby_minimum(idx, val)
Out[66]: array([ 0.1,  0.5,  0.6,  0.1])
Sign up to request clarification or add additional context in comments.

Comments

0

You can also use binned_statistic:

from scipy.stats import binned_statistic

idx_list=np.append(np.unique(idx),np.max(idx)+1)
stats=binned_statistic(idx,val,statistic='min', bins=idx_list)
a=stats.statistic

I think, in older scipy versions, statistic='min' was not implemented, but you can use statistic=np.min instead. Intervals are half open in binned_statistic, so this implementation is safe.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.