# How to get Numpy Array Dimensions using numpy.ndarray.shape & numpy.ndarray.size() in Python

In this article we will discuss how to count number of elements in a 1D, 2D & 3D Numpy array, also how to count number of rows & columns of a 2D numpy array and number of elements per axis in 3D numpy array.

## Get the Dimensions of a Numpy array using ndarray.shape()

#### numpy.ndarray.shape

Python’s Numpy Module provides a function to get the dimensions of a Numpy array,

`ndarray.shape`

It returns the dimension of numpy array as tuple.

Let’s use this to get the shape or dimensions of a 2D & 1D numpy array i.e.

### Get Dimensions of a 2D numpy array using ndarray.shape

Let’s create a 2D Numpy array i.e.

```# Create a 2D Numpy array list of list
arr2D = np.array([[11 ,12,13,11], [21, 22, 23, 24], [31,32,33,34]])

print('2D Numpy Array')
print(arr2D)
```

Output:

```2D Numpy Array
[[11 12 13 11]
[21 22 23 24]
[31 32 33 34]]
```

Get number of rows in this 2D numpy array i.e.

```# get number of rows in 2D numpy array
numOfRows = arr2D.shape[0]

print('Number of Rows : ', numOfRows)
```

Output:

`Number of Rows :  3`

Get number of columns in this 2D numpy array,

```# get number of columns in 2D numpy array
numOfColumns = arr2D.shape[1]

print('Number of Columns : ', numOfColumns)
```

Output:

`Number of Columns :  4`

Get total number of elements in this 2D numpy array,

`print('Total Number of elements in 2D Numpy array : ', arr2D.shape[0] * arr2D.shape[1])`

Output:

`Total Number of elements in 2D Numpy array :  12`

### Get Dimensions of a 1D numpy array using ndarray.shape

Let’s create a 1D Numpy array i.e.

```# Create a Numpy array from list of numbers
arr = np.array([4, 5, 6, 7, 8, 9, 10, 11])
```

Get number of elements of this 1D numpy array i.e.

```print('Shape of 1D numpy array : ', arr.shape)
print('length of 1D numpy array : ', arr.shape[0])
```

Output:

```Shape of 1D numpy array :  (8,)
length of 1D numpy array :  8
```

## Get the Dimensions of a Numpy array using numpy.shape()

Python’s Numpy module provides a function to get the number of elements in a Numpy array along axis i.e.

`numpy.size(arr, axis=None)`

Args: It accepts the numpy array and also the axis along which it needs to count the elements.If axis is not passed then returns the total number of arguments.
Returns: The number of elements along the passed axis.

Let’s use this to get the shape or dimensions of a 2D & 1D numpy array i.e.

### Get Dimensions of a 2D numpy array using numpy.size()

Let’s create a 2D Numpy array i.e.

```# Create a 2D Numpy array list of list
arr2D = np.array([[11 ,12,13,11], [21, 22, 23, 24], [31,32,33,34]])

print('2D Numpy Array')
print(arr2D)
```

Output:

```2D Numpy Array
[[11 12 13 11]
[21 22 23 24]
[31 32 33 34]]
```

Get number of rows and columns of this 2D numpy array:

```# get number of rows in 2D numpy array
numOfRows = np.size(arr2D, 0)

# get number of columns in 2D numpy array
numOfColumns = np.size(arr2D, 1)

print('Number of Rows : ', numOfRows)
print('Number of Columns : ', numOfColumns)
```

Output:

```Number of Rows :  3
Number of Columns :  4
```

Get total number of elements in this 2D numpy array:

`print('Total Number of elements in 2D Numpy array : ', np.size(arr2D))`

Output:

`Total Number of elements in 2D Numpy array :  12`

## Get Dimensions of a 3D numpy array using numpy.size()

Let’s create a 3D Numpy array i.e.

```# Create a 3D Numpy array list of list of list
arr3D = np.array([ [[11, 12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]],
[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]] ])

print(arr3D)
```

Output:

```[[[11 12 13 11]
[21 22 23 24]
[31 32 33 34]]

[[ 1  1  1  1]
[ 2  2  2  2]
[ 3  3  3  3]]]
```

Get number of elements per axis in 3D numpy array i.e.

```print('Axis 0 size : ', np.size(arr3D, 0))
print('Axis 1 size : ', np.size(arr3D, 1))
print('Axis 2 size : ', np.size(arr3D, 2))
```

Output:

```Axis 0 size :  2
Axis 1 size :  3
Axis 2 size :  4
```

