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 [1]:
import pandas as pd
import statsmodels.formula.api as smf

In [2]:
df = pd.read_csv("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 [5]:
male = df[df['Gender']=='Male']
female = df[df['Gender']=='Female']

In [6]:
lm_male = smf.ols(formula="Weight~Height",data=male).fit()
lm_male.params


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

In [7]:
lm_female = smf.ols(formula="Weight~Height",data=female).fit()
lm_female.params


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

In [28]:
gender = input("Enter your gender(F/M):")


Enter your gender(F/M):m

In [30]:
height = input("Enter your height(inches):")


Enter your height(inches):99

In [33]:
def pre_weight(input_gender,input_height):
    if input_gender.upper() == 'F':
        weight = -224.498841 + (5.961774*float(input_height))
        return weight
    elif input_gender.upper() == 'M':
        weight = -246.013266 + (5.994047*float(input_height))
        return weight
    else: 
        print("Please enter your gender again")

In [34]:
pre_weight(gender,height)


Out[34]:
347.39738700000004

In [ ]: