Mandroid
Mandroid

Reputation: 7478

numpy array.shape behaviour

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?

Upvotes: 1

Views: 46

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476574

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.

Upvotes: 2

Related Questions