1

If I want to find the minimum value in each column of a NumPy array, I can use the numpy.amin() function. However, is there a way to find the two minimum values in each column, that is faster than sorting each column?

0

2 Answers 2

2

You can simply use np.partition along the columns to get smallest N numbers -

N = 2
np.partition(a,kth=N-1,axis=0)[:N]

This doesn't actually sort the entire data, simply partitions into two sections such that smallest N numbers are in the first section, also called as partial-sort.

Bonus (Getting top N elements) : Similarly, to get the top N numbers per col, simply use negative kth value -

np.partition(a,kth=-N,axis=0)[-N:]

Along other axes and higher dim arrays

To use it along other axes, change the axis value. So, along rows, it would be axis=1 for a 2D array and extend the same way for higher dimension ndarrays.

Sign up to request clarification or add additional context in comments.

Comments

0

Use the min() method, and specify the axis you want to average over:

a = np.random.rand(10,3)
a.min(axis=0)

gives:

array([0.04435587, 0.00163139, 0.06327353])


a.min(axis=1)

gives

array([0.01354386, 0.08996586, 0.19332211, 0.00163139, 0.55650945,
       0.08409907, 0.23015718, 0.31463493, 0.49117553, 0.53646868])

2 Comments

The problem was, I wanted the two smallest values per column, not just the minimum of each column. I didn't want to sort each column, since that seemed too computationally expensive. I wasn't aware of NumPy's partition function, which does exactly what I need.
Ah ok - gotcha!

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.