NumPy Array Operations
Arithmetic, statistical, and transformation operations.
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:
# 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:
Statistics | Built-In NumPy Functions |
|---|---|
| Minimum Value | numpy.min() |
| Maximum Value | numpy.max() |
| Mean Value | numpy.mean() |
| Median Value | numpy.median() |
| Standard deviation | numpy.std() |
| Get count of unique values | numpy.unique() |
# 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))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:
Operations | Built-In NumPy Functions |
|---|---|
| Change the shape of an array | numpy.array.reshape() |
| Sort the elements of an array | numpy.sort() |
| Change n-d array to 1-D array | numpy.array.flatten() |
| Transpose an array | numpy.array.transpose() |
# 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)