NIH Seizure Prediction using Bayesian Logistic Regression and Pymc3

Code and documentation for my solution (51th place) for the Kaggle Melbourne University AES/MathWorks/NIH Seizure Prediction challenge : https://www.kaggle.com/solomonk

A 2016 Kaggle competition.

https://www.kaggle.com/c/melbourne-university-seizure-prediction

In [1]:
%reset -f
%matplotlib inline

import numpy as np
import pandas as pd
import pymc3 as pm
import seaborn as sns
import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
dirToInclude=parentdir +'/features/'
sys.path.insert(0,dirToInclude)

import IeegConsts
from IeegConsts import *
from IeegFeatures import *

import pandas
import numpy as np
import pandas as pd
from sklearn import cross_validation
from sklearn import metrics
from sklearn.metrics import roc_auc_score, log_loss, roc_auc_score, roc_curve, auc
from sklearn.cross_validation import StratifiedKFold, ShuffleSplit, cross_val_score, train_test_split
import matplotlib.pyplot as plt

import theano.tensor as tt

%matplotlib inline

np.set_printoptions(precision=4, threshold=10000, linewidth=100, edgeitems=999, suppress=True)

pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.width', 100)
pd.set_option('expand_frame_repr', False)
pd.set_option('precision', 6)
    
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 12, 4

train_dir=TRAIN_DATA_FOLDER_IN_ALL
test_dir=TEST_DATA_FOLDER_IN_ALL    


ieegFeatures= IeegFeatures(train_dir, True)
df_cols_train=ieegFeatures.ieegGenCols()
print(len(df_cols_train))
# F_NAME_TRAIN= TRAIN_FEAT_BASE + TRAIN_PREFIX_ALL +'-feat_TRAIN_df.hdf'
# X_df_train=pandas.read_hdf(F_NAME_TRAIN, engine='python')

X_df_train= pd.read_hdf(TRAIN_FEAT_BASE + TRAIN_PREFIX_ALL 
                  + 'X_df_train.hdf', 'data',format='fixed',complib='blosc',complevel=9)

# X_df_train.drop('Unnamed: 0', axis=1, inplace=True)


n=16
last_cols=list()
for i in range(1, n_psd + 1):
    last_cols.append('psd_{}'.format(i))    
for i in range(1, 16 + 1):
    last_cols.append('var_{}'.format(i))    
for i in range(1, 16 + 1):
    last_cols.append('kurt_{}'.format(i))
for i in range(1, n_corr_coeff + 1):
    last_cols.append('corcoef_{}'.format(i))
for i in range(1, n + 1):
    last_cols.append('hurst_{}'.format(i))
# for i in range(1,  n_plv+ 1):
#     last_cols.append('plv_{}'.format(i))    
# for i in range(1, n + 1):
#     last_cols.append('mean_{}'.format(i))
# for i in range(1, n + 1):
#     last_cols.append('median_{}'.format(i))
# for i in range(1, n + 1):
#     last_cols.append('std_{}'.format(i))

X_df_train_SINGLE=X_df_train


X_df_train_SINGLE.drop('id', axis=1, inplace=True)
X_df_train_SINGLE.drop('file', axis=1, inplace=True)
X_df_train_SINGLE.drop('patient_id', axis=1, inplace=True)

X_df_train_SINGLE = X_df_train_SINGLE.loc[X_df_train_SINGLE['file_size'] > 100000]
X_df_train_SINGLE.drop('file_size', axis=1, inplace=True)
X_df_train_SINGLE.drop('sequence_id', axis=1, inplace=True)
X_df_train_SINGLE.drop('segment', axis=1, inplace=True)

answers_1_SINGLE = list (X_df_train_SINGLE[singleResponseVariable].values)
X_df_train_SINGLE = X_df_train_SINGLE.drop(singleResponseVariable, axis=1)

