3

An example:

    >>> import numpy as np    
    >>> list = [1,2,3,4]
    >>> array = np.asarray(list)
    >>> np.shape(array)
    (4,)

Now say I want to process a general array and read the number of rows and columns into variables m and n respectively, I would do:

>>> m, n = np.shape(array)

But this results in the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack

for the example above. In my example above I would have thought m=1 and n = 4 would instead have been an appropriate result. What am I missing?

3 Answers 3

5

Your array has ndim=1, which means len(array.shape)==1. Thus, you cannot unpack the shape tuple as if it were of length==2.

To "stretch" your array to have 2dim in case it currently has fewer, use np.atleast_2d.

>>> x = np.arange(3.0)
>>> y = np.atleast_2d(x)
>>> y
array([[ 0.,  1.,  2.]])
>>> m, n = y.shape

BTW, list and array are not good names for variables in python.

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

1 Comment

This makes m, n the wrong way around. m is the number of rows and n is the number of features. atleast_2d will give you (1, m) given a 1d array.
3

You showed us that:

>>> np.shape(array)
    (4,)

that is, it is a single element tuple.

m, n = (4,)

produces the same error. There is one element in the tuple, so Python can only unpack it into 1 variable. This isn't a numpy issue. When doing this kind of unpacking, the numer of variables has to match the number of terms in the tuple (or list).

If you come from MATLAB you may expect all arrays to be 2d or larger. But in numpy, arrays can be 1d or even 0d (with shape ()). There are multiple of ensuring that your array has 2 dimensions - reshape, extra [], [None,...], np.atleast_2d.

Comments

1

It's because numpy handle this array as 1D array and np.shape returns the value of shape of this 1D array, which is a tuple with a single element.
Thus we can alter it to a 2D array with adding [ ] like [[1,2,3,4]].

You can do as follows:

import numpy as np    

list = [[1,2,3,4]]
array = np.asarray(list)
print np.shape(array)
m, n = np.shape(array)
print m,n

Output:

(1, 4)
1 4

Alternatively you can do this too:

import numpy as np    

list = [1,2,3,4]
list = [list]
array = np.asarray(list)
print np.shape(array)
m, n = np.shape(array)
print m,n

Output:

(1, 4)
1 4

I hope, it helps.

And yes, you should avoid using the names list and array as variable name in Python.

4 Comments

Thanks. Can you please explain why the extra [ ] makes the difference?
Because numpy handle the array as 1D array and np.shape return the value of shape of this 1D array, when add [], we made a 2D array.
Great, that makes sense - +1 for your effort and good explanation.
Thank you, I am happy to help you. However, you didn't accept this as the answer. :)

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.