NumPy Array Operations

Arithmetic, statistical, and transformation operations.

15 min read
Beginner

NumPy Array Operations

In this lesson, you will be learning how to perform arithmetic, statistical and transformation operations on NumPy arrays.

Arithmetic Operations on NumPy Arrays

Various arithmetic operations such as addition, subtraction, multiplication, division, etc can be performed on NumPy arrays.

Such operations can be either performed between NumPy arrays of similar shape or between a NumPy array and a number. Following are some of the examples of arithmetic operations on NumPy arrays:

arr1=[1234]\text{arr1} = \begin{bmatrix} 1 & 2 & 3 & 4 \end{bmatrix} arr2=[2468]\text{arr2} = \begin{bmatrix} 2 & 4 & 6 & 8 \end{bmatrix}
python
# Importing the NumPy library as np
import numpy as np

# Creating a NumPy array from a Python List
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([2, 4, 6, 8])

# Printing the arrays
print("arr1: ", arr1)
print("arr2: ", arr2)

# Arithmetic operations between an array and a number
print("arr1 + 2: ", arr1 + 2)
print("arr1 - 2: ", arr1 - 2)
print("arr1 * 2: ", arr1 * 2)
print("arr1 / 2: ", arr1 / 2)

# Arithmetic operations between NumPy arrays
print("arr1 + arr2: ", arr1 + arr2)
print("arr1 - arr2: ", arr1 - arr2)
print("arr1 * arr2: ", arr1 * arr2)
print("arr1 / arr2: ", arr1 / arr2)

Statistical Operations on NumPy arrays

NumPy contains various in-built functions to get statistical information regarding the array such as the maximum or minimum value in the array, the mean or median of the array, etc. Below is a table of built-in NumPy functions for performing such operations:

Statistical Operations
Statistics
Built-In NumPy Functions
Minimum Valuenumpy.min()
Maximum Valuenumpy.max()
Mean Valuenumpy.mean()
Median Valuenumpy.median()
Standard deviationnumpy.std()
Get count of unique valuesnumpy.unique()
arr1=[1234]\text{arr1} = \begin{bmatrix} 1 & 2 & 3 & 4 \end{bmatrix}
python
# Creating a NumPy array from a Python List
arr1 = np.array([1, 2, 3, 4])

# Printing the array
print("arr1: ", arr1)

# Minimum value
print("Min: ", np.min(arr1))

# Maximum Value
print("Max: ", np.max(arr1))

# Mean
print("Mean: ", np.mean(arr1))

# Median
print("Median: ", np.median(arr1))

# Standard Deviation
print("Standard Deviation: ", np.std(arr1))
python
arr1 = np.array([1, 1, 1, 2])
# Get unique values and their counts
uniqs, counts = np.unique(arr1, return_counts=True)
print("Unique values: ", uniqs)
print("Count of respective unique values: ", counts)

Transformation Operations on NumPy arrays

Transformation Operations on NumPy arrays can help transform the shape and order of elements in a NumPy array. Below is a table of built-in NumPy functions for performing such operations:

Transformation Operations
Operations
Built-In NumPy Functions
Change the shape of an arraynumpy.array.reshape()
Sort the elements of an arraynumpy.sort()
Change n-d array to 1-D arraynumpy.array.flatten()
Transpose an arraynumpy.array.transpose()
python
# Creating a NumPy array from a Python List
arr = np.array([1, 2, 3, 4, 5, 6])

# Print the array
print("arr:", arr)
print("Shape of arr: ", arr.shape)

# Sort the array in ascending order
print("Sorted array: ", np.sort(arr))

# Change the array shape to (2, 3)
reshaped_arr = arr.reshape(2, 3)
print("Reshaped array: ", reshaped_arr)
print("Shape of reshaped array: ", reshaped_arr.shape)

# Transpose the reshaped array
transposed_arr = reshaped_arr.transpose()
print("Transopose array: ", transposed_arr)
print("Shape of transposed array: ", transposed_arr.shape)

# Change the reshaped 2-D array to 1-D
flattened_arr = reshaped_arr.flatten()
print("Flattened array: ", flattened_arr)
arr=[123456]\text{arr} = \begin{bmatrix} 1 & 2 & 3 & 4 & 5 & 6 \end{bmatrix} reshaped_arr=[123456]\text{reshaped\_arr} = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} transposed_arr=[142536]\text{transposed\_arr} = \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix}