Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
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.
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.
import numpy as np
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]
Computing the exponential of all elements in the input array:
print(np.exp(a)) # e^x for each element of a
print(np.log(a)) # Natural log print(np.log10(a)) # Base 10 log print(np.log2(a)) # Base 2 log
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))
arr = np.array([1.65, 2.05, 3.57, 4.89]) print(np.round(arr))
print(np.floor(arr)) # Rounds down print(np.ceil(arr)) # Rounds up
print(np.sum(a)) # Sum of all elements print(np.prod(a)) # Product of all elements
Computing the n-th discrete difference:
print(np.diff(a)) # Outputs [1 1] because 2-1 = 1 and 3-2 = 1
print(np.power(a, 2)) # Square of each element print(np.sqrt(a)) # Square root of each element
c = np.array([-1, -2, 3]) print(np.abs(c)) # [1 2 3]
Returns an element-wise indication of the sign of a number:
print(np.sign(c)) # [-1 -1 1]
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.
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)
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)
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)
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)
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)
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)
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)
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)