0

I've just started using NumPy and I get that this is a very basic and wierd thing to point out but I noticed that the shape function acts differently when called on a 1D array vs when called on a 2D array.

Here's how it returns the results in 1D array -

    arr=np.array([1,2,3])
    arr.shape
OUTPUT - (3,)
In this the number of columns is represented before the " , " ie its represented as (columns,)

And here's how it returns the results in 2D array -

    arr1=np.array([[1,2,3,],[4,5,6]])
    arr1.shape
OUTPUT - (2,3)
In this the number of columns is represented after the " , ".  ie its represented as (rows,columns).

This is not something that effects its functionality or its usefulness but I was still wondering why it returns the output in case of a 1D array in that way. Even if the shape is 1D and the number of rows is left blank, why does the number of columns appear before the " , " ?

Ayush
  • 13
  • 5
  • 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) – Mayank Porwal Apr 14 '20 at 11:48
  • The comma is part of python's `tuple` representation. `shape` is a `tuple` `numpy` is a python package. – hpaulj Apr 14 '20 at 11:58

1 Answers1

0

It's because the shape of a numpy's array is a tuple. You can see it here:

import numpy as np

arr = np.array([1, 2, 3])
print(type(arr.shape))

# output: <class 'tuple'>

And tuples in general are shown like that. Look at the example below:

arr = tuple([1])
print(arr)

# output: (1,)
Farhood ET
  • 995
  • 6
  • 21