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 # package for doing plotting (necessary for adding the line)
import statsmodels.formula.api as smf # package we'll be using for linear regression
    
In [ ]:
    
    
In [2]:
    
df = pd.read_csv("data/heights_weights_genders.csv")
# Male
lm_male = smf.ols(formula="Weight~Height",data=df[df['Gender']=='Male']).fit()
intercept_male, slope_male = lm_male.params
# Female
lm_female = smf.ols(formula="Weight~Height",data=df[df['Gender']=='Female']).fit()
intercept_female, slope_female = lm_female.params
    
In [3]:
    
df.head()
    
    Out[3]:
In [38]:
    
intercept_male
    
    Out[38]:
In [39]:
    
intercept_female
    
    Out[39]:
In [34]:
    
def predicting_weight(gender,height):
    if gender == 'Male':
        return intercept_male + float(height) * slope_male
    elif gender == 'Female':
        return intercept_female + float(height) * slope_female
    
In [37]:
    
y = input("Please insert the height ")
x = input("Male or Female? ")
predicting_weight(x,y)
    
    
    Out[37]:
Additional Analysis
In [36]:
    
lm_male.params #get the parameters from the model fit
    
    Out[36]:
In [41]:
    
lm_female.params
    
    Out[41]:
In [31]:
    
lm_male.summary()
    
    Out[31]:
In [40]:
    
lm_female.summary()
    
    Out[40]:
In [ ]: