Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python xgboost: kernel died

Tags:

python

xgboost

My Jupyter notebook's python kernel keeps dying. I have run all of the following code successfully before. 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))

When I break it apart and run line-by-line, the kernel appears to die on the xgb.train() line.

The data is small. The xgboost parameters should be conservative (i.e. num_boost_round = 2, max_depth:2, eta:1 and not computationally expensive. Not sure what is going on.

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 -- 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 line. This happened ~40-50 times in a row. I tried that many times because I was making changes to the code, thinking I would resolve the xgboost issue later.

Later on, I ran the same cells one-by-one and the xgboost completed fine on the first time I tried this and every time after. I do not know why this happens but it would be nice to know.

like image 850
user2205916 Avatar asked Jul 04 '18 01:07

user2205916


2 Answers

I was having a similar problem. This fixed it for me.

import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
from xgboost import XGBClassifier
like image 176
PandasRocks Avatar answered Nov 11 '22 00:11

PandasRocks


I had the same issue. The stupid thing is it was working fine, just suddenly decided it wants to go crazy! I tried @PandaRocks's solution. it did not work. I tried restarting stuff. even saw some stuff about a library file removal here.

finally ended up solving it by reinstalling the XGBoost using conda as explained here:

$ conda install -c conda-forge xgboost
like image 8
Omid S. Avatar answered Nov 11 '22 00:11

Omid S.