In: Statistics and Probability
BMI= 70 (kg)1.752(m2)=22.86 (kg/m2)
σ=i=1n(xi-μ)2n
where:
xi is the ith value in the BMI data
μ: is the mean value of BMI
n: is the size of the population
Sol:
code :
import math
# read lines from file.
# split the lines at spaces , convert each value to float.
# conver the height in inches to meters.
# covert the weight from pounds to kgs.
def get_data(filename):
file = open(filename)
height = []
weight = []
lines = file.readlines()
for line in lines:
line = list(map(float , line.split(" ")))
height.append(line[1]*0.0254)
weight.append(line[2]*0.453592)
return height , weight
# function to calculate the bmi : weight / height
def calculate_bmi(height , weight):
bmi = []
for h,w in zip(height , weight):
bmi.append(w / h)
return bmi
# function to get teh status.
def gen_status(bmi):
status = []
for i in range(len(bmi)):
if bmi[i] < 18.5:
status.append("Underweight")
elif bmi[i] >= 18.5 and bmi[i] <= 24.9:
status.append("Normal Weight")
elif bmi[i] >= 25.0 and bmi[i] <= 20.9:
status.append("Overweight")
else:
status,append("Obesity")
return status
# function to generate the statistics (mean , std_dev ,
count)
def gen_stats(BMI , status):
counts = [0]*4
mean_bmi = sum(BMI) / len(BMI)
std_dev = 0
for i in range(len(BMI)):
if status[i] == "Underweight":
counts[0] += 1;
elif status[i] == "Normal Weight":
counts[1] += 1;
elif status[i] == "Overweight":
counts[2] += 1;
else:
counts[3] += 1;
std_dev += (BMI[i] - mean_bmi)**2
std_dev /= len(BMI)
return mean_bmi , math.sqrt(std_dev) , counts
def print_data(height , weight , bmi , status , counts ,
mean_bmi , std_dev):
print(" Welcome to the BMI Calculator ")
print("Underweight\t\tNormal\t\tOverweight\t\tObese")
print(counts[0],"\t\t",counts[1],"\t\t",counts[2],"\t\t",counts[3])
print("***********************************")
print("Height\t\tWeight\t\tBMI\t\tStatus")
for i in range(0,10):
print(f"{round(height[i],2)}\t\t{round(weight[i],2)}\t\t{round(bmi[i],2)}\t\t{status[i]}")
print(".......")
for i in range(-1,-10,-1):
print(f"{round(height[i],2)}\t\t{round(weight[i],2)}\t\t{round(bmi[i],2)}\t\t{status[i]}")
print("The average BMI : ", mean_bmi)
print("The standard deviation for BMI is : " , std_dev)
def main():
filename = "hw_25000.txt"
height , weight = get_data(filename)
bmi = calculate_bmi(height , weight)
status = gen_status(bmi)
mean_bmi , std_dev , counts = gen_stats(bmi , status)
print_data(height , weight , bmi , status , counts , mean_bmi ,
std_dev)
if __name__ == "__main__":
main()
Output:
If you Satisfy with Answer, Please give me "Thumb Up". It was very important to me.