NumPy, a popular library in Python, provides powerful tools for performing mathematical operations on arrays. These operations can be applied element-wise, across specific axes, or on the entire array. This article covers various mathematical operations that can be performed on NumPy arrays.
Before performing mathematical operations, you need to import the NumPy library:
import numpy as np
NumPy allows you to perform basic arithmetic operations like addition, subtraction, multiplication, and division directly on arrays.
# Creating arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Element-wise addition add_result = array1 + array2 print("Addition:", add_result) # Output: [5 7 9] # Element-wise subtraction sub_result = array1 - array2 print("Subtraction:", sub_result) # Output: [-3 -3 -3] # Element-wise multiplication mul_result = array1 * array2 print("Multiplication:", mul_result) # Output: [4 10 18] # Element-wise division div_result = array1 / array2 print("Division:", div_result) # Output: [0.25 0.4 0.5]
NumPy provides several mathematical functions that can be applied to arrays.
# Creating an array array = np.array([1, 4, 9, 16]) # Square root sqrt_result = np.sqrt(array) print("Square Root:", sqrt_result) # Output: [1. 2. 3. 4.] # Exponential exp_result = np.exp(array) print("Exponential:", exp_result) # Logarithm log_result = np.log(array) print("Logarithm:", log_result)
Aggregate functions operate on the entire array or along specific axes to compute summary statistics.
# Creating an array array = np.array([[1, 2, 3], [4, 5, 6]]) # Sum of all elements sum_result = np.sum(array) print("Sum:", sum_result) # Output: 21 # Mean of all elements mean_result = np.mean(array) print("Mean:", mean_result) # Output: 3.5 # Maximum and minimum max_result = np.max(array) min_result = np.min(array) print("Max:", max_result) # Output: 6 print("Min:", min_result) # Output: 1
NumPy supports trigonometric functions like sine, cosine, and tangent.
# Creating an array of angles in radians angles = np.array([0, np.pi / 2, np.pi]) # Sine function sin_result = np.sin(angles) print("Sine:", sin_result) # Output: [0. 1. 0.] # Cosine function cos_result = np.cos(angles) print("Cosine:", cos_result) # Output: [1. 0. -1.] # Tangent function tan_result = np.tan(angles) print("Tangent:", tan_result)
NumPy also provides support for linear algebra operations such as dot products and matrix multiplication.
# Creating matrices matrix1 = np.array([[1, 2], [3, 4]]) matrix2 = np.array([[5, 6], [7, 8]]) # Dot product dot_result = np.dot(matrix1, matrix2) print("Dot Product:\n", dot_result) # Determinant det_result = np.linalg.det(matrix1) print("Determinant:", det_result)
NumPy supports broadcasting, which allows arithmetic operations between arrays of different shapes.
# Creating arrays array = np.array([1, 2, 3]) scalar = 5 # Adding a scalar to an array broadcast_result = array + scalar print("Broadcasting Result:", broadcast_result) # Output: [6 7 8]
NumPy provides a comprehensive set of tools for performing mathematical operations on arrays, making it an essential library for numerical computing. By mastering these operations, you can efficiently manipulate and analyze data in Python.