Python xgboost: kernel died

我们两清 提交于 2019-12-05 13:44:06

问题


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.


回答1:


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

import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
from xgboost import XGBClassifier


来源:https://stackoverflow.com/questions/51164771/python-xgboost-kernel-died

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!