X_df_train_SINGLE=X_df_train_SINGLE[last_cols]
X_df_train_SINGLE=X_df_train_SINGLE.apply(lambda x: pandas.to_numeric(x, errors='ignore'))
X_df_train_SINGLE.head(5)
Starting:ieegFeatures:2017-02-11 22:08:18.068192
Cols:1239
1239
Out[1]:
psd_1 psd_2 psd_3 psd_4 psd_5 psd_6 psd_7 psd_8 psd_9 psd_10 psd_11 psd_12 psd_13 psd_14 psd_15 psd_16 psd_17 psd_18 psd_19 psd_20 psd_21 psd_22 psd_23 psd_24 psd_25 psd_26 psd_27 psd_28 psd_29 psd_30 psd_31 psd_32 psd_33 psd_34 psd_35 psd_36 psd_37 psd_38 psd_39 psd_40 psd_41 psd_42 psd_43 psd_44 psd_45 psd_46 psd_47 psd_48 psd_49 psd_50 psd_51 psd_52 psd_53 psd_54 psd_55 psd_56 psd_57 psd_58 psd_59 psd_60 psd_61 psd_62 psd_63 psd_64 psd_65 psd_66 psd_67 psd_68 psd_69 psd_70 psd_71 psd_72 psd_73 psd_74 psd_75 psd_76 psd_77 psd_78 psd_79 psd_80 psd_81 psd_82 psd_83 psd_84 psd_85 psd_86 psd_87 psd_88 psd_89 psd_90 psd_91 psd_92 psd_93 psd_94 psd_95 psd_96 psd_97 psd_98 psd_99 psd_100 psd_101 psd_102 psd_103 psd_104 psd_105 psd_106 psd_107 psd_108 psd_109 psd_110 psd_111 psd_112 psd_113 psd_114 psd_115 psd_116 psd_117 psd_118 psd_119 psd_120 psd_121 psd_122 psd_123 psd_124 psd_125 psd_126 psd_127 psd_128 psd_129 psd_130 psd_131 psd_132 psd_133 psd_134 psd_135 psd_136 psd_137 psd_138 psd_139 psd_140 psd_141 psd_142 psd_143 psd_144 psd_145 psd_146 psd_147 psd_148 psd_149 psd_150 psd_151 psd_152 psd_153 psd_154 psd_155 psd_156 psd_157 psd_158 psd_159 psd_160 psd_161 psd_162 psd_163 psd_164 psd_165 psd_166 psd_167 psd_168 psd_169 psd_170 psd_171 psd_172 psd_173 psd_174 psd_175 psd_176 psd_177 psd_178 psd_179 psd_180 psd_181 psd_182 psd_183 psd_184 psd_185 psd_186 psd_187 psd_188 psd_189 psd_190 psd_191 psd_192 var_1 var_2 var_3 var_4 var_5 var_6 var_7 var_8 var_9 var_10 var_11 var_12 var_13 var_14 var_15 var_16 kurt_1 kurt_2 kurt_3 kurt_4 kurt_5 kurt_6 kurt_7 kurt_8 kurt_9 kurt_10 kurt_11 kurt_12 kurt_13 kurt_14 kurt_15 kurt_16 corcoef_1 corcoef_2 corcoef_3 corcoef_4 corcoef_5 corcoef_6 corcoef_7 corcoef_8 corcoef_9 corcoef_10 corcoef_11 corcoef_12 corcoef_13 corcoef_14 corcoef_15 corcoef_16 corcoef_17 corcoef_18 corcoef_19 corcoef_20 corcoef_21 corcoef_22 corcoef_23 corcoef_24 corcoef_25 corcoef_26 corcoef_27 corcoef_28 corcoef_29 corcoef_30 corcoef_31 corcoef_32 corcoef_33 corcoef_34 corcoef_35 corcoef_36 corcoef_37 corcoef_38 corcoef_39 corcoef_40 corcoef_41 corcoef_42 corcoef_43 corcoef_44 corcoef_45 corcoef_46 corcoef_47 corcoef_48 corcoef_49 corcoef_50 corcoef_51 corcoef_52 corcoef_53 corcoef_54 corcoef_55 corcoef_56 corcoef_57 corcoef_58 corcoef_59 corcoef_60 corcoef_61 corcoef_62 corcoef_63 corcoef_64 corcoef_65 corcoef_66 corcoef_67 corcoef_68 corcoef_69 corcoef_70 corcoef_71 corcoef_72 corcoef_73 corcoef_74 corcoef_75 corcoef_76 corcoef_77 corcoef_78 corcoef_79 corcoef_80 corcoef_81 corcoef_82 corcoef_83 corcoef_84 corcoef_85 corcoef_86 corcoef_87 corcoef_88 corcoef_89 corcoef_90 corcoef_91 corcoef_92 corcoef_93 corcoef_94 corcoef_95 corcoef_96 corcoef_97 corcoef_98 corcoef_99 corcoef_100 corcoef_101 corcoef_102 corcoef_103 corcoef_104 corcoef_105 corcoef_106 corcoef_107 corcoef_108 corcoef_109 corcoef_110 corcoef_111 corcoef_112 corcoef_113 corcoef_114 corcoef_115 corcoef_116 corcoef_117 corcoef_118 corcoef_119 corcoef_120 hurst_1 hurst_2 hurst_3 hurst_4 hurst_5 hurst_6 hurst_7 hurst_8 hurst_9 hurst_10 hurst_11 hurst_12 hurst_13 hurst_14 hurst_15 hurst_16
0 -0.406786 -0.506428 -0.550631 -0.602230 -0.117925 -0.258156 -0.292530 -0.310629 -0.464966 -2.681048 -0.672553 -0.735941 -0.558759 -0.644890 -0.688039 -0.669862 -0.510144 -0.598658 -0.646677 -0.670835 -0.129998 -0.251415 -0.323000 -0.227784 -0.639318 -2.760449 -0.665167 -0.659974 -0.590483 -0.588708 -0.647818 -0.715215 -0.629282 -0.647044 -0.621452 -0.675702 -0.426132 -0.442531 -0.483979 -0.434602 -0.750417 -2.766089 -0.708479 -0.729438 -0.629134 -0.464072 -0.620423 -0.708196 -0.707625 -0.711356 -0.642788 -0.672846 -0.478605 -0.479591 -0.520513 -0.456928 -0.790443 -2.769022 -0.732106 -0.730806 -0.646176 -0.473156 -0.634019 -0.703660 -0.763965 -0.765547 -0.680584 -0.695172 -0.550166 -0.547159 -0.539212 -0.476176 -0.818632 -2.770506 -0.785050 -0.764124 -0.638319 -0.486804 -0.654367 -0.688834 -0.905803 -0.866233 -0.841806 -0.849490 -0.760451 -0.696508 -0.704839 -0.673899 -0.937149 -2.788111 -0.922047 -0.953263 -0.767587 -0.691095 -0.780723 -0.759457 -1.181694 -1.112317 -1.080482 -1.044585 -0.970119 -0.848156 -0.901840 -0.927819 -1.146751 -2.809118 -1.091821 -1.153706 -0.992040 -0.950499 -0.943695 -0.874682 -1.422331 -1.389611 -1.338669 -1.274514 -1.122098 -1.014736 -1.053481 -1.121061 -1.423202 -2.849218 -1.344742 -1.423175 -1.194762 -1.215790 -1.194936 -1.162727 0.615413 0.465645 0.394319 0.333944 0.734827 0.507590 0.499734 0.471931 0.564459 0.117440 0.326137 0.306081 0.306713 0.156820 0.166570 0.143397 0.512056 0.373415 0.298273 0.265338 0.722755 0.514332 0.469264 0.554776 0.390108 0.038039 0.333523 0.382048 0.274988 0.213003 0.206791 0.098044 0.330776 0.269807 0.294938 0.250845 0.369017 0.274044 0.281546 0.327668 0.246239 0.030195 0.253611 0.295587 0.231769 0.326421 0.217546 0.114852 -0.181280 -0.103608 -0.003335 -0.014493 -0.353738 -0.240287 -0.187718 -0.227108 -0.143868 -0.007843 -0.079912 -0.086461 -0.043219 0.113418 0.010754 0.016808 1330.445710 1053.480839 1012.823856 882.276599 5324.886292 3438.669283 2822.905996 3031.208239 949.112785 0.365631 707.979865 596.306308 1072.018943 1306.998679 937.363732 935.186251 2.666569 6.968982 3.161490 1.738240 1.444126 18.737641 33.201227 40.104324 3.765897 30.769902 1.565525 1.709295 2.556811 3.083199 1.464667 2.148079 0.178701 0.032578 -0.023397 -0.152288 -0.219400 -0.201431 -0.190916 0.204805 0.006099 0.081204 0.049197 -0.009776 -0.070802 -0.063187 -0.074768 0.220537 0.081216 -0.179154 -0.259667 -0.271336 -0.228816 0.135671 0.116379 0.205116 0.137675 -0.019435 -0.069349 -0.026827 -0.071847 0.210955 -0.159451 -0.245452 -0.239983 -0.167325 0.007277 0.011734 0.167109 0.104715 -0.002921 -0.062695 -0.046157 -0.062296 -0.169828 -0.228330 -0.229710 -0.120309 -0.001547 0.064140 0.139054 0.209108 0.004949 -0.047816 -0.013022 -0.013767 -0.016553 -0.148324 -0.176761 -0.166190 -0.022829 -0.180159 -0.186908 -0.120251 -0.150716 -0.152404 -0.142461 0.354104 0.136804 -0.234881 -0.070225 -0.273721 -0.272838 -0.240637 -0.232917 -0.275701 -0.222007 0.337664 -0.211292 -0.057565 -0.260903 -0.268919 -0.218403 -0.237076 -0.278634 -0.220479 -0.198198 -0.036452 -0.208113 -0.195315 -0.202388 -0.227254 -0.264204 -0.178708 -0.049883 0.126748 0.116390 0.055399 -0.036598 -0.025073 -0.024066 -0.049266 0.031814 0.016276 0.028395 0.088016 0.058297 0.276228 0.031344 -0.044703 -0.039266 0.000437 0.044788 -0.014861 0.011477 0.072533 0.106350 0.118652 0.088373 0.493077 0.194405 0.354023 0.474543 0.344651 0.312167 0.568701 0.411647 0.388849 0.264564 0.256256 0.495332 0.611394 0.347852 0.331746 0.501788 0.473050 0.391432 0.359969
1 -0.428051 -0.566576 -0.635108 -0.672799 -0.256426 -0.372712 -0.317646 -0.404852 -0.479488 -2.690905 -0.720872 -0.778956 -0.560340 -0.700978 -0.691731 -0.715577 -0.417979 -0.647573 -0.697937 -0.670199 -0.292538 -0.322548 -0.347250 -0.277774 -0.667934 -2.740056 -0.661574 -0.690016 -0.623905 -0.589920 -0.679789 -0.771444 -0.612797 -0.640917 -0.636664 -0.640541 -0.502826 -0.474716 -0.480880 -0.469672 -0.725687 -2.745536 -0.664675 -0.692026 -0.589230 -0.488217 -0.596600 -0.682200 -0.709069 -0.710412 -0.686045 -0.677144 -0.565640 -0.513876 -0.526703 -0.516816 -0.774404 -2.758830 -0.715076 -0.711834 -0.596229 -0.430723 -0.572233 -0.684438 -0.776716 -0.806387 -0.763084 -0.727851 -0.639602 -0.570051 -0.592727 -0.567083 -0.814626 -2.772359 -0.767065 -0.751058 -0.622363 -0.450963 -0.600960 -0.682608 -0.919301 -0.930214 -0.913473 -0.894954 -0.852568 -0.710257 -0.763854 -0.761351 -0.947070 -2.782325 -0.932717 -0.959682 -0.763956 -0.695249 -0.787622 -0.767490 -1.227845 -1.222355 -1.169036 -1.106684 -1.059154 -0.901919 -0.944014 -0.952874 -1.192869 -2.814676 -1.129484 -1.171308 -0.982528 -0.906194 -0.977814 -0.920432 -1.466409 -1.442010 -1.393084 -1.318627 -1.200591 -1.060743 -1.056533 -1.169781 -1.446816 -2.839578 -1.372801 -1.427087 -1.210371 -1.217941 -1.219325 -1.220321 0.618679 0.485593 0.387613 0.315243 0.687265 0.422889 0.527013 0.441787 0.573319 0.107294 0.299179 0.273774 0.299294 0.087059 0.180658 0.121686 0.628751 0.404596 0.324784 0.317843 0.651154 0.473052 0.497408 0.568865 0.384873 0.058143 0.358477 0.362714 0.235729 0.198118 0.192600 0.065819 0.359662 0.336351 0.327431 0.306036 0.377840 0.275827 0.311445 0.330987 0.284924 0.039459 0.307191 0.332190 0.254153 0.318847 0.273614 0.154859 -0.269089 -0.068246 0.002647 -0.011806 -0.273313 -0.197225 -0.185963 -0.237878 -0.099950 -0.018684 -0.051286 -0.030524 0.018424 0.120729 0.081014 0.089040 1452.257995 899.164212 823.909800 802.319827 3459.216856 2657.810182 2670.563703 2588.839317 919.439123 0.347581 731.671687 661.900286 1423.068986 1568.033791 1143.074468 879.163979 3.047194 7.240983 4.372952 2.604562 2.639642 21.375989 16.903039 19.801985 4.652647 35.961740 2.177266 2.657048 4.372113 3.978509 1.861551 1.916273 0.158984 0.006843 -0.020018 -0.114797 -0.187727 -0.183429 -0.153786 0.175157 0.010284 0.052897 -0.014611 -0.064157 -0.117036 -0.133033 -0.121117 0.199643 0.116175 -0.163561 -0.226921 -0.222678 -0.194723 0.107515 0.126857 0.184516 0.111269 -0.061203 -0.125231 -0.091346 -0.106738 0.258751 -0.143910 -0.213731 -0.205557 -0.162809 0.005357 0.008838 0.167182 0.112151 -0.043900 -0.113255 -0.071328 -0.098273 -0.141045 -0.211931 -0.213553 -0.140393 -0.014006 0.060842 0.151790 0.248345 -0.035642 -0.116842 -0.094239 -0.068171 0.047842 -0.159501 -0.132093 -0.119789 -0.039888 -0.155188 -0.133624 -0.096723 -0.129859 -0.148303 -0.128713 0.266662 0.050960 -0.196714 -0.059094 -0.228505 -0.223246 -0.177460 -0.188110 -0.210657 -0.183980 0.248465 -0.184424 -0.047883 -0.198102 -0.205756 -0.154796 -0.195820 -0.221138 -0.193138 -0.157629 -0.034454 -0.178085 -0.166659 -0.135418 -0.194642 -0.207960 -0.151013 -0.056442 0.073630 0.055497 -0.007204 -0.085205 -0.104699 -0.048605 -0.043361 0.032085 0.024517 0.010900 0.068934 0.041464 0.238677 -0.037642 -0.121946 -0.120850 -0.050691 -0.025828 -0.101934 -0.091659 -0.011054 -0.006813 -0.002555 0.026854 0.464664 0.188227 0.363075 0.502679 0.401603 0.414200 0.488810 0.622529 0.485241 0.571458 0.406541 0.615575 0.687594 0.409496 0.531784 0.434593 0.765483 0.494393 0.491168
2 -0.438473 -0.388276 -0.560804 -0.462901 -0.261924 -0.412540 -0.530027 -0.607979 -0.674493 -0.713967 -0.710172 -0.723321 -0.598466 -0.773537 -0.741306 -0.744617 -0.648960 -0.595146 -0.648578 -0.649621 -0.392382 -0.425151 -0.550321 -0.508957 -0.794122 -0.822105 -0.802749 -0.803596 -0.627584 -0.527127 -0.652850 -0.811482 -0.559530 -0.478087 -0.490636 -0.533479 -0.510440 -0.583750 -0.647979 -0.686542 -0.791141 -0.825084 -0.751450 -0.687138 -0.624905 -0.485618 -0.654331 -0.784637 -0.654161 -0.568703 -0.505873 -0.532767 -0.559091 -0.647034 -0.714812 -0.753031 -0.823205 -0.820382 -0.738886 -0.652164 -0.659866 -0.503283 -0.688057 -0.788743 -0.785134 -0.680696 -0.549041 -0.554762 -0.613011 -0.687526 -0.759987 -0.774439 -0.868717 -0.823551 -0.744845 -0.680407 -0.620970 -0.451936 -0.653109 -0.771946 -0.877637 -0.784635 -0.765896 -0.777874 -0.786298 -0.849431 -0.915044 -0.947412 -0.949799 -0.922733 -0.860079 -0.859587 -0.610814 -0.514078 -0.701109 -0.777309 -1.178028 -1.095816 -1.054415 -1.014244 -0.986959 -0.985453 -1.088277 -1.190281 -1.230308 -1.236988 -1.154182 -1.145834 -0.998370 -0.907247 -1.004930 -0.979935 -1.428880 -1.364952 -1.297947 -1.237569 -1.054132 -1.113059 -1.231489 -1.363413 -1.510361 -1.500478 -1.358628 -1.392567 -1.255594 -1.165068 -1.221994 -1.208271 0.563889 0.524655 0.325820 0.417272 0.613218 0.499598 0.463052 0.444107 0.393296 0.338068 0.272525 0.256221 0.164258 -0.105926 0.085670 0.122293 0.353402 0.317786 0.238046 0.230551 0.482760 0.486987 0.442759 0.543129 0.273668 0.229931 0.179948 0.175946 0.135140 0.140484 0.174126 0.055428 0.344518 0.345249 0.367767 0.336182 0.316437 0.279592 0.292698 0.323815 0.239590 0.227718 0.234561 0.295782 0.139791 0.199161 0.173257 0.088665 -0.008884 0.027464 0.129721 0.105631 -0.166323 -0.207395 -0.150061 -0.219314 -0.034078 -0.002212 0.054614 0.119837 0.004651 0.058676 -0.000870 0.033237 2656.218991 1535.532773 1214.674003 1946.472638 2661.030179 1732.844809 1066.889208 944.611286 583.431453 584.678465 725.139566 639.098452 1373.657623 1883.493652 1005.484401 742.213566 0.749912 7.486165 4.812114 1.285146 1.231140 1.518840 3.409387 2.827718 1.183922 2.380808 1.954354 2.532612 1.832488 3.483692 0.901819 1.961327 -0.029160 -0.061747 0.192742 -0.160926 -0.184992 -0.210974 -0.170999 0.025638 -0.085231 -0.148064 -0.124346 -0.094917 -0.140349 -0.138500 -0.221177 0.112207 -0.040638 -0.157796 -0.182545 -0.193836 -0.146527 0.006995 0.032771 0.040653 0.003278 -0.069760 -0.129927 -0.113316 -0.130260 0.138613 -0.159326 -0.173303 -0.164666 -0.097393 -0.094210 -0.000567 0.048494 0.012564 -0.073928 -0.149313 -0.140981 -0.147593 -0.185277 -0.184865 -0.220040 -0.124713 -0.162265 -0.127757 -0.102602 0.031571 -0.103355 -0.147154 -0.119620 -0.221497 0.144652 0.033266 -0.054917 -0.135020 -0.158212 -0.138838 -0.159000 -0.072380 -0.100799 -0.133940 -0.097326 0.390705 0.037903 -0.146170 -0.167650 -0.160586 -0.154301 -0.103053 -0.120547 -0.140514 -0.103375 0.286846 -0.082819 -0.128839 -0.101643 -0.114714 -0.064995 -0.130834 -0.150770 -0.070582 -0.038292 -0.076110 -0.050995 -0.027173 -0.020764 -0.123348 -0.118128 -0.025818 0.279122 0.071281 0.072518 0.022651 -0.117495 -0.123846 -0.025361 0.341524 0.142416 -0.019938 -0.140631 -0.158554 -0.051268 0.197225 -0.030536 -0.145669 -0.177705 -0.043935 -0.011806 -0.141013 -0.146454 -0.034084 -0.167929 -0.096414 -0.004234 0.441489 0.178188 0.407435 0.236761 0.419352 0.262515 0.232381 0.196593 0.279796 0.205667 0.184826 0.258989 0.246480 0.192713 0.360385 0.268366 0.271937 0.228565 0.213197
3 -0.111201 -0.041987 -0.313406 -0.228682 -0.245675 -0.030617 -0.180695 -0.312091 -0.233526 0.009591 -0.279750 -0.301803 -0.203881 -0.209715 -0.205457 -0.254544 -0.330172 -0.221672 -0.476177 -0.453048 -0.550985 -0.417036 -0.528471 -0.600747 -0.413666 -0.398714 -0.542718 -0.581673 -0.472075 -0.445944 -0.471322 -0.551943 -0.662568 -0.692448 -0.868319 -0.821175 -0.896454 -0.761788 -0.864188 -0.902398 -0.738648 -0.756345 -0.805154 -0.824320 -0.664092 -0.679323 -0.750961 -0.830958 -0.697661 -0.752281 -0.928897 -0.873393 -0.946171 -0.814563 -0.931151 -0.953419 -0.780086 -0.816081 -0.868454 -0.895429 -0.729425 -0.741979 -0.815945 -0.901900 -0.733869 -0.810772 -0.962738 -0.927516 -1.014153 -0.868144 -0.992215 -0.999176 -0.828851 -0.885969 -0.915254 -0.965826 -0.822768 -0.819871 -0.876483 -0.970698 -0.972177 -1.062594 -1.156289 -1.116091 -1.288333 -1.179984 -1.264226 -1.247310 -1.083451 -1.085929 -1.178520 -1.229726 -1.104440 -1.064691 -1.102844 -1.237834 -1.339655 -1.480199 -1.520626 -1.407563 -1.617144 -1.543949 -1.609962 -1.547593 -1.381859 -1.360007 -1.522461 -1.622398 -1.390270 -1.358110 -1.394830 -1.581343 -1.565151 -1.734503 -1.791662 -1.650791 -1.810555 -1.802295 -1.858161 -1.760692 -1.490321 -1.535764 -1.744210 -1.874748 -1.571948 -1.623168 -1.601572 -1.777521 1.006954 1.181037 0.987916 1.009138 1.176661 1.294287 1.222866 1.059909 0.973988 1.211287 1.037546 1.081314 1.020372 0.977363 1.019290 1.121932 0.787984 1.001352 0.825146 0.784772 0.871351 0.907869 0.875091 0.771252 0.793848 0.802983 0.774579 0.801444 0.752178 0.741134 0.753425 0.824533 0.421398 0.475432 0.388355 0.366721 0.471007 0.513187 0.480061 0.423903 0.426102 0.385358 0.460572 0.493782 0.488029 0.443142 0.415544 0.481244 -0.366585 -0.525920 -0.436791 -0.418051 -0.400344 -0.394682 -0.395030 -0.347350 -0.367746 -0.417625 -0.314007 -0.307662 -0.264148 -0.297992 -0.337881 -0.343289 3788.115611 5446.828809 1691.364780 2308.655231 1710.880902 4515.610336 2227.635213 1331.450809 2275.862726 5136.964324 1586.126659 1376.815749 2241.370570 2339.214348 2251.594604 1757.943299 2.040104 2.225392 0.739700 1.147698 1.072190 2.500653 0.793764 0.409903 0.840943 0.632733 0.553273 0.550425 1.570896 0.646849 0.687921 0.347896 0.642901 0.274792 -0.066169 -0.233990 -0.180758 -0.177053 -0.112920 -0.219090 -0.108098 -0.101637 -0.229437 -0.227107 -0.255041 -0.275551 -0.379040 0.510787 0.016690 -0.255037 -0.158896 -0.062680 -0.035647 -0.296388 -0.283800 -0.070270 -0.272400 -0.276680 -0.331240 -0.361764 -0.382152 0.062699 -0.176201 -0.132981 0.018451 0.205976 -0.257594 -0.189507 -0.105161 -0.189763 -0.243678 -0.291848 -0.318517 -0.311132 -0.156172 -0.054545 0.038934 0.077202 -0.246424 -0.152501 -0.072676 0.078642 -0.233161 -0.176286 -0.099320 0.110287 0.631025 0.419345 -0.078530 -0.174900 -0.115546 -0.258702 -0.215993 -0.184341 -0.108651 -0.054307 -0.029026 0.545307 -0.039585 -0.259957 -0.179956 -0.283438 -0.225450 -0.314569 -0.247017 -0.201914 -0.125843 0.273577 -0.276813 -0.282466 -0.278067 -0.230892 -0.315863 -0.279530 -0.253046 -0.112160 -0.188416 -0.163674 -0.048980 0.062526 -0.156347 -0.209395 -0.182854 0.011055 0.427245 0.177390 0.088372 0.268092 0.100422 -0.017090 -0.070929 -0.021371 0.031837 -0.091824 -0.026530 0.056613 -0.251510 0.477342 0.044892 -0.102662 -0.161943 0.237846 0.030722 -0.154910 -0.114277 0.472173 0.476516 0.306272 0.260913 0.709682 0.116640 0.170123 0.335260 0.291863 0.326536 0.257931 0.469382 0.175357 0.220069 0.398464 0.263109 0.274553 0.245795 0.289799 0.272079 0.321547 0.246082 0.324690
4 -0.770331 -0.584485 -0.502479 -0.454269 -0.553190 -0.636482 -0.795933 -0.820530 -0.787236 -0.671795 -0.796073 -0.793696 -0.679834 -0.628853 -0.670575 -0.794874 -0.928855 -0.881467 -0.786080 -0.976289 -0.934422 -1.029379 -1.072448 -1.145465 -1.016527 -0.975320 -1.063246 -1.115494 -0.958634 -0.902233 -0.986790 -1.188340 -1.047846 -1.020852 -1.043965 -1.165375 -1.101895 -1.233633 -1.199967 -1.297162 -1.037072 -1.144614 -1.268083 -1.374482 -1.146901 -1.116260 -1.229882 -1.395987 -1.084449 -1.059271 -1.090673 -1.209574 -1.136210 -1.260735 -1.212954 -1.316720 -1.116572 -1.194359 -1.319347 -1.425663 -1.177851 -1.173634 -1.270851 -1.439431 -1.101896 -1.073788 -1.113964 -1.211854 -1.131726 -1.244887 -1.215078 -1.339792 -1.171134 -1.210906 -1.361289 -1.462588 -1.211765 -1.203163 -1.298599 -1.474930 -0.923572 -1.080045 -1.095923 -1.285582 -1.134799 -1.274666 -1.171211 -1.363859 -1.139129 -1.235459 -1.429862 -1.517777 -1.294699 -1.278452 -1.367044 -1.531927 -0.899367 -1.100151 -0.985105 -1.557288 -1.298830 -1.503771 -1.473174 -1.561458 -1.428877 -1.340960 -1.552326 -1.626884 -1.371516 -1.371110 -1.431025 -1.634748 -1.281053 -1.315199 -1.357246 -1.756855 -1.486056 -1.726523 -1.678698 -1.748864 -1.621325 -1.442570 -1.695904 -1.850529 -1.549643 -1.467518 -1.627333 -1.829001 0.140970 0.505496 0.534510 0.946256 0.655926 0.737801 0.500527 0.630986 0.473039 0.613219 0.690718 0.775217 0.651577 0.693462 0.727282 0.785428 -0.017554 0.208515 0.250909 0.424236 0.274694 0.344904 0.224013 0.306051 0.243748 0.309694 0.423545 0.453419 0.372777 0.420082 0.411067 0.391962 -0.162730 0.043468 -0.040567 0.212531 0.092561 0.135059 0.089004 0.133562 0.161324 0.108517 0.174601 0.152609 0.153288 0.164773 0.134974 0.146634 -0.145176 -0.165047 -0.291476 -0.211705 -0.182133 -0.209845 -0.135009 -0.172489 -0.082424 -0.201176 -0.248945 -0.300811 -0.219489 -0.255309 -0.276093 -0.245328 511.591893 717.759756 1305.924328 2609.476978 470.554463 300.968428 200.910476 164.800668 232.670311 353.349813 185.055518 163.490757 327.573035 412.032264 270.711882 147.534822 1.996694 0.228024 0.450064 -0.254879 0.542542 1.087637 0.792145 1.881766 1.114670 0.304191 1.542368 0.757896 1.059704 0.859168 2.562184 0.349247 0.030753 -0.024299 -0.145883 -0.127290 -0.178721 -0.186140 -0.145188 0.021860 0.015256 -0.026582 -0.087564 -0.020789 -0.033702 -0.080075 -0.124249 -0.320325 0.248216 -0.056074 -0.059749 -0.080378 -0.013758 0.015854 -0.313705 -0.134662 -0.250060 -0.123679 -0.293216 -0.207075 -0.225572 -0.473004 -0.000651 -0.194882 -0.098219 -0.236044 -0.178006 0.137542 -0.019509 0.158804 -0.074843 0.072406 0.119957 0.102874 -0.164157 0.035398 -0.130333 0.026446 -0.028035 -0.372921 -0.191441 -0.358372 -0.231236 -0.294606 -0.297595 -0.263418 0.393418 0.083894 -0.097702 -0.102853 -0.121281 -0.099799 -0.083224 -0.132804 -0.160676 -0.118069 -0.146844 0.321721 0.107047 -0.091751 -0.202066 -0.117705 -0.134136 -0.173693 -0.222429 -0.202671 -0.117897 0.502476 -0.154050 -0.136416 -0.045410 0.040682 -0.139774 -0.169549 -0.097835 0.093651 -0.101272 -0.115679 -0.014920 0.094516 -0.141819 -0.142176 -0.142847 0.135922 0.136635 0.067097 -0.102046 0.079685 -0.058121 -0.077385 -0.178405 0.142252 0.097217 0.088642 0.179863 0.101274 0.058654 -0.056705 0.032192 0.021425 0.084679 0.061646 0.076973 0.121898 0.093407 0.485742 0.242453 0.087940 -0.063777 0.188335 0.040613 0.212929 0.435515 0.292176 0.311181 0.236584 0.269141 0.365435 0.430709 0.268305 0.350697 0.300376 0.470037 0.158063 0.384128 0.254672 0.270050 0.276992
In [2]:
y=answers_1_SINGLE
X=X_df_train_SINGLE

# Normalize variables
X_norm = (X - X.mean())/X.std()

lr_best_params = {'penalty': 'l2', 'C': 100, 'solver': 'newton-cg', 'fit_intercept': False}
lr = LogisticRegression(**lr_best_params)
lr.fit(X_norm, y)
#Store LR coeefs
lr_coeefs=lr.coef_        

k = (X_df_train_SINGLE.shape[1])
In [3]:
with pm.Model() as logistic_model:        
    μ = pm.Normal('μ', 0, sd=10)
    b = pm.Laplace('b', 0.0, b=0.1, shape=k)    
    p = pm.math.invlogit(μ + tt.dot(X_norm, b))    
    likelihood = pm.Bernoulli('likelihood', p, observed=y)
In [4]:
niter=3000
with logistic_model:
    trace_logistic_model = pm.sample(niter, n_init=50000)
Auto-assigning NUTS sampler...
Initializing NUTS using advi...
Average ELBO = -8,546.2: 100%|██████████| 50000/50000 [02:06<00:00, 396.60it/s]
Finished [100%]: Average ELBO = -7,615.1
100%|██████████| 3000/3000 [1:00:51<00:00,  1.08s/it]  
In [5]:
ax = pm.traceplot(trace_logistic_model[-1000:], figsize=(12,len(trace_logistic_model.varnames)*1.5),  
    lines={k: v['mean'] for k, v in pm.df_summary(trace_logistic_model[-1000:]).iterrows()})