Get total number of elements in this 3D numpy array i.e.

`print('Total Number of elements in 3D Numpy array : ', np.size(arr3D))`

Output:

`Total Number of elements in 3D Numpy array :  24`

### Get Dimensions of a 1D numpy array using numpy.size()

Let’s create a 1D Numpy array i.e.

```# Create a Numpy array from list of numbers
arr = np.array([4, 5, 6, 7, 8, 9, 10, 11])```

Get number of elements of this 1D numpy array using numpy.size() i.e.

`print('Length of 1D numpy array : ', np.size(arr))`

Output:

`Length of 1D numpy array :  8`

Complete example is as follows:

```import numpy as np

def main():

print('**** Get Dimensions of a 2D numpy array using ndarray.shape ****')

# Create a 2D Numpy array list of list
arr2D = np.array([[11 ,12,13,11], [21, 22, 23, 24], [31,32,33,34]])

print('2D Numpy Array')
print(arr2D)

# get number of rows in 2D numpy array
numOfRows = arr2D.shape[0]

# get number of columns in 2D numpy array
numOfColumns = arr2D.shape[1]

print('Number of Rows : ', numOfRows)
print('Number of Columns : ', numOfColumns)

print('Total Number of elements in 2D Numpy array : ', arr2D.shape[0] * arr2D.shape[1])

print('**** Get Dimensions of a 1D numpy array using ndarray.shape ****')

# Create a Numpy array from list of numbers
arr = np.array([4, 5, 6, 7, 8, 9, 10, 11])

print('Original Array : ', arr)

print('Shape of 1D numpy array : ', arr.shape)
print('length of 1D numpy array : ', arr.shape[0])

print('**** Get Dimensions of a 2D numpy array using np.size() ****')

# Create a 2D Numpy array list of list
arr2D = np.array([[11, 12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]])

print('2D Numpy Array')
print(arr2D)

# get number of rows in 2D numpy array
numOfRows = np.size(arr2D, 0)

# get number of columns in 2D numpy array
numOfColumns = np.size(arr2D, 1)

print('Number of Rows : ', numOfRows)
print('Number of Columns : ', numOfColumns)

print('Total Number of elements in 2D Numpy array : ', np.size(arr2D))

print('**** Get Dimensions of a 3D numpy array using np.size() ****')

# Create a 3D Numpy array list of list of list
arr3D = np.array([ [[11, 12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]],
[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]] ])

print('3D Numpy Array')
print(arr3D)

print('Axis 0 size : ', np.size(arr3D, 0))
print('Axis 1 size : ', np.size(arr3D, 1))
print('Axis 2 size : ', np.size(arr3D, 2))

print('Total Number of elements in 3D Numpy array : ', np.size(arr3D))

print('Dimension by axis : ', arr3D.shape)

print('**** Get Dimensions of a 1D numpy array using numpy.size() ****')

# Create a Numpy array from list of numbers
arr = np.array([4, 5, 6, 7, 8, 9, 10, 11])

print('Original Array : ', arr)

print('Length of 1D numpy array : ', np.size(arr))

if __name__ == '__main__':
main()```

Output:

```**** Get Dimensions of a 2D numpy array using ndarray.shape ****
2D Numpy Array
[[11 12 13 11]
[21 22 23 24]
[31 32 33 34]]
Number of Rows :  3
Number of Columns :  4
Total Number of elements in 2D Numpy array :  12
**** Get Dimensions of a 1D numpy array using ndarray.shape ****
Original Array :  [ 4  5  6  7  8  9 10 11]
Shape of 1D numpy array :  (8,)
length of 1D numpy array :  8
**** Get Dimensions of a 2D numpy array using np.size() ****
2D Numpy Array
[[11 12 13 11]
[21 22 23 24]
[31 32 33 34]]
Number of Rows :  3
Number of Columns :  4
Total Number of elements in 2D Numpy array :  12
**** Get Dimensions of a 3D numpy array using np.size() ****
3D Numpy Array
[[[11 12 13 11]
[21 22 23 24]
[31 32 33 34]]

[[ 1  1  1  1]
[ 2  2  2  2]
[ 3  3  3  3]]]
Axis 0 size :  2
Axis 1 size :  3
Axis 2 size :  4
Total Number of elements in 3D Numpy array :  24
Dimension by axis :  (2, 3, 4)
**** Get Dimensions of a 1D numpy array using numpy.size() ****
Original Array :  [ 4  5  6  7  8  9 10 11]
Length of 1D numpy array :  8```

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top