8

I would like to take the two smallest values from an array x. But when I use np.where:

A,B = np.where(x == x.min())[0:1]

I get this error:

ValueError: need more than 1 value to unpack

How can I fix this error? And do I need to arange numbers in ascending order in array?

18
  • 1
    Well, the [0:1] slicing syntax is equal to [:1] syntax, which means take the first element from that array. Why don't you do something like A, B = [np.where(x==x.min())] * 2 (unpacking a list)? You really don't need the slicing syntax, as there's only one minimal value in a given sequence. Commented May 16, 2017 at 13:06
  • 1
    ok, thank you for the clarification :) Could you edit your question accordingly? Commented May 16, 2017 at 13:39
  • 1
    Could you give us an example of what's in x and what you would like the corresponding output for A and B to be? Commented May 16, 2017 at 14:02
  • 1
    @MSeifert thanks. My x values are here: [ 0.0619444 0.01415524 0.0644649 0.00348524 0.01608257 0.03753657 0.0392854 0.0503084 0.10295524] Commented May 16, 2017 at 15:19
  • 1
    And the expected output would be 0.00348524 and 0.01415524 for A and B respectivly? Commented May 16, 2017 at 15:24

3 Answers 3

10

You can use numpy.partition to get the lowest k+1 items:

A, B = np.partition(x, 1)[0:2]  # k=1, so the first two are the smallest items

In Python 3.x you could also use:

A, B, *_ = np.partition(x, 1)

For example:

import numpy as np
x = np.array([5, 3, 1, 2, 6])
A, B = np.partition(x, 1)[0:2]
print(A)  # 1
print(B)  # 2
Sign up to request clarification or add additional context in comments.

Comments

1

There are two errors in the code. The first is that the slice is [0:1] when it should be [0:2]. The second is actually a very common issue with np.where. If you look into the documentation, you will see that it always returns a tuple, with one element if you only pass one parameter. Hence you have to access the tuple element first and then index the array normally:

A,B = np.where(x == x.min())[0][0:2]

Which will give you the two first indices containing the minimum value. If no two such indices exist you will get an exception, so you may want to check for that.

2 Comments

@MSeifert Yes, well, I assumed that was expected to be true, although I guess it could be checked. Btw, I understood the question as "give me the first two indices with the minimum value", although maybe it was "give me the two smallest values"?
Sorry, you are right. It should give me the two smallest values. But "not_a_robot" says: there's only one minimal value in a given sequence. I am confused.
0

How about using sorted instead of np.where?

A,B = sorted(x)[:2]

1 Comment

Seems like that is how everyone is reading the question, including myself and @MSeifert. 1) Partitioning is MUCH more efficient than sorting. 2) OP is looking for multiple occurrences of the same value.

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.