Instagram
youtube
Facebook
Twitter

Arithmetic Operations on Numpy Arrays

Numpy provides a wide range of functions for working with arrays, which are like lists but can be used for mathematical operations efficiently. In this tutorial, we'll explore some basic arithmetic operations you can perform on NumPy arrays.

Addition

Addition is the most basic operation in mathematics. We can add NumPy arrays element-wise using the "+" operator:

Example:

import numpy as np
array1 = np.array([1,2,3,4])
array2 = np.array([5,6,7,8])
result_addition = array1 + array2
print("Addition Result:", result_addition)

Result:

Addition Result: [ 6  8 10 12]

Subtraction

Subtraction works similar to addition. It subtracts each element in the first array from the corresponding element in the second array.

Example:

import numpy as np
array1 = np.array([1,2,3,4])
array2 = np.array([5,6,7,8])
result_subtraction = array1 - array2
print("Subtraction Result:", result_subtraction)

Result:

Subtraction Result: [-4 -4 -4 -4]

Multiplication

Similar to multiplication operation on python integers we can multiply arrays element-wise using the "*" operator.

Example:

import numpy as np
array1 = np.array([1,2,3,4])
array2 = np.array([5,6,7,8])
result_multiplication = array1 * array2
print("Multiplication Result:", result_multiplication)

Result:

Multiplication Result: [ 5 12 21 32]

Division

To divide one array by another array (element-wise), we can use the "/" operator

Example:

import numpy as np
array1 = np.array([1,2,3,4])
array2 = np.array([5,6,7,8])
result_division = array1 / array2
print("Division Result:", result_division)

Result:

Division Result: [0.2        0.33333333 0.42857143 0.5       ]