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