NumPy Introduction

create NumPy ndarray objects

NumPy is used to process arrays. The array objects in NumPy are called ndarray.

We can use array() function creates a NumPy ndarray object.

Example

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

Run Example

type(): This built-in Python function tells us the type of the object passed to it. Like the above code, it indicates arr is numpy.ndarray type.

to create ndarray, we can pass a list, tuple, or any similar array-like object to array() method, then it will be converted to ndarray:

Example

Create a NumPy array using a tuple:

import numpy as np 
arr = np.array((1, 2, 3, 4, 5))
print(arr)

Run Example

The dimensions in the array

The dimensions in the array are a level of the array depth (nested arrays).

Nested arrays:refers to an array of arrays as elements.

0-D array

0-D array, or scalar (Scalars), are the elements of the array. Each value in the array is a 0-D array.

Example

Create a 0-D array using the value 61:

import numpy as np
arr = np.array(61)
print(arr)

Run Example

1-D array

Its elements are arrays of 0-D arrays, known as one-dimensional or 1-D arrays.

This is the most common and basic array.

Example

Create a 1-D array containing the values 1, 2, 3, 4, 5, 6:

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

Run Example

2-D array

Its elements are arrays of 1-D arrays, known as 2-D arrays.

They are usually used to represent matrices or second-order tensors.

NumPy has a complete sub-module dedicated to matrix operations numpy.mat.

Example

Create a 2-D array containing two arrays of values 1, 2, 3 and 4, 5, 6:

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

Run Example

3-D array

Its elements are arrays of 2-D arrays, known as 3-D arrays.

Example

Create a 3-D array using two 2-D arrays, both containing the values 1, 2, 3 and 4, 5, 6:

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

Run Example

Check the number of dimensions?

NumPy arrays provide ndim Attribute, which returns an integer indicating the number of dimensions of the array.

Example

Check the number of dimensions of the array:

import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim) 
print(b.ndim) 
print(c.ndim) 
print(d.ndim)

Run Example

Higher-dimensional arrays

An array can have any number of dimensions.

You can use it when creating an array. ndmin Parameter defines the dimension.

Example

Create an array with 5 dimensions and verify that it has 5 dimensions:

import numpy as np
arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr)
print('number of dimensions :', arr.ndim)

Run Example

In this array, the innermost dimension (the 5th dim) has 4 elements, the 4th dim has 1 element as a vector, the 3rd dim has 1 element that is a matrix with the vector, the 2nd dim has 1 element that is a 3D array, and the 1st dim has 1 element, which is a 4D array.