Array Math or Arithmetic
can be performed
very easily on NumPy Array’s. For example, addition of two rank 2 arrays can be done as:
Program 1: Add two numpy array’s using “+” operator.
import numpy as np A = np.array([[1,2,3],[4,5,6]]) B = np.array([[7,8,9],[10,11,12]]) print("Addition of A and B is:") print(A + B)
Output:
Addition of A and B is: [[ 8 10 12] [14 16 18]]
Array Math is important, as it can be used to perform vector calculations. One of the good example is as follows:
Program 2: Add two numpy array’s and plot the resultant graph.
import numpy as np X = np.array([2,3]) Y = np.array([4,2]) print("Addition of X and Y is:") print(X + Y)
Output:
Addition of X and Y is: [6 5]
The resultant graph shows the use of arrays to represent vectors and uses array addition to perform vector addition.
Graph: Using array addition for vector addition.
Besides using the + operator, we can also use the np.add() function to add two arrays.
Program 3: Add two numpy array’s using np.add() function.
import numpy as np A = np.array([[1,2,3],[4,5,6]]) B = np.array([[7,8,9],[10,11,12]]) print("Addition of A and B is:") print(np.add(A, B))
Output:
Addition of A and B is: [[ 8 10 12] [14 16 18]]
Apart from addition; subtraction, multiplication, as well as division can be performed with NumPy arrays.
Program 4: Subtract two numpy array’s using “-” operator.
import numpy as np A = np.array([[1,2,3],[4,5,6]]) B = np.array([[7,8,9],[10,11,12]]) print("Subtraction of A and B is:") #same as np.subtract(A,B) print(A - B)
Output:
Subtraction of A and B is: [[-6 -6 -6] [-6 -6 -6]]
Program 5: Multiply two numpy array’s using “ * “ operator.
import numpy as np A = np.array([[1,2,3],[4,5,6]]) B = np.array([[7,8,9],[10,11,12]]) print("Multiplication of A and B is:") #same as np.multiply(A,B) print(A * B)
Output:
Multiplication of A and B is: [[ 7 16 27] [40 55 72]]
Program 6: Divide two numpy array’s using “ / “ operator.
import numpy as np A = np.array([[1,2,3],[4,5,6]]) B = np.array([[7,8,9],[10,11,12]]) print("Division of A and B is:") #same as np.divide(A,B) print(A / B)
Output:
Division of A and B is: [[0.14285714 0.25 0.33333333] [0.4 0.45454545 0.5 ]]
What’s a practical use of the ability to multiply or divide two arrays?
Let’s suppose we have three arrays; one containing the names of a group of people, another the corresponding heights of these individuals, and the last one the corresponding weights of the individuals in the group. We want to calculate the Body Mass Index (BMI) of this group of people. The formula to calculate BMI is as follows:
Divide the weight in kilograms (kg) by the height in meters (m).
Divide the answer by the height again.
Using the BMI, you can classify a person as healthy, overweight, or underweight using the following categories:
Underweight if BMI < 18.5.
Overweight if BMI > 25.
Normal weight if 18.5 <= BMI <= 25.
Using array division, we could simply calculate BMI.
Program 7: Calculate the body mass index (BMI) of the group of people and classify them as healthy, overweight and underweight.
import numpy as np names = np.array(['Nirmal','Priyanka','Resham']) heights = np.array([1.5, 1.78, 1.6]) weights = np.array([65, 46, 59]) bmi = weights/heights **2 # calculate the BMI print("The Body Mass Index (BMI) of Nirmal, Priyanka and Resham is:", bmi) print("Overweight: " , names[bmi>25]) # Overweight: ['Nirmal'] print("Underweight: " , names[bmi<18.5]) # Underweight: ['Priyanka'] print("Healthy: " , names[(bmi>=18.5) & (bmi<=25)]) # Healthy: ['Resham']
Output:
The Body Mass Index (BMI) of Nirmal, Priyanka and Resham is: [28.88888889 14.51836889 23.046875 ] Overweight: ['Nirmal'] Underweight: ['Priyanka'] Healthy: ['Resham']
Sir thanks for providing this such information
this is being very helpfull as I'm currently working on numpy