Question: Python BMI Program, Convert from arrays to classes Python code: import csv import matplotlib.pyplot as plt import numpy as np import matplotlib.mlab as mlab obj=open(bmi.csv);

Python BMI Program, Convert from arrays to classes

Python code:

import csv import matplotlib.pyplot as plt import numpy as np import matplotlib.mlab as mlab

obj=open("bmi.csv"); #opening file

obj_reader=csv.reader(obj,delimiter=' ') male=[] # having male's bmi female=[] # having female bmi

file_obj2=open("bmi_health.csv","w") # opening file for adding bmi and status

for val in obj_reader: bmi=(int(val[1])*2.2*703)/(int(val[2])*int (val[2])) # bmi calculation bmi=round(bmi,2) file_obj2.write(val[0]) file_obj2.write(' ') file_obj2.write(val[1]) file_obj2.write(' ') file_obj2.write(val[2]) file_obj2.write(' ') file_obj2.write("%0.2f "%bmi) file_obj2.write(' ') if val[0]=='male': # checking for male male.append(bmi) if bmi>=19 and bmi<=25: # bmi healthy status file_obj2.write('healthy') elif bmi>25: file_obj2.write('obese') else: file_obj2.write('underweight') else: # checking for male female.append(bmi) if bmi>=19 and bmi<=25: # bmi healthy status

file_obj2.write('healthy') elif bmi>25:

file_obj2.write('obese') else:

file_obj2.write('underweight') file_obj2.write(' ') print(" BMI list for male: ") print(male) # male bmi calculation list print(" BMI list for female: ") print(female) #female bmi calculation list obj.close() # closing old file file_obj2.close() #closing new file bins=1 plt.ylabel('NUMBER') plt.xlabel('BMI') plt.title('HISTROGRAM OF BMI OF MALE & FEMALE ') plt.hist([male,female]) plt.show()

INITIAL FILE NAMED AS : bmi.csv

From the previous assignment on BMI(above but you may rewrite the program as necessary) rewrite the program using classes instead of an array.

Read the BMI file from before (attached again (since documents are not attachable, please make up numbers to demonstrate that the code functions). Filter the data to output to a file only patients that are classified as obese or underweight, and output those two sets to different csv files. Specifically, in base Python, make your own class for reading .csv files. Define a class called Patient. Feed one line from the CSV into Patient at a time, into the class constructor, which should parse the line and set the variables Gender, Height and Weight. You should construct a list of instances of class Patient from all the data in the CSV file. Additionally, the class should have a method bmi to calculate and store a column associated with bmi, then another method which classifies the health status (using bmi) as health, overweight or underweigh. Finally have a method which prints the data from the class out to a file, one index of the list of your class at a time. Save this program as "hw5_q1.py"

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!