NumPy Arithmetic Operations
In NumPy, performing mathematical operations on arrays is very simple and fast.
Instead of using loops, NumPy allows element-wise operations directly on arrays.
These are called Arithmetic Operations.
What are Arithmetic Operations in NumPy?
Arithmetic operations allow you to perform:
- Addition (+)
- Subtraction (−)
- Multiplication (×)
- Division (÷)
- Modulus (%)
- Power (**)
on NumPy arrays element by element.
Why Use NumPy Arithmetic Operations?
- Faster than Python loops
- Cleaner and readable code
- Works on large datasets
- Supports broadcasting
- Essential for data science and ML
1. NumPy Array Addition
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = a + b
print(result)
Output:
[5 7 9]
2. NumPy Array Subtraction
import numpy as np
a = np.array([10, 20, 30])
b = np.array([1, 2, 3])
result = a - b
print(result)
Output:
[9 18 27]
3. NumPy Array Multiplication
import numpy as np
a = np.array([2, 3, 4])
b = np.array([5, 6, 7])
result = a * b
print(result)
Output:
[10 18 28]
4. NumPy Array Division
import numpy as np
a = np.array([10, 20, 30])
b = np.array([2, 5, 10])
result = a / b
print(result)
Output:
[5. 4. 3.]
5. Modulus Operation
import numpy as np
a = np.array([10, 15, 20])
b = np.array([3, 4, 6])
result = a % b
print(result)
Output:
[1 3 2]
6. Power Operation
import numpy as np
a = np.array([1, 2, 3])
result = a ** 2
print(result)
Output:
[1 4 9]
Arithmetic with Scalars
NumPy supports operations with single values:
import numpy as np
a = np.array([1, 2, 3])
print(a + 10)
print(a * 2)
Output:
[11 12 13]
[2 4 6]
✔ This works using broadcasting
Arithmetic on 2D Arrays
import numpy as np
a = np.array([
[1, 2],
[3, 4]
])
b = np.array([
[5, 6],
[7, 8]
])
print(a + b)
Output:
[[ 6 8]
[10 12]]
Key Concept: Element-wise Operations
NumPy performs operations like this:
[a1, a2, a3] + [b1, b2, b3]
= [a1+b1, a2+b2, a3+b3]
Arithmetic Operations vs Python Lists
❌ Python List:
[1, 2, 3] + [4, 5, 6]
Result:
[1, 2, 3, 4, 5, 6] (concatenation)
✅ NumPy Array:
np.array([1,2,3]) + np.array([4,5,6])
Result:
[5 7 9]
Broadcasting in Arithmetic
NumPy automatically expands values:
a = np.array([1, 2, 3])
print(a + 10)
✔ 10 becomes [10, 10, 10]
Real-World Use Cases
Arithmetic operations are used in:
- Data normalization
- Image processing
- Machine learning training
- Statistical calculations
- Financial modeling
Example: Image Brightness Increase
image = image + 50
✔ Increases brightness of all pixels
Advantages of NumPy Arithmetic Operations
- Very fast execution
- No loops required
- Clean syntax
- Supports large datasets
- Works with multi-dimensional arrays
Summary
NumPy arithmetic operations allow fast and efficient element-wise calculations on arrays.
They are a core part of NumPy and are widely used in scientific computing and data processing with Python.
Conclusion
Mastering arithmetic operations in NumPy helps you write faster, cleaner, and more powerful code for data science and machine learning applications.


0 Comments