# Python XGBoost killing kernel

My Jupyter notebook's python kernel keeps dying when attempting to train an XGBoost logistic classifier. Previously, I have run all of the following code successfully. Presently, there are issues. First, I will show you the code chunk that I am able to run successfully:

import xgboost as xgb
xgtrain = xgb.DMatrix(data = X_train_sub.values, label = Y_train.values)       # create dense matrix of training values
xgtest  = xgb.DMatrix(data = X_test_sub.values,  label = Y_test.values)        # create dense matrix of test values
param   = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic'}  # specify parameters via map


where my data is small:

X_train_imp_sub.shape
(1365, 18)


however, my notebook's kernel keeps dying on this chunk:

xgmodel = xgb.train(param,  xgtrain, num_boost_round = 2)                      # train the model
predictions = xgmodel.predict(xgtest)                                          # make prediction
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_true = Y_test,
y_pred = predictions.round(),
normalize = True) # If False, return # of correctly classified samples. Else, return fraction of correctly classified samples
print("Accuracy: %.2f%%" % (accuracy * 100.0))


As stated before, I have been able to run both chunks successfully before. I have shut down all other notebooks and restarted my computer without luck. I am launching jupyter through Anaconda Navigator on a Macbook Pro.

UPDATE:

To further debug, I used Terminal CLI and ran:

1. conda install runipy
2. runipy MyNotebook.ipynb

which was very helpful, because it produced the following error message that I could then Google:

OMP: Error #15: Initializing libomp.dylib, but found libiomp5.dylib already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://openmp.llvm.org/


I found this thread:

https://github.com/dmlc/xgboost/issues/1715

Which shows others having the same problem on MacOS. I also suspected the problem being linked to matplotlib somehow because running any cell with matplotlib related code somehow causes the xgb.train to kill the kernel.

• What is the shape of xgtrain? It might be possible that python is running out of memory. – Ankit Seth Jul 4 '18 at 5:29
• What are your machine specs... It's actually weird as you can't run out of memory with such a small dataset... I guess the error is somewhere else.. You have tried restarting the kernel also, so another option to try it out is run it in raw python as a script and report back... – Aditya Jul 4 '18 at 9:13
• @AnkitSeth not sure how to find the shape of an xgb.DMatrix. – user2205916 Jul 4 '18 at 11:25
• @Aditya it's a new Macbook Pro so should be pretty powerful. Yes, I have restarted the kernel probably a few dozen times trying to fix the problem. – user2205916 Jul 4 '18 at 11:26
• You can try downgrading the version of xgboost to 0.80 – Zoom Deres Feb 4 '19 at 9:20

When I selected a cell beneath my xgboost training cell, then selected: Cells --> Run All Above, the kernel would always die on the xgboost training cell. This happened probably 40-50 times in a row.

When I ran the same cells one-by-one, it completed fine on the first time I tried this and every time after.

• Now this is really weird, try running your whole code in a .py file directly via  python filename.py – Aditya Jul 4 '18 at 14:54
• @Aditya yeah. good idea. i updated my original post w/ more info and what i found as a result of your suggestion. – user2205916 Jul 4 '18 at 23:52
• Pardon me I ain't a MAC user but It seems your env has some issues with openmp... – Aditya Jul 5 '18 at 2:34

The best solution I found was here.

Check your anaconda/lib folder, ll lib*omp*, do you see some old libiomp5.dylib file? Remove it.

On mac, you can get the latest libomp by running brew install libomp.