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]:
df.plot(kind="scatter",x="Height",y="Weight")
Out[3]:
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]:
In [6]:
intercept, slope = lm.params #assign those values to variables
In [7]:
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[7]:
In [8]:
lm.summary()
Out[8]:
In [ ]: