In [1]:
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt # package for doing plotting (necessary for adding the line)
import statsmodels.formula.api as smf # package we'll be using for linear regression

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. Find the weights and use those in your function (i.e. don't generate a model each time)


In [2]:
df = pd.read_csv("data/heights_weights_genders.csv")

In [11]:
df.head()


Out[11]:
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 [4]:
lm = smf.ols(formula="Weight~Height",data=df).fit() #notice the formula regresses Y on X (Y~X)

In [5]:
lm.params #get the parameters from the model fit


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

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

In [8]:
df.plot(kind="scatter",x="Height",y="Weight")
plt.plot(df["Height"],slope*df["Height"]+intercept,"-",color="red") #we create the best fit line from the values in the fit model


Out[8]:
[<matplotlib.lines.Line2D at 0x10769cbe0>]

In [49]:
def weight_predictor(height):
    return slope*height + intercept

In [50]:
weight_predictor(73.847017)


Out[50]:
219.16147979082859

In [ ]: