Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

module 'xgboost' has no attribute 'DMatrix'

I pulled some ML code that ran on kaggle (linux) and tried to run it in a jupyter notebook on a windows machine. Here is the code (some of it):

##### RUN XGBOOST
import xgboost as xgb

print("\nSetting up data for XGBoost ...")
# xgboost params
xgb_params = {
    'eta': 0.037,
    'max_depth': 5,
    'subsample': 0.80,
    'objective': 'reg:linear',
    'eval_metric': 'mae',
    'lambda': 0.8,   
    'alpha': 0.4, 
    'base_score': y_mean,
    'silent': 1
}

#### These lines were causing the folloing error on 9/1/2017:
# AttributeError: module 'xgboost' has no attribute 'DMatrix'
dtrain = xgb.DMatrix(x_train.values, y_train.values)
dtest = xgb.DMatrix(x_test)

num_boost_rounds = 250
print("num_boost_rounds="+str(num_boost_rounds))

# train model
print( "\nTraining XGBoost ...")
model = xgb.train(dict(xgb_params, silent=1), dtrain, 
num_boost_round=num_boost_rounds)

print( "\nPredicting with XGBoost ...")
xgb_pred1 = model.predict(dtest)

print( "\nFirst XGBoost predictions:" )
print(pd.DataFrame(xgb_pred1).head())

Received the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-a63b74bc35c6> in <module>()
     17 #### These lines were causing the folloing error on 9/1/2017:
     18 # AttributeError: module 'xgboost' has no attribute 'DMatrix'
---> 19 dtrain = xgb.DMatrix(x_train.values, y_train.values)
     20 dtest = xgb.DMatrix(x_test)
     21 

AttributeError: module 'xgboost' has no attribute 'DMatrix'

This is odd because I pull xgboost models from linux machines to windows all the time. I can not find any info on how to fix on the internet, so I am wondering if anyone knows how to fix?

like image 344
Jeff Saltfist Avatar asked Mar 08 '23 04:03

Jeff Saltfist


2 Answers

We probably have the same problem.

I solved it by telling Python explicitly where to find xgboost library.

The reason is that I have more than one scripts with the name xgboost.py. Python might have imported one of them mistakenly, so that it cannot find the definition of 'DMatrix'.

Here is the command I used:

export PYTHONPATH=~/xgboost/python-package

You should change '~/xgboost/python-package' into the folder where your /xgboost/python-package/setup.py file located.

like image 156
bitit1994 Avatar answered Mar 15 '23 12:03

bitit1994


Intrestingly but it is really easy;

import xgboost as xgb
like image 42
İzzet Türkalp Akbaşlı Avatar answered Mar 15 '23 14:03

İzzet Türkalp Akbaşlı