
NumPy is a popular library in Python for numerical computation. It is an essential tool for data manipulation and analysis in various fields, including finance, science, engineering, and data science. NumPy provides high-level mathematical functions for working with large multi-dimensional arrays and matrices.
In this article, I will explore the following sub-topics highlighted below in a bid to improve your understanding of the NumPy Library.
Before diving into NumPy basics, we need to ensure that Numpy have been installed into the system. To install NumPy, one can use pip, the package installer for Python. It can be installed using the following command on your terminal or command prompt or any integrated development environment you are using:
pip install numpyOnce installed, one can import NumPy into our Python program using the following command:
import numpy as npFor example, let's create an array of integers:
import numpy as np
# create a 1-dimensional array
arr = np.array([1, 2, 3, 4, 5])
print(arr)The output of the code above:
[1 2 3 4 5]# create a 2-dimensional array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr)The output of the code above:
[[1 2 3]
[4 5 6]
[7 8 9]]For example to create a numpy array with zeros:
# create an array of zeros
zeros_arr = np.zeros((2, 3))
print(zeros_arr)The output of the code above:
[[0. 0. 0.]
[0. 0. 0.]]For example to create a numpy array with ones:
# create an array of ones
ones_arr = np.ones((2, 3))
print(ones_arr)The output of the code above:
[[1. 1. 1.]
[1. 1. 1.]]For example to create a numpy array with random values:
# create an array of random values
rand_arr = np.random.rand(2, 3)
print(rand_arr)The output of the code above:
[[0.36056714 0.4220256 0.4876324 ]
[0.88501101 0.25223722 0.98649871]]Note: This random number generated in the array above will be different for you while practicing.
NumPy provides several mathematical functions for performing operations on arrays. For example, one can perform arithmetic operations such as addition, subtraction, multiplication, and division on arrays.
Let's consider an example of performing a simple arithmetic operation on two arrays:
For example : Addition of two arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Add two arrays
sum_arr = arr1 + arr2
print(sum_arr)The output of the code above:
[5 7 9]For example : Subtraction of two arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Subtract two arrays
diff_arr = arr1 - arr2
print(diff_arr)The output of the code above:
[-3 -3 -3]For example : Multiplication of two arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Multiply two arrays
prod_arr = arr1 * arr2
print(prod_arr)The output of the code above:
[ 4 10 18]For example : Division of two arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Divide two arrays
div_arr = arr1 / arr2
print(div_arr)The output of the code above:
[0.25 0.4 0.5 ]Note: One can also perform functions such as sin(), cos(), exp(), log(), and many others.
Here are some of the most commonly used NumPy array manipulation routines with code samples:
NumPy provides several methods for reshaping arrays. The reshape() function is used to change the shape of an array without changing its data. Here is an example:
import numpy as np
# Creating a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Reshaping the array to 3 rows and 2 columns
reshaped_arr = arr.reshape(3, 2)
print("Original Array:\n", arr)
print("Reshaped Array:\n", reshaped_arr)
The output of the code above:
Original Array:
[[1 2 3]
[4 5 6]]
Reshaped Array:
[[1 2]
[3 4]
[5 6]]The flatten() function is used to flatten a multi-dimensional array into a one-dimensional array. Here is an example:
import numpy as np
# Creating a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Flattening the array
flattened_arr = arr.flatten()
print("Original Array:\n", arr)
print("Flattened Array:\n", flattened_arr)The output of the code above:
Original Array:
[[1 2 3]
[4 5 6]]
Flattened Array:
[1 2 3 4 5 6]The transpose() function is used to transpose the rows and columns of an array. Here is an example:
import numpy as np
# Creating a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])
# Transposing the array
transposed_arr = arr.transpose()
print("Original Array:\n", arr)
print("Transposed Array:\n", transposed_arr)The output of the code above:
Original Array:
[[1 2 3]
[4 5 6]]
Transposed Array:
[[1 4]
[2 5]
[3 6]]The concatenate() function is used to concatenate two or more arrays along a specified axis. Here is an example:
import numpy as np
# Creating two 1D arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Concatenating the two arrays along axis 0
concatenated_arr = np.concatenate((arr1, arr2), axis=0)
print("Array 1:\n", arr1)
print("Array 2:\n", arr2)
print("Concatenated Array:\n", concatenated_arr)
The output of the code above:
Array 1:
[1 2 3]
Array 2:
[4 5 6]
Concatenated Array:
[1 2 3 4 5 6]The split() function is used to split an array into multiple sub-arrays along a specified axis. Here is an example:
import numpy as np
# Creating a 1D array
arr = np.array([1, 2, 3, 4, 5, 6])
# Splitting the array into three sub-arrays
sub_arrays = np.split(arr, 3)
print("Original Array:\n", arr)
print("Sub-arrays:\n", sub_arrays)The output of the code above:
Original Array:
[1 2 3 4 5 6]
Sub-arrays:
[array([1, 2]), array([3, 4]), array([5, 6])]Note: The split() function splits the array into equal-sized sub-arrays. If the array cannot be split into equal-sized sub-arrays, an error is raised.
NumPy provides several functions for adding and removing elements from an array.
1. The append() function is used to add elements to the end of an array. Here is an example:
import numpy as np
# Creating a 1D array
arr = np.array([1, 2, 3])
# Appending an element to the array
new_arr = np.append(arr, 4)
print("Original Array:\\n", arr)
print("New Array:\\n", new_arr)The output of the code above:
Original Array:
[1 2 3]
New Array:
[1 2 3 4]2. The insert() function is used to insert elements into an array at a specified index. Here is an example:
import numpy as np
# Creating a 1D array
arr = np.array([1, 2, 3])
# Inserting an element into the array at index 1
new_arr = np.insert(arr, 1, 4)
print("Original Array:\n", arr)
print("New Array:\n", new_arr)The output of the code above:
Original Array:
[1 2 3]
New Array:
[1 4 2 3]3. The delete() function is used to remove elements from an array at a specified index. Here is an example:
import numpy as np
# Creating a 1D array
arr = np.array([1, 2, 3])
# Removing an element from the array at index 1
new_arr = np.delete(arr, 1)
print("Original Array:\n", arr)
print("New Array:\n", new_arr)The output of the code above:
Original Array:
[1 2 3]
New Array:
[1 3]Note: These are some of the most commonly used NumPy array manipulation routines. There are many other routines available in NumPy that you can use to manipulate arrays in various ways.
NumPy provides a wide range of advanced mathematical functions that can be used to perform complex mathematical operations on arrays. Here are some examples:
1. Trigonometric Functions:
NumPy provides several trigonometric functions that can be used to perform trigonometric operations on arrays. These functions include:
Here is an example that shows how to use these functions:
import numpy as np
# Creating an array of angles in radians
angles = np.array([0, np.pi/6, np.pi/4, np.pi/3, np.pi/2])
# Computing the sine of the angles
sin_values = np.sin(angles)
# Computing the cosine of the angles
cos_values = np.cos(angles)
# Computing the tangent of the angles
tan_values = np.tan(angles)
# Computing the inverse sine of the sin_values
arcsin_values = np.arcsin(sin_values)
# Computing the inverse cosine of the cos_values
arccos_values = np.arccos(cos_values)
# Computing the inverse tangent of the tan_values
arctan_values = np.arctan(tan_values)
print("Angles (in radians):", angles)
print("Sine values:", sin_values)
print("Cosine values:", cos_values)
print("Tangent values:", tan_values)
print("Inverse sine values:", arcsin_values)
print("Inverse cosine values:", arccos_values)
print("Inverse tangent values:", arctan_values)The output of the code above:
Angles (in radians): [0. 0.52359878 0.78539816 1.04719755 1.57079633]
Sine values: [0. 0.5 0.70710678 0.8660254 1. ]
Cosine values: [1.0000000e+00 8.6602540e-01 7.0710678e-01 5.0000000e-01 6.1232340e-17]
Tangent values: [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00 1.63312394e+16]
Inverse sine values: [0. 0.52359878 0.78539816 1.04719755 1.57079633]
Inverse cosine values:
Inverse tangent values:
2. Exponential and Logarithmic Functions:
NumPy provides several exponential and logarithmic functions that can be used to perform exponential and logarithmic operations on arrays. These functions include:
Here is an example that shows how to use these functions:
import numpy as np
# Creating an array of values
values = np.array([1, 2, 3, 4, 5])
# Computing the exponential of the values
exp_values = np.exp(values)
# Computing the natural logarithm of the values
log_values = np.log(values)
# Computing the base-10 logarithm of the values
log10_values = np.log10(values)
print("Values:", values)
print("Exponential values:", exp_values)
print("Natural logarithm values:", log_values)
print("Base-10 logarithm values:", log10_values)The output of the code above:
Values: [1 2 3 4 5]
Exponential values: [ 2.71828183 7.3890561 20.08553692 54.59815003 148.4131591 ]
Natural logarithm values: [0. 0.69314718 1.09861229 1.38629436 1.60943791]
Base-10 logarithm values: [0. 0.30103 0.47712125 0.60205999 0.69897 ]3. Arithmetic Operations:
NumPy provides several arithmetic functions that can be used to perform arithmetic operations on arrays. These functions include:
Here is an example that shows how to use these functions:
import numpy as np
# Creating two arrays
x = np.array([1, 2, 3, 4, 5])
y = np.array([6, 7, 8, 9, 10])
# Computing the sum of the arrays
sum_values = np.add(x, y)
# Computing the difference of the arrays
diff_values = np.subtract(x, y)
# Computing the product of the arrays
prod_values = np.multiply(x, y)
# Computing the division of the arrays
div_values = np.divide(x, y)
print("Array x:", x)
print("Array y:", y)
print("Sum values:", sum_values)
print("Difference values:", diff_values)
print("Product values:", prod_values)
print("Division values:", div_values)
The output of the code above:
Array x: [1 2 3 4 5]
Array y: [ 6 7 8 9 10]
Sum values: [ 7 9 11 13 15]
Difference values: [-5 -5 -5 -5 -5]
Product values: [ 6 14 24 36 50]
Division values: [0.16666667 0.28571429 0.375 0.44444444 0.5 ]4. Statistical Functions:
NumPy provides several statistical functions that can be used to perform statistical operations on arrays. These functions include:
Here is an example that shows how to use these functions:
import numpy as np
# Creating an array of values
values = np.array([1, 2, 3, 4, 5])
# Computing the mean of the values
mean = np.mean(values)
# Computing the median of the values
median = np.median(values)
# Computing the variance of the values
variance = np.var(values)
# Computing the standard deviation of the values
std_deviation = np.std(values)
print("Values:", values)
print("Mean:", mean)
print("Median:", median)
print("Variance:", variance)
print("Standard deviation:", std_deviation)
The output of the code above:
Values: [1 2 3 4 5]
Mean: 3.0
Median: 3.0
Variance: 2.0
Standard deviation: 1.41421356237309515. Linear Algebra Functions:
NumPy provides several linear algebra functions that can be used to perform linear algebra operations on arrays. These functions include:
Here's a continuation of the Linear Algebra Functions:
import numpy as np
# Creating two arrays
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# Computing the dot product of the two arrays
dot_product = np.dot(a, b)
# Computing the determinant of a square matrix
det = np.linalg.det(a)
# Computing the inverse of a matrix
inverse = np.linalg.inv(a)
print("Array a:\n", a)
print("Array b:\n", b)
print("Dot product:\n", dot_product)
print("Determinant of a:", det)
print("Inverse of a:\n", inverse)The output of the code above:
Array a:
[[1 2]
[3 4]]
Array b:
[[5 6]
[7 8]]
Dot product:
[[19 22]
[43 50]]
Determinant of a: -2.0000000000000004
Inverse of a:
[[-2. 1. ]
[ 1.5 -0.5]]6. Sorting Functions:
NumPy provides several sorting functions that can be used to sort arrays. These functions include:
Here is an example that shows how to use these functions:
import numpy as np
# Creating an array of random integers
a = np.random.randint(1, 10, size=5)
# Sorting the array in ascending order
sorted_array = np.sort(a)
# Getting the indices that would sort the array
indices = np.argsort(a)
# Finding the indices where elements should be inserted to maintain order
search_indices = np.searchsorted(sorted_array, [2, 6])
print("Array a:", a)
print("Sorted array:", sorted_array)
print("Indices that would sort the array:", indices)
print("Indices where elements should be inserted to maintain order:", search_indices)The output of the code above:
Array a: [9 8 8 7 3]
Sorted array: [3 7 8 8 9]
Indices that would sort the array: [4 3 1 2 0]
Indices where elements should be inserted to maintain order: [0 3]Note: These are some of the advanced NumPy functions that are commonly used in scientific computing and data analysis. NumPy provides many other functions that can be used for a wide range of tasks, so it is worth exploring the NumPy documentation to learn more.
NumPy Masked Arrays are arrays that contain masked values, which are elements that are not considered in computations or data analysis. Masked arrays are useful for working with data sets that contain missing or invalid values, or for filtering out certain data points based on certain criteria.
Here's an example of how to create and use masked arrays:
import numpy as np
# Creating a NumPy array
a = np.array([1, 2, -999, 4, 5])
# Creating a masked array from the original array
mask = np.ma.masked_values(a, -999)
# Computing the mean of the masked array
mean = np.ma.mean(mask)
# Printing the original array, the masked array, and the mean
print("Original array:", a)
print("Masked array:", mask)
print("Mean of the masked array:", mean)The output of the code above:
Original array: [ 1 2 -999 4 5]
Masked array: [1 2 -- 4 5]
Mean of the masked array: 3.0In this example, I created a NumPy array a that contains five elements, including one masked value (-999). We then created a masked array mask using the np.ma.masked_values() function, which creates a masked array from an input array by replacing all occurrences of a specified value with a masked value. We then computed the mean of the masked array using the np.ma.mean() function, which computes the mean of an input array while ignoring masked values. The output shows the original array, the masked array, and the mean of the masked array.
There are several other functions and methods provided by NumPy for working with masked arrays, including:
Here's an example that demonstrates the use of these functions and methods:
import numpy as np
# Creating a NumPy array
a = np.array([1, 2, -999, 4, 5])
# Creating a mask based on a condition
mask_condition = a < 0
# Creating a masked array based on the condition
mask = np.ma.masked_where(mask_condition, a)
# Computing the sum of the unmasked elements
sum_unmasked = np.sum(mask.compressed())
# Checking if any or all elements in the mask are True
any_true = mask.any()
all_true = mask.all()
# Printing the mask, the sum of the unmasked elements, and the results of the any() and all() methods
print("Mask:", mask)
print("Sum of unmasked elements:", sum_unmasked)
print("Any elements in mask are True:", any_true)
print("All elements in mask are True:", all_true)The output of the code above:
Mask: [1 2 -- 4 5]
Sum of unmasked elements: 12
Any elements in mask are True: True
All elements in mask are True: FalseIn this example, I created a NumPy array a that contains five elements, including one negative value (-999). We then created a mask based on the condition that the elements in a are less than zero.
We used the np.ma.masked_where() function to create a masked array mask from a where the masked values are the elements that satisfy the mask condition. We then computed the sum of the unmasked elements using the compressed() method, which returns a 1D array of all unmasked values in the mask.
We also used the any() and all() methods to check if any or all elements in the mask are True, respectively. The output shows the mask, the sum of the unmasked elements, and the results of the any() and all() methods.
Note: Masked arrays are a useful tool for handling missing or invalid data in NumPy arrays. They allow you to perform computations and data analysis while ignoring masked values, and provide a range of functions and methods for creating and manipulating masked arrays.
NumPy is a powerful library for scientific computing in Python that provides a wide range of functions and tools for working with multidimensional arrays and matrices. It provides a high-performance implementation of array-based computing and is widely used in various fields such as physics, engineering, data science, and machine learning.
The key features of NumPy include efficient storage and manipulation of array data, broadcasting and vectorization of operations, and a comprehensive set of mathematical functions and algorithms for linear algebra, Fourier analysis, statistical analysis, and more. Additionally, NumPy provides tools for indexing, slicing, and manipulating arrays, as well as working with masked arrays to handle missing or invalid data.
For more understanding of the Numpy library, you can check out the documentation here
In the next article in this series, I will dive into the Pandas library which is a popular open-source data analysis and manipulation tool written in Python. It provides data structures for efficiently storing and manipulating large datasets, and functions for cleaning, transforming, and aggregating data.
If you want to get started with data analytics and looking to improving your skills, you can check out our Learning Track
Empowering individuals and businesses with the tools to harness data, drive innovation, and achieve excellence in a digital world.
2025Resagratia (a brand of Resa Data Solutions Ltd). All Rights Reserved.