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


/usr/local/lib/python3.5/site-packages/matplotlib/__init__.py:1035: UserWarning: Duplicate key in file "/Users/mercybenzaquen/.matplotlib/matplotlibrc", line #2
  (fname, cnt))

In [5]:
import statsmodels.formula.api as smf

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

In [3]:
df.head()


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

In [8]:
lm.params


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

y = ß0 + ß1x

Here:

y: is the variable that we want to predict

ß0: is intercept of the regression line i.e. value of y when x is 0

ß1: is coefficient of x i.e. variation in y with change in value of x

x: Variables that affects value of y i.e. already know variable whose effect we want to se on values of y


In [29]:
given_height= input("What's your height")


What's your height68.781904

In [30]:
given_height


Out[30]:
'68.781904'

In [33]:
predicted_weight = -350.737192 + (7.717288 * float(given_height))

In [34]:
predicted_weight


Out[34]:
180.07257035635195

In [ ]:
def weight_calculator(my_height): 
    return (-350.737192 + (7.717288 * float(my_height)))

In [36]:
df['predicted_weight'] = df['Height'].apply(weight_calculator)

In [37]:
df


Out[37]:
Gender Height Weight predicted_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
5 Male 67.253016 152.212156 168.273699
6 Male 68.785081 183.927889 180.097090
7 Male 68.348516 167.971110 176.727987
8 Male 67.018950 175.929440 166.467344
9 Male 63.456494 156.399676 138.974848
10 Male 71.195382 186.604926 198.698077
11 Male 71.640805 213.741169 202.135534
12 Male 64.766329 167.127461 149.083223
13 Male 69.283070 189.446181 183.940213
14 Male 69.243732 186.434168 183.636632
15 Male 67.645620 172.186930 171.303537
16 Male 72.418317 196.028506 208.135814
17 Male 63.974326 172.883470 142.971104
18 Male 69.640060 185.983958 186.695207
19 Male 67.936005 182.426648 173.544523
20 Male 67.915050 174.115929 173.382810
21 Male 69.439440 197.731422 185.146964
22 Male 66.149132 149.173566 159.754710
23 Male 75.205974 228.761781 229.648966
24 Male 67.893196 162.006652 173.214157
25 Male 68.144033 192.343977 175.149935
26 Male 69.089631 184.435174 182.447392
27 Male 72.800844 206.828189 211.087884
28 Male 67.421242 175.213922 169.571952
29 Male 68.496415 154.342639 177.869372
... ... ... ... ...
9970 Female 65.618737 151.500389 155.661499
9971 Female 64.640247 155.318297 148.110213
9972 Female 60.653733 123.084293 117.345135
9973 Female 60.737031 120.926500 117.987966
9974 Female 65.393947 143.017835 153.926727
9975 Female 66.251923 124.019917 160.547979
9976 Female 61.475904 121.387236 123.690067
9977 Female 64.494838 149.402547 146.988044
9978 Female 57.375759 114.192209 92.048061
9979 Female 62.056012 125.135897 128.166924
9980 Female 60.472262 110.768229 115.944670
9981 Female 60.443264 135.559390 115.720883
9982 Female 69.868511 177.992066 188.458233
9983 Female 65.830726 132.827889 157.297478
9984 Female 59.047029 111.707369 104.945733
9985 Female 68.041065 170.514213 174.355303
9986 Female 63.352698 141.906510 138.173821
9987 Female 65.610243 151.169475 155.595951
9988 Female 59.538729 121.244876 108.740323
9989 Female 60.955084 95.686674 119.670748
9990 Female 63.179498 141.266100 136.837192
9991 Female 62.636675 102.853563 132.648068
9992 Female 62.077832 138.691680 128.335314
9993 Female 60.030434 97.687432 112.534954
9994 Female 59.098250 110.529686 105.341024
9995 Female 66.172652 136.777454 159.936222
9996 Female 67.067155 170.867906 166.839356
9997 Female 63.867992 128.475319 142.150498
9998 Female 69.034243 163.852461 182.019944
9999 Female 61.944246 113.649103 127.304393

10000 rows × 4 columns


In [ ]: