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. To do this, find the parameters (lm.params) and use those in your function (i.e. don't generate a model each time)

In [1]:
import pandas as pd
import statsmodels.formula.api as smf

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

In [4]:
df.head()


Out[4]:
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 [5]:
lm = smf.ols(formula="Weight~Height",data=df).fit()
lm.params


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

In [8]:
def pre_weight(height):
    i = -350.737192
    h = 7.717288
    weight = i + (h * float(height))
    return weight

In [10]:
df['predict_weight'] = df['Height'].apply(pre_weight)

In [12]:
df.head()


Out[12]:
Gender Height Weight predict_weight
0 Male 73.847017 241.893563 219.161506
1 Male 68.781904 162.310473 180.072571
2 Male 74.110105 212.740856 221.191835
3 Male 71.730978 220.042470 202.831427
4 Male 69.881796 206.349801 188.560753

In [ ]: