forked from sziritt/baby_birthweight_ML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Marcelo_birthweight.py
795 lines (510 loc) · 19.1 KB
/
Marcelo_birthweight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 4 17:51:20 2019
@author: Stephy Zirit
Working Directory:
/Users/Stephy Zirit/Documents/HULT/Module B/Machine Learning
Purpose:
Assigment 1
Certain factors contribute to the health of a newborn
baby. One such health measure is birth weight.
Countless studies have identified factors, both
preventative and hereditary, that lead to low birth
weight.
Your team has been hired as public health
consultants to analyze and model an infant’s birth
weight based on such characteristics.
"""
###############################################################################
##### DATA DICTIONARY
###############################################################################
"""
|----------|---------|---------------------------------|
| variable | label | description |
|----------|---------|---------------------------------|
| 1 | mage | mother's age |
| 2 | meduc | mother's educ |
| 3 | monpre | month prenatal care began |
| 4 | npvis | total number of prenatal visits |
| 5 | fage | father's age, years |
| 6 | feduc | father's educ, years |
| 7 | omaps | one minute apgar score |
| 8 | fmaps | five minute apgar score |
| 9 | cigs | avg cigarettes per day |
| 10 | drink | avg drinks per week |
| 11 | male | 1 if baby male |
| 12 | mwhte | 1 if mother white |
| 13 | mblck | 1 if mother black |
| 14 | moth | 1 if mother is other |
| 15 | fwhte | 1 if father white |
| 16 | fblck | 1 if father black |
| 17 | foth | 1 if father is other |
| 18 | bwght | birthweight, grams |
|----------|---------|---------------------------------|
"""
###############################################################################
##### LIBRARIES AND SET UP OF FILE
###############################################################################
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
file = 'birthweight_feature_set.xlsx'
birth_weight = pd.read_excel(file)
###############################################################################
##### MISSING VALUES
###############################################################################
print(birth_weight.isnull().sum())
# Flagging missing values
for col in birth_weight:
if birth_weight[col].isnull().astype(int).sum() > 0:
birth_weight['m_'+col] = birth_weight[col].isnull().astype(int)
# Filling NAs in 'npvis' , 'meduc' and 'feduc' with their MEDIANs
birth_weight.npvis = birth_weight.npvis.fillna(birth_weight.npvis.median())
birth_weight.meduc = birth_weight.meduc.fillna(birth_weight.meduc.median())
birth_weight.feduc = birth_weight.feduc.fillna(birth_weight.feduc.median())
# Rechecking NAs:
print(birth_weight.isnull().sum())
#########################################
# NEW VARIABLES - FEATURE ENGINEERING:
# Creating binary variable 'drinker'
birth_weight['drinker'] = (birth_weight.drink > 0).astype('int')
# Creating binary variable 'smoker'
birth_weight['smoker'] = (birth_weight.cigs > 0).astype('int')
birth_weight['trasher'] = birth_weight.drinker+birth_weight.smoker
birth_weight.loc[birth_weight.trasher == 2,'trasher'] = 4
#outliers:
# Creating binary variable 'out_drink' # original=12
birth_weight['out_drink'] = (birth_weight.drink > 8).astype('int')
# Creating binary variable 'out_cigs' # original - no outliers?
birth_weight['out_cigs'] = (birth_weight.cigs > 12).astype('int')
# Creating binary variable 'lo_out_npvis' # original=7
birth_weight['lo_out_npvis'] = (birth_weight.npvis < 7).astype('int')
# Creating binary variable 'hi_out_npvis' # original=15
birth_weight['hi_out_npvis'] = (birth_weight.npvis > 15).astype('int')
# Creating binary variable 'out_mage' # original=60
birth_weight['out_mage'] = (birth_weight.mage > 54).astype('int')
# INCLUDE MOTHER´S AGE > 65
# birth_weight['big_out_mage'] = (birth_weight.mage > 67).astype('int')*3 +
# Creating binary variable 'out_fage' # original=55
birth_weight['out_fage'] = (birth_weight.fage > 55).astype('int')
# Creating binary variable 'out_meduc'
# birth_weight['out_meduc'] = (birth_weight.meduc < 13).astype('int')
# Creating binary variable 'out_feduc' # original=7
birth_weight['out_feduc'] = (birth_weight.feduc < 7).astype('int')
# Creating binary variable 'out_monpre' # original=4
birth_weight['out_monpre'] = (birth_weight.monpre > 4).astype('int')
# New variable 'visgap' = diff between # prenatal visits & prenatal month of start
birth_weight['visgap'] = birth_weight.npvis-birth_weight.monpre
df = birth_weight
#####################
# Adding normalized columns for ['mage','meduc','monpre','npvis','fage','feduc','cigs','drink']
normalize = lambda var : (birth_weight[var]-min(birth_weight[var]))/(max(birth_weight[var])-min(birth_weight[var]))
standardize = lambda var : (birth_weight[var]-birth_weight[var].mean())/birth_weight[var].std()
cont_vars = ['mage','meduc','monpre','npvis','fage','feduc'] #,'cigs','drink']
for col in cont_vars:
#birth_weight['norm_'+col] = normalize(col)
#birth_weight['std_'+col] = standardize(col)
birth_weight['log_'+col] = np.log(birth_weight[col])
df = birth_weight
#########################################
#########
## K-means clusters
from sklearn.cluster import KMeans
data_for_cluster = birth_weight.drop(['omaps','fmaps','bwght'],axis=1)
kmeans = KMeans(n_clusters=3, random_state=0).fit(data_for_cluster) #orig=6
# Check clusters
# kmeans.labels_
# assign new column:
clusters = pd.get_dummies(kmeans.labels_,drop_first=False)
clusters.columns = ['group1','group2','group3']#,'group4','group5']
df = pd.concat([df,clusters],axis=1)
##########################################
# Factor Analysis
from sklearn.decomposition import FactorAnalysis
df_factor = df.drop('bwght',axis=1)
transformer = FactorAnalysis(n_components=3, random_state=0)
X_transformed = transformer.fit_transform(df_factor)
factornames = ['factor1','factor2','factor3']
factors = pd.DataFrame(X_transformed,columns=factornames)
df = pd.concat([df,factors],axis=1)
##############################################
# Classes of weights
#(REMOVE FROM MODEL TRAIN-TEST!):
# low, normal, high weight
df['wclass'] = 'norm_weight'
df.loc[df.bwght < 2500,'wclass'] = 'lo_weight'
df.loc[df.bwght > 4000,'wclass'] = 'hi_weight'
weights = pd.get_dummies(df['wclass'],drop_first=False)
df = pd.concat([df,weights],axis=1)
df = df.drop('wclass',axis=1)
###############################################################################
##### EXPLORATORY ANALYSIS
###############################################################################
# Column names
birth_weight.columns
# Displaying the first rows of the DataFrame
print(birth_weight.head())
# Dimensions of the DataFrame
birth_weight.shape
# Information about each variable
birth_weight.info()
# Descriptive statistics
birth_weight.describe().round(2)
# Normal weight (between 2500 and 4000)
nweight = birth_weight[(birth_weight.bwght <= 4000) & (birth_weight.bwght >= 2500)]
# LARGE FOR GESTATIONAL AGE (LGA)
# A.K.A. "giant babies"
hiweight = birth_weight[birth_weight.bwght > 4000]
hiweight
#230 LGAs (12.55%)
# SMALL FOR GESTATIONAL AGE (SGA)
# A.K.A. "small rats"
lowweight = birth_weight[birth_weight.bwght < 2500]
# 92 SGAs (5.02%)
# Very low weight
vlow = birth_weight[birth_weight.bwght < 1500]
print('normal')
nweight.describe()
print('hi')
hiweight.describe()
print('low')
lowweight.describe()
print('very low')
vlow.describe()
##Class of mothers with more than 14 prenatal visits:
high_risk = birth_weight[birth_weight.npvis > 14]
high_risk.describe()
plt.hist(high_risk.bwght)
plt.show()
plt.boxplot(high_risk.bwght)
plt.show()
# Tracking mothers with high risk:
high_risk = birth_weight[birth_weight.npvis > 14]
high_risk.describe()
## Variable distributions:
for col in df.columns:
x = df[col]
plt.title("Variable: "+col)
plt.hist(x)
plt.show()
for col in df.columns:
x = df[col]
plt.title("Variable: "+col)
plt.boxplot(x,vert=False)
plt.show()
## Correlation between variables:
# adding jitter to better visualize data:
def rand_jitter(arr):
stdev = .01*(max(arr)-min(arr))
return arr + np.random.randn(len(arr)) * stdev
for col in df.columns:
x = df[col]
y = df['bwght']
#print("#### x VARIABLE:",col)
#print("#### y VARIABLE: bwght")
#sns.stripplot(x,y,jitter=True)
plt.scatter(rand_jitter(x), y)
plt.xlabel(col)
plt.ylabel("bwght")
plt.axhline(2500,color='blue')
plt.axhline(4000,color='red')
plt.show()
# Correlations:
df.corr()
df.corr()['bwght'].sort_values()
# Paiwise relationship:
for col1 in range(0,len(df.columns)):
x = df.columns[col1]
for col2 in range(0,len(df.columns)):
y = df.columns[col2]
if x != 'wclass':
if y != 'wclass':
sns.lmplot(x,y,data=df,hue='wclass')
plt.show()
#########################################
# MODEL ONE - LINEAR REGRESSION
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
# Parameters X and y:
X = df.drop(['omaps', 'fmaps','bwght','omaps', 'fmaps','m_meduc','m_npvis','m_feduc','group3'],axis=1)
y = df.bwght
#################################
# Create training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state=508)
# Create the regressor: reg_all
reg_all = LinearRegression(normalize=False)
# Fit the regressor to the training data
reg_all.fit(X_train,y_train)
# Predict on the test data: y_pred
y_pred = reg_all.predict(X_test)
# Compute and print R^2 and RMSE
print("R^2: {}".format(reg_all.score(X_test, y_test)))
rmse = np.sqrt(mean_squared_error(y_test , y_pred))
print("Root Mean Squared Error: {}".format(rmse))
#########################################
# MODEL 2 - KNN
from sklearn.neighbors import KNeighborsRegressor
# Creating a regressor object
knn_reg = KNeighborsRegressor(algorithm = 'auto',
n_neighbors = 3)
# Checking the type of this new object
type(knn_reg)
# Teaching (fitting) the algorithm based on the training data
knn_reg.fit(X_train, y_train)
# Calling the score method, which compares the predicted values to the actual values
y_score = knn_reg.score(X_test, y_test)
# The score is directly comparable to R-Square
print(y_score)
###################################################
# APPROACH USING STATSMODELS - LINEAR REGRESSION
import statsmodels.formula.api as smf
# Code to help building the variables to fit:
# for x in X.columns: print("df['"+x+"'] +")
# Building a Regression Base
lm_babyweight = smf.ols(formula = """bwght ~ df['mage'] +
df['meduc'] +
df['monpre'] +
df['npvis'] +
df['fage'] +
df['feduc'] +
df['cigs'] +
df['drink'] +
df['male'] +
df['mwhte'] +
df['mblck'] +
df['moth'] +
df['fwhte'] +
df['fblck'] +
df['foth'] +
df['out_drink'] +
df['lo_out_npvis'] +
df['hi_out_npvis'] +
df['out_mage'] +
df['out_fage'] +
df['out_feduc'] +
df['out_monpre'] """,
data = df)
# Fitting Results
results = lm_babyweight.fit()
# Printing Summary Statistics
print(results.summary())
# LINEAR REGRESSION - CLEANER MODEL V.2:
lm_babyweight2 = smf.ols(formula = """bwght ~ df['mage'] +
df['cigs'] +
df['drink'] +
df['mwhte'] +
df['mblck'] +
df['moth'] +
df['fwhte'] +
df['fblck'] +
df['foth']
""",
data = df)
results2 = lm_babyweight2.fit()
# Printing Summary Statistics
print(results2.summary())
# R-squared reduced...
####################################################
# LASSO MODEL
# Import Lasso
from sklearn.linear_model import Lasso
# Instantiate a lasso regressor: lasso # original lasso w.o. normalized data 0.005
lasso = Lasso(alpha=0.855,normalize=True,max_iter=100000)
# Fit the regressor to the data
lasso.fit(X_train, y_train)
# Calling the score method, which compares the predicted values to the actual values
y_score = lasso.score(X_test, y_test)
# The score is directly comparable to R-Square
print(y_score)
# Predict on the test data: y_pred
y_pred = lasso.predict(X_test)
# Compute and print R^2 and RMSE
print("R^2: {}".format(lasso.score(X_test, y_test)))
rmse = np.sqrt(mean_squared_error(y_test , y_pred))
print("Root Mean Squared Error: {}".format(rmse))
#####
# Compute and print the coefficients
lasso_coef = lasso.coef_
# Variables + coef table:
var_coef = pd.DataFrame({'var':X.columns,'coef':lasso_coef})
print(lasso_coef)
# Plot the coefficients:
#
plt.plot(range(len(X.columns)), lasso_coef)
plt.xticks(range(len(X.columns)), X.columns.values, rotation=60)
plt.margins(0.02)
plt.show()
######################################################
# RIDGE REGRESSION MODEL
# Model finetuning (Run code in Lasso model first)
drop_vars = []
for val in var_coef.loc[var_coef.coef ==0,'var'].values: drop_vars.append(val)
# Parameters X and y:
X = X.drop(drop_vars,axis=1)
y = df.bwght
#################################
# Create training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state=508)
# Import necessary modules
from sklearn.linear_model import Ridge
from sklearn.model_selection import cross_val_score
# Create a ridge regressor: ridge
ridge = Ridge(normalize=True)
ridge.alpha = 0.75
ridge.fit(X_train, y_train)
# Calling the score method, which compares the predicted values to the actual values
y_score = ridge.score(X_test, y_test)
# The score is directly comparable to R-Square
print(y_score)
# Predict on the test data: y_pred
y_pred = ridge.predict(X_test)
# Compute and print R^2 and RMSE
print("R^2: {}".format(ridge.score(X_test, y_test)))
rmse = np.sqrt(mean_squared_error(y_test , y_pred))
print("Root Mean Squared Error: {}".format(rmse))
###################
# ALPHA TESTING
# Setup the array of alphas and lists to store scores
alpha_space = np.logspace(-4, 0, 50)
ridge_scores = []
ridge_scores_std = []
# Compute scores over range of alphas
for alpha in alpha_space:
# Specify the alpha value to use: ridge.alpha
ridge.alpha = alpha
# Perform 10-fold CV: ridge_cv_scores
ridge_cv_scores = cross_val_score(ridge, X, y, cv=10)
# Append the mean of ridge_cv_scores to ridge_scores
ridge_scores.append(np.mean(ridge_cv_scores))
# Append the std of ridge_cv_scores to ridge_scores_std
ridge_scores_std.append(np.std(ridge_cv_scores))
# Display the plot
def display_plot(cv_scores, cv_scores_std):
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(alpha_space, cv_scores)
std_error = cv_scores_std / np.sqrt(10)
ax.fill_between(alpha_space, cv_scores + std_error, cv_scores - std_error, alpha=0.2)
ax.set_ylabel('CV Score +/- Std Error')
ax.set_xlabel('Alpha')
ax.axhline(np.max(cv_scores), linestyle='--', color='.5')
ax.set_xlim([alpha_space[0], alpha_space[-1]])
ax.set_xscale('log')
plt.show()
display_plot(ridge_scores, ridge_scores_std)
##################################################
# POLYNOMIAL REGRESSION
from sklearn.preprocessing import PolynomialFeatures
from sklearn import linear_model
poly = PolynomialFeatures(degree=4)
X_ = poly.fit_transform(X)
X_test_ = poly.fit_transform(X_test)
X_train_ = poly.fit_transform(X_train)
# Instantiate
lg = LinearRegression()
# Fit
lg.fit(X_train_, y_train)
# Calling the score method, which compares the predicted values to the actual values
y_score = lg.score(X_test_, y_test)
# The score is directly comparable to R-Square
print(y_score)
###################################################
# XGBOOST
import xgboost as xgb
from sklearn.metrics import mean_squared_error
import pandas as pd
import numpy as np
xg_reg = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1,
max_depth = 5, alpha = 1, n_estimators = 10)
xg_reg.fit(X_train,y_train)
# Calling the score method, which compares the predicted values to the actual values
predicted = xg_reg.predict(X_test)
residuals = np.array(y_test) - predicted
y_test_mean = np.mean(y_test)
# Calculate total sum of squares
tss = sum((y_test - y_test_mean)**2 )
# Calculate residual sum of squares
rss = (residuals**2).sum()
# Calculate R-squared
rsq = 1 - (rss/tss)
cat('The R-square of the test data is ', round(rsq,3), '\n')
##########################################################
# SVR
from sklearn.svm import SVR
svr = SVR(kernel='linear',C=1.179)
svr.fit(X_train, y_train)
# Calling the score method, which compares the predicted values to the actual values
y_score = svr.score(X_test, y_test)
# The score is directly comparable to R-Square
print(y_score)
# Predict on the test data: y_pred
y_pred = svr.predict(X_test)
# Compute and print R^2 and RMSE
print("R^2: {}".format(svr.score(X_test, y_test)))
rmse = np.sqrt(mean_squared_error(y_test , y_pred))
print("Root Mean Squared Error: {}".format(rmse))
##########################################
# SGD
from sklearn import linear_model
clf = linear_model.SGDRegressor(max_iter=1000, tol=1e-3)
clf.fit(X_train, y_train)
# Calling the score method, which compares the predicted values to the actual values
y_score = clf.score(X_test, y_test)
# The score is directly comparable to R-Square
print(y_score)
##################################
# comparing results to evaluate model
lasso_predictions = pd.DataFrame(lasso.predict(X))
lasso_predictions.columns = ['lasso_results']
results = pd.concat([df,lasso_predictions],axis=1)
results['residuals'] = results['bwght'] - results['lasso_results']
results.to_excel('lasso_results2.xls')
#########################################
# ELASTIC NET MODEL
from sklearn.linear_model import ElasticNet
regr = ElasticNet(random_state=0)
regr.alpha = 1.9
regr.l1_ratio = 0.65
regr.fit(X_train,y_train)
# Calling the score method, which compares the predicted values to the actual values
y_score = regr.score(X_test, y_test)
# The score is directly comparable to R-Square
print(y_score)
#########
# Theil sen model
from sklearn.linear_model import TheilSenRegressor # Theil Sen Regressor Model
# Instantiate
ts_reg = TheilSenRegressor(random_state = 508)
# Fit
ts_reg.fit(X_train, y_train)
# Predict
y_pred = ts_reg.predict(X_test)
# Score
y_score_ts = ts_reg.score(X_test, y_test)
print(y_score_ts)
#############
# Regression tree
from sklearn.tree import DecisionTreeRegressor # Regression trees
# Instantiate
tree_reg = DecisionTreeRegressor(criterion = 'mse',
min_samples_leaf = 14,
random_state = 508)
# Fit
tree_reg.fit(X_train, y_train)
# Predict
y_pred = tree_reg.predict(X_test)
# Score
y_score_tree = tree_reg.score(X_test, y_test)
print(y_score_tree)
########
# Bayesian ridge
from sklearn import linear_model
bayes_reg = linear_model.BayesianRidge()
bayes_reg.fit(X_train, y_train)
# Predict
y_pred = bayes_reg.predict(X_test)
# Score
y_score_bayes_reg = bayes_reg.score(X_test, y_test)
print(y_score_bayes_reg)