1

For following:

d = np.array([[0,1,4,3,2],[10,18,4,7,5]])
print(d.shape)

Output is:

(2, 5)

It is expected.

But, for this(difference in number of elements in individual rows):

d = np.array([[0,1,4,3,2],[10,18,4,7]])
print(d.shape)

Output is:

(2,)

How to explain this behaviour?

1 Answer 1

2

Short answer: It parses it as an array of two objects: two lists.

Numpy is used to process "rectangular" data. In case you pass it non-rectangular data, the np.array(..) function will fallback on considering it a list of objects.

Indeed, take a look at the dtype of the array here:

>>> d
array([list([0, 1, 4, 3, 2]), list([10, 18, 4, 7])], dtype=object)

It is an one-dimensional array that contains two items two lists. These lists are simply objects.

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

2 Comments

Thanks for the input. Btw, any good numpy resource for python beginners?
There's a lot of numpy documentation. But the problem with your example is that it's a kind of error, trying to make a 2d array from lists that differ in size. np.array normally makes a multidimensional numeric array. Making the 1d object dtype array is a fall-back alternative. It get's a lot of attention in SO questions, not so much in the formal documentation or tutorials.

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.