
NumPy array operations are a set of functions and methods that allow you to perform various operations on NumPy arrays. In this tutorial, we will introduce some of the most commonly used array operations in NumPy, and show you how to use them to perform calculations and manipulate arrays of data.
- NumPy Array Mathematical Functions
- NumPy Array Statistical Functions
- NumPy Array Set Operations
- NumPy Array Sorting
One of the most fundamental array operations is element-wise operations, which allow you to perform arithmetic and other operations on the elements of an array individually. For example, you can use element-wise operations to add two arrays element-wise, multiply two arrays element-wise, or perform any other operation element-wise.
Another important array operation is aggregation, which allows you to perform statistical and other calculations on the elements of an array. NumPy provides several built-in aggregation functions, such as mean
, sum
, min
, and max
, which allow you to compute the mean, sum, minimum, and maximum of an array, respectively.
Finally, NumPy provides several functions for reshaping arrays, which allow you to change the shape and size of an array. These functions include reshape
, resize
, and flatten
, which allow you to change the shape of an array, resize an array, and flatten an array into a 1-D array, respectively.
In the following sections, we will take a closer look at each of these array operations in NumPy, and see some examples of how to use them in practice.
NumPy Array Mathematical Functions
NumPy array mathematical functions are functions that perform mathematical operations on NumPy arrays. These functions are useful for a wide variety of purposes, such as:
- Performing mathematical calculations on arrays of data: You can use NumPy array mathematical functions to perform mathematical operations on arrays of data, such as calculating trigonometric functions, exponentiation, and logarithms.
- Analyzing and manipulating arrays of data: You can use NumPy array mathematical functions to analyze and manipulate arrays of data, such as calculating statistical measures, finding the minimum and maximum values, and sorting the elements of an array.
- Machine learning and data science: NumPy array mathematical functions are commonly used in machine learning and data science to perform operations on arrays of data, such as normalizing data, calculating distances, and generating random numbers.
Here are a few examples of using NumPy array mathematical functions:
import numpy as np
# Create an array of random values
a = np.random.rand(3, 4)
print(a)
# Output: [[0.55 0.72 0.23 0.61]
# [0.93 0.67 0.34 0.59]
# [0.94 0.17 0.53 0.96]]
# Calculate the sine of each element in the array
b = np.sin(a)
print(b)
# Output: [[ 0.541 0.656 -0.23 -0.588]
# [-0.735 0.599 -0.339 -0.539]
# [-0.745 -0.173 -0.506 0.856]]
# Calculate the mean of the array
c = np.mean(a)
print(c)
# Output: 0.62
# Find the minimum and maximum values of the array
d = np.min(a)
e = np.max(a)
print(d, e)
# Output: 0.17 0.94
# Sort the elements of the array
f = np.sort(a)
print(f)
# Output: [[0.23 0.55 0.61 0.72]
# [0.34 0.59 0.67 0.93]
# [0.17 0.53 0.94 0.96]]
NumPy Array Statistical Functions
NumPy array statistical functions are functions that perform statistical calculations on NumPy arrays. These functions are useful when you need to perform statistical analysis on arrays of data.
Here are a few scenarios where you might use NumPy array statistical functions:
- Exploratory data analysis: NumPy array statistical functions are useful for exploring and understanding the properties of a dataset. For example, you can use statistical functions to compute the mean, median, standard deviation, and other statistical measures of an array.
- Data preprocessing: You can use statistical functions to preprocess data before feeding it into a machine learning model. For example, you can use statistical functions to normalize data by subtracting the mean and dividing by the standard deviation.
- Data visualization: NumPy array statistical functions can be useful for generating plots and charts to visualize the properties of a dataset. For example, you can use statistical functions to compute the quartiles of an array and plot them as a box plot.
Here are a few examples of using NumPy array statistical functions:
import numpy as np
# Create an array of random values
a = np.random.rand(3, 4)
print(a)
# Output: [[0.55 0.72 0.23 0.61]
# [0.93 0.67 0.34 0.59]
# [0.94 0.17 0.53 0.96]]
# Calculate the mean of the array
b = np.mean(a)
print(b)
# Output: 0.62
# Calculate the median of the array
c = np.median(a)
print(c)
# Output: 0.61
# Calculate the standard deviation of the array
d = np.std(a)
print(d)
# Output: 0.27
# Calculate the quartiles of the array
e = np.quantile(a, [0.25, 0.5, 0.75])
print(e)
# Output: [0.53 0.61 0.72]
NumPy Array Set Operations
Set operations are functions that perform set operations on arrays, such as union, intersection, and difference. Set operations are useful for analyzing and manipulating data arrays, such as finding unique elements or removing duplicates. NumPy array set operations are functions that perform set operations on NumPy arrays, such as union, intersection, and difference. These functions are useful for a variety of purposes, such as:
- Performing set operations on arrays of data: You can use NumPy array set operations to perform set operations on arrays of data, such as finding the union or intersection of two arrays.
- Analyzing and manipulating arrays of data: You can use NumPy array set operations to analyze and manipulate arrays of data, such as finding unique elements in an array or removing duplicates.
- Data preprocessing: NumPy array set operations can be useful for preprocessing data before feeding it into a machine learning model. For example, you can use set operations to remove duplicates from a dataset or encode categorical variables as numerical values.
NumPy array set operations are generally considered to be an intermediate to advanced technique, as they require a basic understanding of set theory and the set operations of union, intersection, and difference. However, they are relatively easy to use and can be a powerful tool for working with arrays of data.
Here are a few examples of using NumPy array set operations:
import numpy as np
# Create two arrays
a = np.array([1, 2, 3, 4])
b = np.array([3, 4, 5, 6])
# Find the union of the two arrays
c = np.union1d(a, b)
print(c)
# Output: [1 2 3 4 5 6]
# Find the intersection of the two arrays
d = np.intersect1d(a, b)
print(d)
# Output: [3 4]
# Find the difference between the two arrays
e = np.setdiff1d(a, b)
print(e)
# Output: [1 2]
# Find the unique elements in the array
f = np.unique(a)
print(f)
# Output: [1 2 3 4]
NumPy Array Sorting
NumPy array sorting is a technique for sorting the elements of a NumPy array in a specific order. NumPy provides several functions for sorting arrays, including sort
, argsort
, and lexsort
.
Here is an example of using the sort
function to sort an array of integers in ascending order:
import numpy as np
# Create an array of random values
a = np.array([3, 1, 4, 2])
# Sort the array in ascending order
b = np.sort(a)
print(b)
# Output: [1 2 3 4]
The sort
function returns a new sorted array, and does not modify the original array. If you want to sort the array in place, you can use the sort
method of the array object:
import numpy as np
# Create an array of random values
a = np.array([3, 1, 4, 2])
# Sort the array in ascending order
a.sort()
print(a)
# Output: [1 2 3 4]
You can also use the argsort
function to obtain the indices of the elements of the array that would sort the array:
import numpy as np
# Create an array of random values
a = np.array([3, 1, 4, 2])
# Get the indices that would sort the array
b = np.argsort(a)
print(b)
# Output: [1 3 0 2]
Here is an example of using the lexsort
function to sort an array of strings by their length, and then by their lexicographic order:
import numpy as np
# Create an array of strings
a = np.array(['cat', 'dog', 'bird', 'ant', 'rat'])
# Sort the array using lexsort
b = np.lexsort((a, len(a)))
print(a[b])
# Output: ['ant' 'rat' 'cat' 'dog' 'bird']
In this example, the lexsort
function takes two keys as arguments: a
, the array of strings, and len(a)
, the lengths of the strings. The lexsort
function sorts the array first by the lengths of the strings (in ascending order), and then by the lexicographic order of the strings (in ascending order).
As a result, the lexsort
function returns an array of indices b
that can be used to sort the original array a
by the desired criteria. The sorted array can be obtained by indexing a
with b
, as shown in the example above.
- What Are NumPy Array Operations (vegibit.com)
- numpy.array — NumPy v1.24 Manual (numpy.org)
- Numpy Array Operations in Python – PythonForBeginners.com (www.pythonforbeginners.com)
- Understanding non-homogeneous numpy arrays – Stack (stackoverflow.com)
- 1.4.2. Numerical operations on arrays — Scipy lecture (scipy-lectures.org)
- Operations on Arrays with NumPy | Pluralsight ()
- Overview of Basic Numpy Operations | Pluralsight (www.pluralsight.com)
- Look Ma, No For-Loops: Array Programming With (realpython.com)
- NumPy Array Operations – The Click Reader (www.theclickreader.com)
- Learn Numpy Operations With Examples – Simplified Python (www.simplifiedpython.net)
- 20 NumPy Operations That Every Data Scientist Should Know (towardsdatascience.com)
- Numpy Basics — 2. Arrays Operations | by Esteban Thilliez (medium.com)
- 12 NumPy Operations for Beginners – MUO (www.makeuseof.com)
- Computation on NumPy Arrays: Universal Functions (jakevdp.github.io)
- Python NumPy Array Operations – Spark By {Examples} (sparkbyexamples.com)