NumPy

NumPy is a Python library that provides fast and efficient operations on large multi-dimensional arrays. NumPy is the fundamental package for scientific computing in Python.

Installation

Install NumPy using conda:

conda install numpy

Install NumPy using pip:

pip install numpy

Import NumPy

To use NumPy, import the numpy module:

import numpy as np

NumPy Arrays

NumPy arrays are multi-dimensional arrays. NumPy arrays are created using the numpy.array() function.

Create a NumPy array:

import numpy as np

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

NumPy arrays can have multiple dimensions.

Create a 2D NumPy array:

import numpy as np

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

There are several other commonly used functions to create NumPy arrays:

import numpy as np

# Create an array of zeros
arr = np.zeros((2, 3))
print(arr)

# Create an array of ones
arr = np.ones((2, 3))
print(arr)

# Create an array using a range
arr = np.arange(1, 10, 2)
print(arr)

# Create an array using a range and reshape
arr = np.arange(12).reshape(3, 4)
print(arr)

In the last example, np.arange(12) creates an array of integers from 0 to 11. reshape(3, 4) reshapes the array to a 3x4 matrix. The resulting array is:

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

NumPy Array Attributes

NumPy arrays have several attributes that provide information about the array.

import numpy as np

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

print('Shape:', arr.shape) # (2, 3)
print('Dimensions:', arr.ndim) # 2

dtype

The dtype attribute specifies the data type of the elements in the array.

import numpy as np

arr = np.array([1, 2, 3, 4, 5], dtype='float32')
print(arr.dtype)

arr = np.array([1, 2, 3, 4, 5], dtype='int32')
print(arr.dtype)

NumPy Operations

Element-wise Operations

NumPy arrays support element-wise operations.

The following example demonstrates some common operations on one-dimensional NumPy arrays:

import numpy as np

arr1 = np.arange(4)
arr2 = np.arange(5, 9)

print('Addition:', arr1 + arr2)
print('Subtraction:', arr1 - arr2)
print('Multiplication:', arr1 * arr2)
print('Division:', arr1 / arr2)
print('Exponentiation:', arr1 ** arr2)
print('Sin:', np.sin(arr1))
print('arr1 = [2, 2, 2, 2]:', arr1 == 2)
Broadcasting

NumPy arrays support broadcasting. Broadcasting is a mechanism that allows NumPy to perform operations on arrays of different shapes.

Consider a numpy array a = np.array([[1, 2, 3]]) and a scalar b = 2. To compute a + b, NumPy broadcasts the scalar b to the shape of a as [2, 2, 2].

NumPy arrays also support matrix operations.

import numpy as np

matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.arange(4).reshape(2, 2)

print('element-wise multiplication:', matrix1 * matrix2)

In this example, matrix1 is

[[1 2]
 [3 4]]

and matrix2 is

[[0 1]
 [2 3]]

The result of the element-wise multiplication, matrix1 * matrix2, is

[[0 2]
 [6 12]]

Dot Product

The np.dot() function computes the dot product of two arrays.

[123][456]=32\begin{bmatrix} 1 & 2 & 3 \end{bmatrix} \begin{bmatrix} 4 \\ 5 \\ 6 \end{bmatrix} = 32
import numpy as np

vector1 = np.array([1, 2, 3])
vector2 = np.array([4, 5, 6])

print(np.dot(vector1, vector2))
[1234][0123]=[47815]\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \begin{bmatrix} 0 & 1 \\ 2 & 3 \end{bmatrix} = \begin{bmatrix} 4 & 7 \\ 8 & 15 \end{bmatrix}
import numpy as np

matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[0, 1], [2, 3]])

print(np.dot(matrix1, matrix2))

The result of the dot product is

[[ 4  7]
 [ 8 15]]

NumPy Indexing

NumPy arrays support indexing and slicing.

import numpy as np

# one-dimensional array
arr = np.array([1, 2, 3, 4, 5])
print(arr[0])

# two-dimensional array
arr = np.arange(12).reshape(3, 4)
print('Row 1:', arr[0])
print('Element (1, 1):', arr[0, 0])

# slicing
print('Row 1, Column 1-2:', arr[0, 0:2])

Transpose and flatten

[01234567891011]T=[04815926103711]\begin{bmatrix} 0 & 1 & 2 & 3 \\ 4 & 5 & 6 & 7 \\ 8 & 9 & 10 & 11 \end{bmatrix} ^T = \begin{bmatrix} 0 & 4 & 8 \\ 1 & 5 & 9 \\ 2 & 6 & 10 \\ 3 & 7 & 11 \end{bmatrix}
import numpy as np

arr = np.arange(12).reshape(3, 4)
print('Original:', arr)
print('Transpose:', arr.T)

Numpy also provides the flatten() function to flatten an array into a one-dimensional array.

import numpy as np

arr = np.arange(12).reshape(3, 4)
print('Original:', arr)
print('Flatten:', arr.flatten())

After flattening the array, the result is

[ 0  1  2  3  4  5  6  7  8  9 10 11]

Stack and concatenate

vstack

np.vstack() function stacks arrays vertically.

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.vstack((a, b)))

hstack

np.hstack() function stacks arrays horizontally.

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.hstack((a, b)))

concatenate

np.concatenate() function concatenates arrays along a specified axis. The arrays must have the same shape, except in the dimension corresponding to the axis.

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])

# a.shape = (2, 2), b.shape = (1, 2)
print(np.concatenate((a, b), axis=0))

# a.shape = (2, 2), b.T.shape = (2, 1)
print(np.concatenate((a, b.T), axis=1))

split

np.split() function splits an array into multiple sub-arrays.

Consider the following matrix:

[01234567891011]\begin{bmatrix} 0 & 1 & 2 & 3 \\ 4 & 5 & 6 & 7 \\ 8 & 9 & 10 & 11 \end{bmatrix}
import numpy as np

a = np.arange(12).reshape(3, 4)
# split a into 2 sub-arrays along the axis=1
print(np.split(a, 2, axis=1))'
# split a into 3 sub-arrays along the axis=0
print(np.split(a, 3, axis=0))

a = np.array([1, 2, 3, 4, 5, 6])
print(np.split(a, [2, 3]))
# [array([1, 2]), array([3]), array([4, 5, 6])]

If the second parameter is an array of sorted integers, the array is split at the indices specified by the integers. For example, np.split(a, [2, 3]) splits the array as

  • a[:2]

  • a[2:3]

  • a[3:]

copy

np.copy() returns a copy of the array.

import numpy as np

x = np.array([1, 2, 3])
y = x
z = np.copy(x)
x[0] = 10
print(f'x: {x}, y: {y}, z: {z}')
# x: [10  2  3], y: [10  2  3], z: [1 2 3]

After changing the value of x, the value of y is also changed because y is a reference to x. The value of z remains unchanged because z is a copy of x.

It is important to note that np.copy() is a shallow copy. It will not copy objects within the array.

import numpy as np

x = np.array([1, 'hello', [1, 2, 3]], dtype=object)
y = np.copy(x)
x[2][0] = 10
print(f'x: {x},\ny: {y}')
# x: [1 'hello' list([10, 2, 3])],
# y: [1 'hello' list([10, 2, 3])]

To copy objects within the array, use deepcopy from the copy module.

import numpy as np
import copy

x = np.array([1, 'hello', [1, 2, 3]], dtype=object)
y = copy.deepcopy(x)
x[2][0] = 10
print(f'x: {x},\ny: {y}')
# x: [1 'hello' list([10, 2, 3])],
# y: [1 'hello' list([1, 2, 3])]

Last updated