In [3]:
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import statsmodels.formula.api as smf
In [4]:
df = pd.read_csv("../data/heights_weights_genders.csv")
In [5]:
df.head(5)
Out[5]:
In [12]:
women = df[df['Gender']=='Female']
men = df[df['Gender']=='Male']
In [14]:
lm_f = smf.ols(formula="Weight~Height",data=women).fit()
lm_m = smf.ols(formula="Weight~Height",data=men).fit()
In [15]:
lm_f.params
Out[15]:
In [16]:
lm_m.params
Out[16]:
In [18]:
intercept_f, slope_f = lm_f.params
intercept_m, slope_m = lm_m.params
In [21]:
women.plot(kind="scatter",x="Height",y="Weight",color='lightgray',alpha=0.2,linewidth=0,title ="women")
plt.plot(women["Height"],slope_f*women["Height"]+intercept_f,"-",color="pink",alpha=1)
Out[21]:
In [22]:
men.plot(kind="scatter",x="Height",y="Weight",color='lightgray',alpha=0.2,linewidth=0,title ="women")
plt.plot(men["Height"],slope_m*men["Height"]+intercept_m,"-",color="steelblue",alpha=1)
Out[22]:
In [23]:
def get_weight(height,gender):
if gender == "female":
print("Your height is", height,"and your estimate weight will be", slope_f*height+intercept_f)
elif gender == "male":
print("Your height is", height,"and your estimate weight will be", slope_m*height+intercept_m)
else:
print("Gender must be male or female, try again!")
In [25]:
get_weight(60,"female")
In [ ]: