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]:
import pandas as pd


/usr/local/lib/python3.5/site-packages/matplotlib/__init__.py:1035: UserWarning: Duplicate key in file "/Users/mercybenzaquen/.matplotlib/matplotlibrc", line #2
  (fname, cnt))

In [3]:
import statsmodels.formula.api as smf

In [4]:
df = pd.read_csv("heights_weights_genders.csv")

In [5]:
df.head()


Out[5]:
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

y = ß0 + ß1x

Here:

y: is the variable that we want to predict

ß0: is intercept of the regression line i.e. value of y when x is 0

ß1: is coefficient of x i.e. variation in y with change in value of x

x: Variables that affects value of y i.e. already know variable whose effect we want to se on values of y


In [6]:
lm = smf.ols(formula="Weight ~ Height + Gender",data=df).fit()

In [7]:
lm.params


Out[7]:
Intercept        -244.923503
Gender[T.Male]     19.377711
Height              5.976941
dtype: float64

In [ ]:
given_height= input("What's your height")

In [8]:
given_gender= input('Male or Famale')


Male or FamaleMale

In [12]:
def weight_calculator(my_height, my_gender): 
    Intercept= -244.923503
    Male= 19.377711 #add to males
    Slope= 5.976941
    if my_gender == 'Male':
        return (Intercept + Male + (Slope * float(my_height)))
    else:
        return (Intercept + (Slope * float(my_height)))

In [13]:
weight_calculator(73.847017, 'Male')


Out[13]:
215.83347163499695

In [14]:
weight_calculator(73.847017, 'Female')


Out[14]:
196.45576063499695

In [ ]: