• 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 [1]:
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import statsmodels.formula.api as smf

In [2]:
df=pd.read_csv('heights_weights_genders.csv')

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

In [4]:
df_female.head()


Out[4]:
Gender Height Weight
5000 Female 58.910732 102.088326
5001 Female 65.230013 141.305823
5002 Female 63.369004 131.041403
5003 Female 64.479997 128.171511
5004 Female 61.793096 129.781407

In [5]:
lm1 = smf.ols(formula="Weight~Height",data=df_male).fit()

In [9]:
intercept_male, slope_male = lm1.params

In [7]:
lm2 = smf.ols(formula="Weight~Height",data=df_female).fit()

In [10]:
intercept_female, slope_female = lm2.params

In [28]:
def predict_weight_gender(): 
    input_gender=input("What's your gender?").upper()
    input_height=int(input("What's your height?"))
    if input_gender == 'MALE': 
        return (slope_male*input_height) + intercept_male
    elif input_gender == 'FEMALE': 
        return (slope_female*input_height) + intercept_female
    else: 
        print('Invalid inputs')

In [29]:
predict_weight_gender()


What's your gender?Female
What's your height?68
Out[29]:
161.58190406343221