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
In [2]:
import statsmodels.formula.api as smf
In [4]:
df = pd.read_csv("heights_weights_genders.csv")
In [5]:
df
Out[5]:
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
5
Male
67.253016
152.212156
6
Male
68.785081
183.927889
7
Male
68.348516
167.971110
8
Male
67.018950
175.929440
9
Male
63.456494
156.399676
10
Male
71.195382
186.604926
11
Male
71.640805
213.741169
12
Male
64.766329
167.127461
13
Male
69.283070
189.446181
14
Male
69.243732
186.434168
15
Male
67.645620
172.186930
16
Male
72.418317
196.028506
17
Male
63.974326
172.883470
18
Male
69.640060
185.983958
19
Male
67.936005
182.426648
20
Male
67.915050
174.115929
21
Male
69.439440
197.731422
22
Male
66.149132
149.173566
23
Male
75.205974
228.761781
24
Male
67.893196
162.006652
25
Male
68.144033
192.343977
26
Male
69.089631
184.435174
27
Male
72.800844
206.828189
28
Male
67.421242
175.213922
29
Male
68.496415
154.342639
...
...
...
...
9970
Female
65.618737
151.500389
9971
Female
64.640247
155.318297
9972
Female
60.653733
123.084293
9973
Female
60.737031
120.926500
9974
Female
65.393947
143.017835
9975
Female
66.251923
124.019917
9976
Female
61.475904
121.387236
9977
Female
64.494838
149.402547
9978
Female
57.375759
114.192209
9979
Female
62.056012
125.135897
9980
Female
60.472262
110.768229
9981
Female
60.443264
135.559390
9982
Female
69.868511
177.992066
9983
Female
65.830726
132.827889
9984
Female
59.047029
111.707369
9985
Female
68.041065
170.514213
9986
Female
63.352698
141.906510
9987
Female
65.610243
151.169475
9988
Female
59.538729
121.244876
9989
Female
60.955084
95.686674
9990
Female
63.179498
141.266100
9991
Female
62.636675
102.853563
9992
Female
62.077832
138.691680
9993
Female
60.030434
97.687432
9994
Female
59.098250
110.529686
9995
Female
66.172652
136.777454
9996
Female
67.067155
170.867906
9997
Female
63.867992
128.475319
9998
Female
69.034243
163.852461
9999
Female
61.944246
113.649103
10000 rows × 3 columns
In [18]:
df.head()
Out[18]:
Gender
Height
Weight
weight_prediction
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 [19]:
lm = smf.ols(formula="Weight~Height",data=df).fit()
In [20]:
lm.params
Out[20]:
Intercept -350.737192
Height 7.717288
dtype: float64
In [21]:
height_input = input("What is your height in inches?")
What is your height in inches?73.847017
In [22]:
height_input
Out[22]:
'73.847017'
In [23]:
weight_prediction = -350.737192 + (7.717288 * float(height_input))
weight_prediction
Out[23]:
219.16150612989594
In [26]:
def weight_estimation(individual_height):
return (-350.737192 + (7.717288 * float(individual_height)))
In [27]:
df['weight_prediction '] = df['Height'].apply(weight_estimation)
In [28]:
df
Out[28]:
Gender
Height
Weight
weight_prediction
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 [29]:
df.head()
Out[29]:
Gender
Height
Weight
weight_prediction
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 [ ]:
Content source: ledeprogram/algorithms
Similar notebooks: