Hello! Down below is my code for the project - I found the coding aspect fairly easy but the lack of hand holding and guidance was something I initially found a little intimidating. I’m not sure if my code checks all the boxes. Specifically I would like to know how the code could be made more efficient, I feel like I clumsily write 5 lines where there could be 2. Thanks for your help!
import csv
#store information in list variables
age_lst = []
bmi_lst = []
sex_lst = []
children_lst = []
smoker_lst = []
charges_lst = []
region_lst = []
full_dict = []
#store other datatypes in dictionary format too
sex_dict = {"female": 0, "male": 0}
children_dict = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
smoker_dict = {"Yes": 0, "No": 0}
region_dict = {"Northwest": 0, "Southeast": 0, "Southwest": 0, "Northeast": 0}
#create the lists
with open('insurance.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
age_lst.append(int(row['age']))
bmi_lst.append(float(row['bmi']))
charges_lst.append(row['charges'])
sex_lst.append(row["sex"])
children_lst.append(int(row["children"]))
smoker_lst.append(row["smoker"])
region_lst.append(row["region"])
sex_dict[row["sex"]] += 1
children_dict[int(row["children"])] += 1
smoker_dict[row["smoker"].capitalize()] += 1
region_dict[row["region"].capitalize()] += 1
full_dict.append(row)
#Function to determine the average insurance cost per weight category
def bmi_price_dictionary_maker():
underweight = 0
u_total_cost = 0
healthy_weight = 0
h_total_cost = 0
overweight = 0
over_total_cost = 0
obese = 0
obese_total_cost = 0
for item in full_dict:
if float(item['bmi']) < 18.5:
underweight += 1
u_total_cost += float(item["charges"])
elif float(item['bmi']) < 24.9:
healthy_weight += 1
h_total_cost += float(item["charges"])
elif float(item['bmi']) < 29.9:
overweight += 1
over_total_cost += float(item["charges"])
else:
obese += 1
obese_total_cost += float(item["charges"])
bmi_price_dictionary = {"Underweight": round((u_total_cost/underweight), 2), "Healthy Weight": round((h_total_cost/healthy_weight), 2), "Overweight": round((over_total_cost/overweight), 2), "Obese": round((obese_total_cost/obese), 2)}
return bmi_price_dictionary
print(bmi_price_dictionary_maker())
class patientsInfo:
#initialise the class
def __init__(self, age, sex, bmi, children, smoker, region, charges):
self.age = age
self.sex = sex
self.bmi = bmi
self.children = children
self.smoker = smoker
self.region = region
self.charges = charges
#various methods which return the averages of some traits in the sampled group
def age_analysis(self):
average = round(sum(self.age)/len(self.age), 2)
print ("The average age of the people in this dataset is {}".format(average))
return average
def sex_analysis(self):
males_count = self.sex.count("male")
females_count = self.sex.count("female")
print("The number of males: {males}. The number of females: {females}".format(males=males_count, females=females_count))
def bmi_analysis(self):
average = round(sum(self.bmi)/len(self.bmi), 2)
health = ""
if average < 18.5:
health = "underweight"
elif average < 24.9:
health = "healthy"
elif average < 29.9:
health = "overweight"
else:
health = "obese"
print("The average BMI of the people in this dataset is {}, this is considered {}.".format(average, health))
return average
#make an if statement to say if they are healthy weight on average
def children_analysis(self):
average = round(sum(self.children)/len(self.children), 2)
print("The average number of children people in this dataset have is {}.".format(average))
return average
my_info = patientsInfo(age_lst, sex_lst, bmi_lst, children_lst, smoker_lst, region_lst, charges_lst)
my_info.age_analysis()
my_info.sex_analysis()
my_info.bmi_analysis()
my_info.children_analysis()