Technology3 min read

NumPy's ndarray: Multi-Dimensional Arrays in Python

Abhishek Ahlawat

By Abhishek Ahlawat — MAY 16, 2023

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

#numpy#python#multidimensional-array

Summarize this article using AI

NumPy was created in 2005 by Travis Oliphant by combining features of Numarray into Numeric package.
Its main objects are multi-Dimensional (D) array. It is a table of elements, indexed by a tuple of positive integers.

Think of these arrays as tables filled with elements, and you can access those elements using a combination of positive integers.

To help you understand this concept better, let's look at an example:

[[1, 0, 8],
 [0, 1, 1]]

In this example:

  • The first dimension (or axis) has a length of 2
  • The second dimension (or axis) has a length of 3
  • The rank of this array is 2, which means it's a 2-dimensional (2-D) array.


Axes : NumPy Dimensions
Rank : No. of axes

NumPy array class is known as ndarray.

Attributes of ndarray class:

Ndarray has several attributes that are useful to know:

  • ndarray.ndim : This tells Rank of array which is number of axes of the array.
  • ndarray.shape : This provides the dimensions of array. This is a tuple of integers indicating the size of array.
    (n, m) : n(rows), m(column).
  • ndarray.size : Total number of elements in the array (n * m).
  • ndarray.dtype : An object describing the type of elements in the array. One can create or specify data types using standard Python types. e.g. numpy.int32, numpy.float 64
  • ndarray.itemsize : dtype/8 – Equivalent to ndarray.dtype.itemsize.
  • ndarray.data : Actual elements of the array are stored in this buffer.

Python program for illustration:

Let's see a Python code example to illustrate the working of the ndarray class:

# Python Program illustrating the working of ndarray class

import numpy as np

# Array declaration
a = np.arange(10).reshape(2, 5)

print("array : \n", a)

# use of ndarray.shape
print("\nndarray.shape : ", a.shape, "\n")

# use of ndarray.ndim
print("ndarray.ndim : ", a.ndim, "\n")

# use of ndarray.dtype
print("ndarray.dtype : ", a.dtype, "\n")

# use of ndarray.size
print("ndarray.size : ", a.size, "\n")

# use of ndarray.itemsize - dtype/8
print("ndarray.itemsize : ", a.itemsize, "\n")

Output :


array :
[[0 1 2 3 4]
[5 6 7 8 9]]

ndarray.shape : (2, 5)

ndarray.ndim : 2

ndarray.dtype : int32

ndarray.size : 10

ndarray.itemsize : 4

I hope these additional explanations help you understand the basics of NumPy's ndarray class.

Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops — put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next