0

I am wondering x.shape[0] is whether row or column of the array.

I coded...

x=np.array([1,2,3,4])
y=np.array([[1,2,3],[4,5,6]])
print(x.shape)
print(y.shape)

(4,) (2,3)

in x.shape, the element at index 0 is column of the array, x. in y.shape, the element at index 0 is row of the array y.

I don't understand how shape function returns its output. please help :)

p.s. Also I don't understand why x.shape returns (4,). Why not (4)?

Inho Kang
  • 3
  • 1
  • 2
    Does this answer your question? [Difference between numpy.array shape (R, 1) and (R,)](https://stackoverflow.com/questions/22053050/difference-between-numpy-array-shape-r-1-and-r) – FBruzzesi Apr 29 '20 at 07:21
  • p.s.: Try print(type((4))) and then print(type((4,))) The first is an integer surrounded by parentheses. The second is a tuple. – ZSG Apr 29 '20 at 07:36
  • " Also I don't understand why x.shape returns (4,). Why not (4)" because `.shape` returns a tuple, that describes the shape, i.e. the size of each of the dimensions. `x` has a single dimension of length 4, `len(d.shape) == 1` and `d.shape[0] == 4)` – juanpa.arrivillaga Apr 29 '20 at 07:39
  • Did you consult the documentation for [`ndarray.shape`](https://numpy.org/devdocs/reference/generated/numpy.ndarray.shape.html#numpy.ndarray.shape) ? – AMC Apr 29 '20 at 07:51
  • New python users ignore or aren't aware of the special notation for a single element `tuple`. Instead the comma in `(4,)` is assumed to be some sort of missing dimension marker. – hpaulj Apr 29 '20 at 15:53

2 Answers2

0

Thinking about the shape of NumPy arrays in the form of rows and columns will quickly let you down if you start working with more complicated data.

NumPy arrays are in fact multi-dimensional tensors. A tensor can have any number of dimensions. Vectors and matrices are in fact nothing but 1-D and 2-D tensors, respectively.

The shape of an array returns the size of each dimension of the tensor. Let's begin with a simple vector:

x = np.array([1, 2, 3])

Calling .shape on this variable will return (3,) since the array is a 1-D tensor with 3 elements in its first dimension. The comma is just a Python convention for indicating that the parenthesis represent a Python Tuple.

Now let's try a matrix:

m = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

Running m.shape now results in (2, 3) since the first dimension of the tensor contains 2 vectors, and the second dimension (the vectors themselves) each contain 3 elements.

Finally, let's try a 3-D tensor:

t = np.array([
    [
        [1, 2, 3],
        [4, 5, 6]
    ],
    [
        [7, 8, 9],
        [0, 1, 2]
    ]
])

t.shape will return (2, 2, 3) which means that the first dimension of the array contains two elements (two matrices in this case), the second dimension (the matrices themsevles) contain two vectors each, and the third dimension (the vectors) each contain three elements.

0

I don't understand why x.shape returns (4,). Why not (4)?

shape is Tuple of array dimensions, which for 1-D array is tuple with single element. As it is always tuple you might easily find how many dimensions have array x by doing:

len(x.shape)

note that this would not be possible if x.shape would be (4) as len((4)) result in error, whilst len((4,)) gives 1. Regarding 2D arrays like:

y=np.array([[1,2,3],[4,5,6]])

shape is (number_of_rows, number_of_columns), knowing that y.shape is (2, 3) you might deduce that following will work:

y[0][0]  # 0 < 2 and 0 < 3
y[0][1]  # 0 < 2 and 1 < 3
y[0][2]  # 0 < 2 and 2 < 3
y[1][0]  # 1 < 2 and 0 < 3
y[1][1]  # 1 < 2 and 1 < 3
y[1][2]  # 1 < 2 and 2 < 3

while following (just some examples) will produce IndexError:

y[2][0]  # as 2 < 2 and 0 < 3 is False
y[0][3]  # as 0 < 2 and 3 < 3 is False
y[2][3]  # as 2 < 2 and 3 < 3 is False
Daweo
  • 10,139
  • 2
  • 5
  • 10