NumPy – Mathematical Functions
Mathematical functions are the core of scientific computing and data analysis.
NumPy provides a powerful set of built-in mathematical functions that work efficiently on arrays.
These functions help perform:
- Arithmetic operations
- Trigonometric calculations
- Exponential and logarithmic operations
- Rounding and statistical transformations
Import NumPy
import numpy as np
1. Basic Arithmetic Functions
NumPy allows element-wise arithmetic operations.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.add(a, b))
print(np.subtract(b, a))
print(np.multiply(a, b))
print(np.divide(b, a))
Output:
[5 7 9]
[3 3 3]
[4 10 18]
[4. 2.5 2.]
2. Power and Square Root
import numpy as np
a = np.array([1, 4, 9, 16])
print(np.sqrt(a))
print(np.power(a, 2))
Explanation:
- sqrt → square root
- power → exponentiation
3. Trigonometric Functions
import numpy as np
angles = np.array([0, np.pi/2, np.pi])
print(np.sin(angles))
print(np.cos(angles))
print(np.tan(angles))
Use case:
- Physics simulations
- Signal processing
4. Exponential and Logarithmic Functions
import numpy as np
a = np.array([1, 2, 3])
print(np.exp(a)) # e^x
print(np.log(a)) # natural log
print(np.log10(a)) # base-10 log
Meaning:
- exp → exponential growth
- log → logarithmic scaling
5. Rounding Functions
import numpy as np
a = np.array([1.234, 5.678, 9.876])
print(np.round(a, 2))
print(np.floor(a))
print(np.ceil(a))
Explanation:
- round → nearest value
- floor → lower integer
- ceil → upper integer
6. Aggregate Mathematical Functions
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(np.sum(a))
print(np.mean(a))
print(np.median(a))
print(np.std(a))
Output Meaning:
- sum → total
- mean → average
- median → middle value
- std → spread
Real-World Applications
1. Data Science
- Feature scaling
- Statistical analysis
2. Machine Learning
- Loss functions
- Gradient calculations
3. Engineering
- Signal processing
- Physics modeling
4. Finance
- Risk analysis
- Growth modeling
Why Use NumPy Mathematical Functions?
Using NumPy provides:
- Fast vectorized computation
- Efficient array operations
- Built-in scientific functions
- Optimized performance
Combined with Python, it becomes essential for data science, AI, and scientific computing.
Summary
NumPy provides a wide range of mathematical functions:
np.add()
np.subtract()
np.multiply()
np.divide()
np.sqrt()
np.exp()
np.log()
Conclusion
NumPy mathematical functions are essential for performing fast and efficient computations in data science, machine learning, and scientific applications.


0 Comments