Nemo
Nemo

Reputation: 1227

Shape 1 of numpy array

Consider

x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])

In Python's view, x has shape (4, 3) and v shape (3, ). Why didn't Python view v as having shape (, 3). Also, why do v and v.T have the same shape (3, ). IMHO, I think if v has shape (3, ) then v.T should have shape (, 3)?

Upvotes: 0

Views: 1708

Answers (2)

RebornCodeLover
RebornCodeLover

Reputation: 88

As you know, numpy arrays are n-dimensional. The shape tells dimensions in the order. If it is 1-D you will see only 1st dimension, 2-D only 2 dimensions and so on.

Here x is a 2-D array while v is a 1-D array (aka vector). That is why when you do shape on v you see (3,) meaning it has only one dimension whereas x.shape gives (4,3). When you transpose v, then that is also a 1-D array. To understand this better, try another example. Create a 3-D array.

z=np.ones((5,6,7))
z.shape
print (z)

Upvotes: 1

iz_
iz_

Reputation: 16593

(3,) does not mean the 3 is first. It is simply the Python way of writing a 1-element tuple. If the shape were a list instead, it would be [3].

(, 3) is not valid Python. The syntax for a 1-element tuple is (element,).

The reason it can't be just (3) is that Python simply views the parentheses as a grouping construct, meaning (3) is interpreted as 3, an integer.

Upvotes: 2

Related Questions