NumPy provides built-in functions for performing simple arithmetic operations on arrays efficiently. These operations include addition, subtraction, multiplication, division, and exponentiation. Here's how to perform these operations using NumPy:
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Using numpy.add()
result_add = np.add(arr1, arr2)
# Using the + operator
result_add_operator = arr1 + arr2
print("Using numpy.add():", result_add) b
print("Using + operator:", result_add_operator)
import numpy as np
arr1 = np.array([4, 5, 6])
arr2 = np.array([1, 2, 3])
# Using numpy.subtract()
result_subtract = np.subtract(arr1, arr2)
# Using the - operator
result_subtract_operator = arr1 - arr2
print("Using numpy.subtract():", result_subtract)
print("Using - operator:", result_subtract_operator)
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Using numpy.multiply()
result_multiply = np.multiply(arr1, arr2)
# Using the * operator
result_multiply_operator = arr1 * arr2
print("Using numpy.multiply():", result_multiply)
print("Using * operator:", result_multiply_operator)
import numpy as np
arr1 = np.array([6, 12, 18])
arr2 = np.array([2, 3, 6])
# Using numpy.divide()
result_divide = np.divide(arr1, arr2)
# Using the / operator
result_divide_operator = arr1 / arr2
print("Using numpy.divide():", result_divide)
print("Using / operator:", result_divide_operator)
import numpy as np
arr = np.array([2, 3, 4])
# Using numpy.power()
result_power = np.power(arr, 2)
# Using the ** operator
result_power_operator = arr ** 2
print("Using numpy.power():", result_power)
print("Using ** operator:", result_power_operator)
These are some of the basic arithmetic operations you can perform on NumPy arrays. NumPy provides efficient implementations for these operations, making it suitable for numerical computations and scientific computing tasks.