NumPy - Ndarray Object

NumPy ndarray

The most important object defined in NumPy is an N-dimensional array type called ndarray. It describes a collection of items of the same type, which can be accessed using a zero-based index.

Each item in an ndarray takes the same size of block in the memory and is represented by a data-type object called dtype.

Any item extracted from an ndarray object (by slicing) is represented by a Python object of one of the array scalar types.

Relationship Between ndarray, dtype, and Array Scalar Type

The following diagram shows a relationship between ndarray, the data type object dtype and array scalar types −

Ndarray

Creating an ndarray

An instance of ndarray class can be constructed by different array creation routines. The basic ndarray is created using the array() function in NumPy.

The numpy.array() function creates an ndarray from any object exposing the array interface or from any method that returns an array. Following is the syntax −

numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)

The above constructor takes the following parameters −

Sr.No.Parameter & Description
1objectAny object exposing the array interface method returns an array, or any (nested) sequence.
2dtypeDesired data type of array, optional
3copyOptional. By default (true), the object is copied
4orderC (row major) or F (column major) or A (any) (default)
5subokBy default, returned array forced to be a base class array. If true, sub-classes passed through
6ndminSpecifies minimum dimensions of resultant array

Take a look at the following examples to understand better.

Example: Create a One-dimensional Array

In the following example, we are creating a 1D array using the array() function in NumPy −

import numpy as np
a = np.array([1, 2, 3])
print(a)

The output obtained is as follows −

[1, 2, 3]

Example: Create a Multi-dimensional Array

Here, we are creating a 2D array using the array() function in NumPy −

import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a)

This will produce the following result −

[[1 2]
 [3 4]]

Example: Specify Minimum Dimensions

Now, we are specifying the minimum dimensions of the resultant array by passing “ndmin” as an argument to the array() function −

import numpy as np
a = np.array([1, 2, 3, 4, 5], ndmin=2)
print(a)

Following is the output of the above code −

[[1 2 3 4 5]]

Example: Specify Data Type

In the example below, we are setting the “dtype” parameter as complex in the array() function −

import numpy as np
a = np.array([1, 2, 3], dtype=complex)
print(a)

After executing the above code, we get the following output −

[1.+0.j 2.+0.j 3.+0.j]

Memory Layout of ndarray

An ndarray object consists of a contiguous block of memory. This means that all elements in the array are stored in a single, continuous segment of memory. This contiguous memory layout allows for easy access and manipulation of array elements.

Indexing Scheme

The indexing scheme maps each item in the ndarray to a specific location in the memory block. The ndarray uses a combination of shape and strides to determine the location of each element.

  • Shape − The shape of an array is a tuple of integers representing the size of the array along each dimension. For example, the shape of a 2x3 array is (2, 3).
  • Strides − Strides are the number of bytes to step in each dimension when traversing the array. The strides determine how to move from one element to the next in each dimension.

Row-major and Column-major Orders

The elements in an ndarray can be stored in two primary orders. They are −

  • Row-major Order (C-style) − In row-major order, the last index changes the fastest. This means that elements in the same row are stored next to each other in memory. NumPy uses row-major order by default. For example, in a 2x3 array −

    [[1, 2, 3],
     [4, 5, 6]]
    

    The elements are stored in memory as: 1, 2, 3, 4, 5, 6.

  • Column-major Order (FORTRAN-style) − In column-major order, the first index changes the fastest. This means that elements in the same column are stored next to each other in memory. For example, in a 2x3 array −

    [[1, 2, 3],
     [4, 5, 6]]
    

    The elements are stored in memory as: 1, 4, 2, 5, 3, 6.

Example

Following is a basic example to demonstrate the usage of the memory layout −

import numpy as np

# Creating a 2x3 array in row-major order
a = np.array([[1, 2, 3], [4, 5, 6]])
print("Array:")
print(a)
print("Shape:", a.shape)
print("Strides:", a.strides)

The shape of the array is (2, 3), indicating it has 2 rows and 3 columns. The strides are (24, 8), meaning that to move to the next row, we need to skip 24 bytes (since each element is an 8-byte integer, and there are 3 columns), and to move to the next column, we need to skip 8 bytes (the size of one integer) as shown in the output below −

Array:
[[1 2 3]
 [4 5 6]]
Shape: (2, 3)
Strides: (24, 8)
© 2024 All rights reserved. | Made With 🤍 By The_MAK Team