Home > Software engineering >  Print out a numpy array with the BMIs of all players whose BMI is below 21
Print out a numpy array with the BMIs of all players whose BMI is below 21

Time:12-28

Use light inside square brackets to do a selection on the bmi array.

import numpy as np
Calculate the BMI: bmi
np_height_m = np.array(height_in) * 0.0254
np_weight_kg = np.array(weight_lb) * 0.453592
bmi = np_weight_kg / np_height_m ** 2
print(bmi)
Create the light array
light = bmi<21
Print out light
print(light)
Print out BMIs of all baseball players whose BMI is below 21
bmi[light<21]

CodePudding user response:

light is an array of booleans. You should use it directly to slice:

bmi[light]

When you run:

bmi[light<21]  # equivalent to bmi[(bmi<21)<21]

This compares the booleans to 21, which is always True as True equals 1 and False equals 0. This thus yields all elements.

Of note, you don't need the intermediate light array, you could use directly:

bmi[bmi<21]

CodePudding user response:

import numpy as np

Calculate the BMI: bmi

np_height_m = np.array(height_in) * 0.0254
np_weight_kg = np.array(weight_lb) * 0.453592
bmi = np_weight_kg / np_height_m ** 2
print(bmi)

Create the light array

light = bmi<21

Print out light

print(light)

Print out BMIs of all baseball players whose BMI is below 21

bmi[light]
  • Related