Mathematical operations are one of the biggest advantages of using NumPy arrays. Unlike Python lists, NumPy allows you to perform calculations on entire arrays at once.
This makes data analysis faster and more efficient.
Basic Arithmetic Operations
First, import NumPy:
import numpy as np
Create an array:
arr = np.array([1, 2, 3, 4])
Addition
arr + 5
Adds 5 to each element.
Subtraction
arr - 2
Multiplication
arr * 3
Division
arr / 2
Power
arr ** 2
Squares each element.
Array to Array Operations
Create another array:
arr2 = np.array([10, 20, 30, 40])
Addition:
arr + arr2
Multiplication:
arr * arr2
Division:
arr2 / arr
Operations happen element-wise.
Mathematical Functions
Square Root
np.sqrt(arr)
Exponential
np.exp(arr)
Logarithm
np.log(arr)
Absolute Value
np.abs(arr)
Statistical Operations
Mean:
np.mean(arr)
Sum:
np.sum(arr)
Minimum:
np.min(arr)
Maximum:
np.max(arr)
Standard Deviation:
np.std(arr)
Variance:
np.var(arr)
Matrix Operations (For 2D Arrays)
Create a matrix:
matrix = np.array([[1, 2], [3, 4]])
Matrix Transpose:
matrix.T
Matrix Multiplication:
np.dot(matrix, matrix)
Why Mathematical Operations Matter
In Data Analytics, mathematical operations help you:
Clean and transform data
Calculate statistics
Normalize values
Perform modeling
Analyze trends
NumPy makes these operations fast and simple.
Key Takeaway
NumPy allows you to perform powerful mathematical and statistical operations on entire datasets efficiently. Mastering these operations is essential for Data Analytics and Data Science.