calculate BMI by numpy array
Numpy arrays are great alternatives to Python Lists. Some of the key advantages of Numpy arrays are that they are fast, easy to work with, and give users the opportunity to perform calculations across entire arrays.
In the following example, you will first create two Python lists. Then, you will import the numpy package and create numpy arrays out of the newly created lists.
# Create 2 new lists height and weight height = [1.6, 1.87, 1.87, 1.82, 1.91] weight = [55.2,81.65, 97.52, 95.25, 92.98] print("height") print(height) print("weight") print(weight) # Import the numpy package as np import numpy as np # Create 2 numpy arrays from height and weight np_height = np.array(height) np_weight = np.array(weight) print("type") print(type(np_height)) # Calculate bmi bmi = np_weight / np_height ** 2 # Print the result print("BMI") print(bmi) # For a boolean response print("bmi > 23") print(bmi > 23) print("bmi < 23") print(bmi < 23) # Print only those observations above 23 print("bmi[bmi > 23]") print(bmi[bmi > 23]) ==== RESTART: C:/python/calculateBMIbyNumpy.py ===== height [1.6, 1.87, 1.87, 1.82, 1.91] weight [55.2, 81.65, 97.52, 95.25, 92.98] typeBMI [21.5625 23.34925219 27.88755755 28.75558507 25.48723993] bmi > 23 [False True True True True] bmi < 23 [ True False False False False] bmi[bmi > 23] [23.34925219 27.88755755 28.75558507 25.48723993] >>>
コメント
コメントを投稿