Numpy - Mathematical Function

Numpy - Mathematical Function

NumPy, being a numerical computing library in Python, provides a suite of mathematical functions that can operate on arrays. Let's go through a tutorial on using some of these mathematical functions.

1. Introduction:

NumPy offers a broad range of mathematical functions that can be applied element-wise to arrays, making it easy to perform mathematical operations on data sets.

2. Setup:

Importing NumPy:

import numpy as np 

3. Basic Arithmetic:

Addition, Subtraction, Multiplication, and Division:

These can be done element-wise:

a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) print(np.add(a, b)) # [5 7 9] print(np.subtract(a, b)) # [-3 -3 -3] print(np.multiply(a, b)) # [4 10 18] print(np.divide(a, b)) # [0.25 0.4 0.5] 

4. Exponential and Logarithmic Functions:

Exponential:

Computing the exponential of all elements in the input array:

print(np.exp(a)) # e^x for each element of a 

Natural Logarithm, Base 10 Logarithm, Base 2 Logarithm:

print(np.log(a)) # Natural log print(np.log10(a)) # Base 10 log print(np.log2(a)) # Base 2 log 

5. Trigonometric Functions:

NumPy provides all the basic trigonometric functions:

angles = np.array([0, np.pi/4, np.pi/2]) print(np.sin(angles)) print(np.cos(angles)) print(np.tan(angles)) 

6. Rounding:

Round to nearest integer:

arr = np.array([1.65, 2.05, 3.57, 4.89]) print(np.round(arr)) 

Floor and Ceil:

print(np.floor(arr)) # Rounds down print(np.ceil(arr)) # Rounds up 

7. Sum, Product, and Difference:

Sum and Product:

print(np.sum(a)) # Sum of all elements print(np.prod(a)) # Product of all elements 

Difference:

Computing the n-th discrete difference:

print(np.diff(a)) # Outputs [1 1] because 2-1 = 1 and 3-2 = 1 

8. Powers and Square Roots:

print(np.power(a, 2)) # Square of each element print(np.sqrt(a)) # Square root of each element 

9. Miscellaneous Functions:

Absolute:

c = np.array([-1, -2, 3]) print(np.abs(c)) # [1 2 3] 

Sign:

Returns an element-wise indication of the sign of a number:

print(np.sign(c)) # [-1 -1 1] 

10. Conclusion:

NumPy's mathematical functions provide an easy and efficient way to compute a range of operations over arrays. By understanding and leveraging these functions, one can greatly simplify the process of data manipulation and mathematical computation in Python.

Examples

1. Common mathematical operations in NumPy:

NumPy provides a wide range of common mathematical operations such as addition, subtraction, multiplication, and division. Here's a simple example:

import numpy as np # Create two NumPy arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Addition result_addition = array1 + array2 # Subtraction result_subtraction = array1 - array2 # Multiplication result_multiplication = array1 * array2 # Division result_division = array1 / array2 print("Addition:", result_addition) print("Subtraction:", result_subtraction) print("Multiplication:", result_multiplication) print("Division:", result_division) 

2. Python NumPy math functions examples:

NumPy provides various math functions that operate element-wise on arrays. Examples include np.sin(), np.cos(), np.sqrt(), etc.

# Assuming 'array' is already defined # Example math functions result_sin = np.sin(array) result_sqrt = np.sqrt(array) print("Sin:", result_sin) print("Square root:", result_sqrt) 

3. Element-wise operations with NumPy:

NumPy performs element-wise operations by default. For example:

# Assuming 'array' is already defined # Element-wise operations result_square = np.square(array) result_exp = np.exp(array) print("Square:", result_square) print("Exponential:", result_exp) 

4. NumPy trigonometric functions:

NumPy provides trigonometric functions like np.sin(), np.cos(), and np.tan() for array elements.

# Assuming 'array' is already defined # Trigonometric functions result_sin = np.sin(array) result_cos = np.cos(array) result_tan = np.tan(array) print("Sine:", result_sin) print("Cosine:", result_cos) print("Tangent:", result_tan) 

5. Exponential and logarithmic functions in NumPy:

Exponential and logarithmic functions are available in NumPy. Example:

# Assuming 'array' is already defined # Exponential and logarithmic functions result_exp = np.exp(array) result_log = np.log(array) print("Exponential:", result_exp) print("Logarithm:", result_log) 

6. NumPy statistical functions:

NumPy includes statistical functions such as np.mean(), np.std(), and np.sum().

# Assuming 'array' is already defined # Statistical functions result_mean = np.mean(array) result_std = np.std(array) result_sum = np.sum(array) print("Mean:", result_mean) print("Standard Deviation:", result_std) print("Sum:", result_sum) 

7. Mathematical constants in NumPy:

NumPy provides mathematical constants like np.pi and np.e.

# Mathematical constants pi_value = np.pi e_value = np.e print("Pi:", pi_value) print("Euler's number:", e_value) 

8. Vectorized mathematical operations in NumPy:

NumPy is designed for vectorized operations, meaning you can perform operations on entire arrays.

# Assuming 'array' is already defined # Vectorized operations result_vectorized = 2 * array + np.sin(array) print("Vectorized operation result:", result_vectorized) 

More Tags

pseudo-element google-sheets-macros wildfly uifont vaadin jsonpath rails-admin vpn microservices interop

More Programming Guides

Other Guides

More Programming Examples