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 [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

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

In [3]:
lm = smf.ols(formula="Weight~Height",data=df).fit() #notice the formula regresses Y on X (Y~X)

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


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

In [5]:
intercept, slope = lm.params #assign those values to variables

In [6]:
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[6]:
[<matplotlib.lines.Line2D at 0x96bc978>]

In [7]:
def predicting_weight(height):
    return intercept + float(height) * slope

In [8]:
x = input("What is the height? ")
print("Expected Weight : "+str(predicting_weight(x)))


What is the height? 73.84
Expected Weight : 219.107327583

In [ ]: