In [ ]:
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 [ ]:
df = pd.read_csv("data/heights_weights_genders.csv")
In [ ]:
df.plot(kind="scatter",x="Height",y="Weight")
In [ ]:
lm = smf.ols(formula="Weight~Height",data=df).fit() #notice the formula regresses Y on X (Y~X)
In [ ]:
lm.params #get the parameters from the model fit
In [ ]:
intercept, slope = lm.params #assign those values to variables
In [ ]:
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
In [ ]:
lm.summary() #you don't need to explain all the regression results
In [ ]: