NumPy Array Operations

Greetings! Some links on this site are affiliate links. That means that, if you choose to make a purchase, The Click Reader may earn a small commission at no extra cost to you. We greatly appreciate your support!

Learn to perform arithmetic, statistical, and transformation operations on NumPy arrays in Python.


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:

# 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])

# Creating a NumPy array from a Python List
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)
arr1: [6 7 8 9]
arr2: [1 2 3 4]

arr1 + 2: [ 8 9 10 11]
arr1 - 2: [4 5 6 7]
arr1 * 2: [12 14 16 18]
arr1 / 2: [3. 3.5 4. 4.5]

arr1 + arr2: [ 7 9 11 13]
arr1 - arr2: [5 5 5 5]
arr1 * arr2: [ 6 14 24 36]
arr1 / arr2: [6. 3.5 2.66666667 2.25 ]

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:

StatisticsBuilt-In NumPy Functions
Minimum Valuenumpy.min()
Maximum Valuenumpy.max()
Mean Valuenumpy.mean()
Median Valuenumpy.median()
Standard deviationnumpy.std()
Get count of unique valuesnumpy.unique()
# 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])

# 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))

# 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)
arr1: [1 2 3 4] 
Min: 1 
Max: 4 
Mean: 2.5 
Median: 2.5 
Standard Deviation: 1.118033988749895 
Unique values: [1 2 3 4] 
Count of respective unique values: [1 1 1 1]

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 functions for performing such operations:

OperationsBuilt-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()
# Importing the NumPy library as np
import numpy as np

# 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: [1 2 3 4 5 6] 
Shape of arr: (6,) 
Sorted array: [1 2 3 4 5 6] 
Reshaped array: [[1 2 3] [4 5 6]] 
Shape of reshaped array: (2, 3) 
Transopose array: [[1 4] [2 5] [3 6]] 
Shape of transposed array: (3, 2) 
Flattened array: [1 2 3 4 5 6]

This is it for operations on NumPy arrays. Feel free to construct your own NumPy arrays and try out different operations.

Leave a Comment