Arithmetic Operations and Universal Functions in Numpy¶
Numpy provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on them efficiently. Understanding how to perform arithmetic operations and utilize universal functions (ufuncs) is essential for data analysis, machine learning, and scientific computations.
Importing NumPy¶
Before you begin working with NumPy, you need to import the library. It's standard practice to import NumPy using the alias np for convenience.
import numpy as np
# Creating two arrays
arr1 = np.arange(0, 5)
arr2 = np.arange(6, 11)
- arr1 will contain [0, 1, 2, 3, 4].
- arr2 will contain [6, 7, 8, 9, 10].
Addition¶
You can add two arrays element-wise using the + operator:
# Element-wise addition
addition_result = arr1 + arr2
addition_result
array([ 6, 8, 10, 12, 14])
This operation adds each element of arr1 to the corresponding element in arr2.
Subtraction¶
Subtraction is also performed element-wise using the - operator:
# Element-wise subtraction
subtraction_result = arr2 - arr1
This subtracts each element of arr1 from the corresponding element in arr2.
Multiplication¶
Element-wise multiplication uses the *** operator:
# Element-wise multiplication
multiplication_result = arr1 * arr2
Each element in arr1 is multiplied by the corresponding element in arr2.
Division¶
Element-wise division is performed using the / operator:
# Element-wise division
division_result = arr1 / arr2
This divides each element in arr1 by the corresponding element in arr2.
Note: Be cautious of division by zero. In this example, arr1 starts with 0, but since arr2 does not contain zero, division by zero does not occur.
Exponentiation (Squaring)¶
You can raise each element of an array to a power using the **** operator:
# Element-wise exponentiation (squaring arr1)
squared_result = arr1 ** 2
This squares each element in arr1.
Universal Functions (ufuncs)¶
Universal functions, or ufuncs, are functions that operate element-wise on arrays. They are highly optimized and support broadcasting, typecasting, and several other features.
Mathematical Functions¶
NumPy provides a range of mathematical functions that operate element-wise.
Addition (np.add)¶
# Using np.add to add two arrays
addition_result = np.add(arr1, arr2)
This function performs element-wise addition of arr1 and arr2.
Subtraction (np.subtract)¶
# Using np.subtract to subtract arr2 from arr1
subtraction_result = np.subtract(arr1, arr2)
subtraction_result
array([-6, -6, -6, -6, -6])
Subtracts each element in arr2 from the corresponding element in arr1.
Multiplication (np.multiply)¶
# Using np.multiply to multiply two arrays
multiplication_result = np.multiply(arr1, arr2)
Multiplies each element in arr1 by the corresponding element in arr2.
Division (np.divide)¶
# Using np.divide to divide arr1 by arr2
division_result = np.divide(arr1, arr2)
Divides each element in arr1 by the corresponding element in arr2.
Power (np.power)¶
# Using np.power to raise elements of arr1 to powers from arr2
power_result = np.power(arr1, arr2)
power_result
array([ 0, 1, 256, 19683, 1048576])
# Calculating the sine of each element in arr1
sine_result = np.sin(arr1)
Computes the sine of each element in arr1.
Cosine (np.cos)¶
# Calculating the cosine of each element in arr1
cosine_result = np.cos(arr1)
Computes the cosine of each element in arr1.
Tangent (np.tan)¶
# Calculating the tangent of each element in arr2
tangent_result = np.tan(arr2)
# Calculating the exponential (e^x) of each element in arr2
exponential_result = np.exp(arr2)
Computes the exponential of each element in arr2.
Logarithm (np.log)¶
# Calculating the natural logarithm of each element in arr2
logarithm_result = np.log(arr2)
# Comparing elements of arr1 and arr2
greater_result = np.greater(arr1, arr2)
Returns True where elements in arr1 are greater than those in arr2, else False.
Less (np.less)¶
# Checking if elements in arr1 are less than those in arr2
less_result = np.less(arr1, arr2)
Returns True where elements in arr1 are less than those in arr2, else False.