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

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 [2]:
df = pd.read_csv("data/heights_weights_genders.csv")

In [3]:
df.head()


Out[3]:
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 [4]:
lm = smf.ols(formula="Weight~Height",data=df).fit()

In [5]:
lm.params


Out[5]:
Intercept   -350.737192
Height         7.717288
dtype: float64

In [6]:
intercept, slope = lm.params

In [10]:
df1 = df[df['Gender'] == 'Female']
df2 = df[df['Gender'] == 'Male']

In [25]:
df1.head(2)


Out[25]:
Gender Height Weight
5000 Female 58.910732 102.088326
5001 Female 65.230013 141.305823

In [26]:
df2.head(2)


Out[26]:
Gender Height Weight
0 Male 73.847017 241.893563
1 Male 68.781904 162.310473

In [15]:
lm.female = smf.ols(formula="Weight~Height",data=df1).fit()
lm.male = smf.ols(formula="Weight~Height",data=df2).fit()

In [16]:
lm.female.params


Out[16]:
Intercept   -246.013266
Height         5.994047
dtype: float64

In [17]:
lm.male.params


Out[17]:
Intercept   -224.498841
Height         5.961774
dtype: float64

In [21]:
F_intercept, F_slope = lm.female.params

In [20]:
M_intercept, M_slope = lm.male.params

In [22]:
def weight_predictor(height, Gender):
    if Gender == 'Female':
        return F_slope*height + F_intercept
    elif Gender == 'Male':
        return M_slope*height + M_intercept

In [27]:
#Example 1

Gender = 'Male'
height = 73.847017

weight_predictor(height, Gender)


Out[27]:
215.76037134150519

In [29]:
#Example 2

Gender = 'Female'
height = 58.910732

weight_predictor(height, Gender)


Out[29]:
107.10040797652709

In [ ]:


In [ ]: