Assignment 2

  • Create two models for the relationship between height and weight based on gender
  • Modify the code in Assignment 1 to ask for a person's gender as well as their height to produce an estimate of a person's weight using the models you created
  • Find the weights and use those in your function (i.e. don't generate a model each time)

In [57]:
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import statsmodels.formula.api as smf

In [58]:
df = pd.read_csv("data/heights_weights_genders.csv")

In [59]:
df.head()


Out[59]:
Gender Height Weight
0 Male 73.847017 241.893563
1 Male 68.781904 162.310473
2 Male 74.110105 212.740856
3 Male 71.730978 220.042470
4 Male 69.881796 206.349801

In [60]:
users_sex = input('Please enter your sex, m or f: ')
users_height = int(input('Please enter your height in inches: '))


Please enter your sex, m or f: m
Please enter your height in inches: 180

In [61]:
df_male = df[df['Gender'] == 'Male']
df_female = df[df['Gender'] == 'Female']

In [62]:
if users_sex == 'f':
    lm = smf.ols(formula="Weight~Height",data=df_female).fit()
elif users_sex == 'm':
    lm = smf.ols(formula="Weight~Height",data=df_male).fit()
else:
    print("You didn't enter your sex correctly.")

intercept, height = lm.params
weight = height*users_height+intercept
print('You are approximately', round(weight), "pounds, which is", round(weight * 0.453592), 'kg.')


You are approximately 849.0 pounds, which is 385.0 kg.

In [ ]: