Algorithms by Richard Dunks is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative-Commons-License 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 matplotlib.pyplot as plt
%matplotlib inline
import statsmodels.formula.api as smf

In [2]:
df = pd.read_csv("/home/sean/git/algorithms/class5/data/heights_weights_genders.csv")

In [9]:
male_df=df[df['Gender']=='Male']

In [11]:
male_lm = smf.ols(formula="Weight~Height",data=male_df).fit()

In [12]:
male_lm.params


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

In [13]:
male_intercept, male_slope = male_lm.params

In [14]:
female_df=df[df['Gender']=='Female']

In [15]:
female_lm = smf.ols(formula="Weight~Height",data=female_df).fit()

In [16]:
female_lm.params


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

In [45]:
female_intercept, female_slope = female_lm.params

In [17]:
def male_wt_predict(str_ht):
    ht=float(str_ht)
    return '%s' % float('%.4g' % (male_slope*ht+male_intercept))

In [18]:
def female_wt_predict(str_ht):
    ht=float(str_ht)
    return '%s' % float('%.4g' % (female_slope*ht+female_intercept))

In [19]:
def determine_gender(input_str):
    return input_str[0].lower()

In [49]:
gender_str=input('Enter your gender, (m)ale or (f)emale: ')
gender=determine_gender(gender_str)
while not (gender == 'm') and not (gender == 'f'):
    gender_str=input('Please enter male or female. You can also just enter m or f: ')
    gender=determine_gender(gender_str)
ht=input('Enter your height (inches):')
if gender=='m':
    wt=male_wt_predict(ht)
else:
    wt=female_wt_predict(ht)
print('Predicted weight of {} lbs from height of {} in.'.format(wt, ht))


Enter your gender, (m)ale or (f)emale: zebra
Please enter male or female. You can also just enter m or f: m
Enter your height (inches):75
Predicted weight of 222.6 lbs from height of 75 in.

In [ ]: