Assignment 1

Use the data from heights_weights_genders.csv to create a simple predictor that takes in a person's height and guesses their weight based on a model using all the data, regardless of gender. To do this, find the parameters (lm.params) and use those in your function (i.e. don't generate a model each time)

In [6]:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import statsmodels.formula.api as smf

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

In [8]:
df.head()


Out[8]:
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 [11]:
lm = smf.ols(formula="Weight~Height",data=df).fit()

In [12]:
lm.params


Out[12]:
Intercept   -350.737192
Height         7.717288
dtype: float64

In [13]:
intercept, slope = lm.params

In [41]:
plt.scatter(x=df['Height'], y=df['Weight'])
plt.plot(df["Height"],slope*df["Height"]+intercept,"-",color="red")


Out[41]:
[<matplotlib.lines.Line2D at 0x7fc825885b00>]

In [32]:
def predict_wt(str_ht):
    ht=float(str_ht)
    return '%s' % float('%.4g' % (slope*ht+intercept))

In [37]:
ht=input('Enter your height (inches):')
print('Predicted weight:', predict_wt(ht)+'lbs')


Enter your height (inches):60
Predicted weight: 112.3lbs

In [ ]: