Understanding NumPy array dimensions in Python

In this tutorial, we will learn the NumPy array and its dimensions in Python. The array is used to store multiple values in one single variable.

Understanding What Is Numpy Array

Numpy array stands for Numerical Python. Numpy array is a library consisting of multidimensional array objects. It can be used to solve mathematical and logical operation on the array can be performed.

Creating a NumPy Array And Its Dimensions

Here we show how to create a Numpy array.  When we create Numpy array first we need to install the Numpy library package in our using IDE, after then we write our code as an import NumPy as np then after it will be working our writing code.

Here we give an example to create a zero-dimensional array:

import numpy as np
a=np.array(5)
print(a)
print(np.ndim(a))

The given example has output is:

5
0

Here array contains element 5 and its dimension is 0.

Here we give an example to create a one-dimensional array:

import numpy as np
a=np.array([10,20,30,40,50])
print(a)
print(a.ndim)

The given example output is:

[10 20 30 40 50]
1

Here the given example dimension is 1.

Here we give an example to create a two-dimensional array:

import numpy as np
a=np.array([10,20,30],[40,50,60],[70,80,90])
print(a)
print(np.ndim(a))

The given example has output is:

[[10 20 30]
 [40 50 60]
 [70 80 90]]
2

Here the given example dimension is 2.

Here we give an example to create a three-dimensional array:

import numpy as np
a=np.array([
           [[1,2,3],[4,5,6],[7,8,9]],
           [[9,8,7],[6,5,4],[3,2,1]],
           [[1,2,3],[6,5,6],[7,8,9]]
           ])

The given example has output is:

[[[1 2 3]
  [4 5 6]
  [7 8 9]]

 [[9 8 7]
  [6 5 4]
  [3 2 1]]

 [[1 2 3]
  [6 5 6]
  [7 8 9]]]
3

The given example has 3 dimensions.

Above the given all example have one variable contains the array element. Given the out has come to matrix size to know-how much dimension is there.

The above example is understood to how to create a Numpy array and its dimension also in Python.

Also, read: Multiplication of two matrices in Python using NumPy

Leave a Reply

Your email address will not be published. Required fields are marked *