In [1]:
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import statsmodels.formula.api as smf
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]:
In [4]:
lm = smf.ols(formula="Weight~Height",data=df).fit()
In [5]:
lm.params
Out[5]:
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]:
In [26]:
df2.head(2)
Out[26]:
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]:
In [17]:
lm.male.params
Out[17]:
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]:
In [29]:
#Example 2
Gender = 'Female'
height = 58.910732
weight_predictor(height, Gender)
Out[29]:
In [ ]:
In [ ]: