In [2]:
import statsmodels.formula.api as smf #package used for linear regression
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 [4]:
df=pd.read_csv("heights_weights_genders.csv")
In [5]:
df.head()
Out[5]:
In [6]:
lm = smf.ols(formula="Weight~Height",data=df).fit() # formula regresses weight on Height (Weight~Height)
lm.params
Out[6]:
In [7]:
def predict_weight(height):
m=7.717288
b=-350.737192
y=b + m*float(height)
return y
In [12]:
df['Weight_Predictor']=df['Height'].apply(predict_weight)
In [13]:
df.head()
Out[13]:
In [ ]: