'XGBClassifier' object has no attribute 'DMatrix'


#1

[Migrated from https://github.com/dmlc/xgboost/issues/3549]

Working with

Mac OS X 10.11.6
Conda 4.5.9
Python environment 3.6.3
pip installed XGBoost 0.72.1
all of a sudden I get this error:

‘XGBClassifier’ object has no attribute ‘DMatrix’

in this line of code:

dtrain = xgb.DMatrix(X_train, y_train, feature_names=columns)

How can I fix this?

(Btw: Thanks for making xgboost available. Very much appreciated!)


#2

Make sure that xgb refers to the XGBoost module:

import xgboost as xgb

#3

Thanks for your superfast reply! :+1:

I already do import with the exact code line that you propose but nevertheless get the error.

What can I do?


#4

What’s the value of print(xgb)?


#5

<module 'xgboost' from '/Users/arneckpa/anaconda/envs/k3/lib/python3.6/site-packages/xgboost/__init__.py'>


#6

I found something else.

Right now I import with

import xgboost as xgb
from xgboost import XGBClassifier

If I comment out the second line and restart the notebook I don’t get the error (but cannot use the XGBClassifier). The code ran until recently with both import lines.


#7

With the second line enabled, what is the value of print(xgb.DMatrix)?

Until the problem is resolved, you can use xgb.XGBClassifier.


#8

Thanks! Will try with xgb.XGBClassifier

With only the first line:

<module 'xgboost' from '/Users/arneckpa/anaconda/envs/k3/lib/python3.6/site-packages/xgboost/__init__.py'>

With both lines (second enabled too):

<module 'xgboost' from '/Users/arneckpa/anaconda/envs/k3/lib/python3.6/site-packages/xgboost/__init__.py'>

#9

I cannot reproduce the problem on my MacBook Pro. What is the value of print(xgb.DMatrix)?


#10

It is this:

<class 'xgboost.core.DMatrix'>

#11

Hang on for a second. I see that in my current code I instantiate an xgb.XGBClassifier as xgb.

Since I haven’t executed the code cells in order, this might haven’t yielded the error on previous runs.

Will investigate.


#12

That was the cause for the error. I now instantiate wit xgboost = xgb.XGBClassifier(…). No more errors thrown.

I’m very sorry to have taken your time. I very much value your immediate help which has enabled me to quickly resolve the matter. Very much appreciated!


#13

@hcho3, I have encounter same problems. It was working perfectly for long time. But today, suddenly I got error message ‘XGBClassifier’ object has no attribute ‘DMatrix’ I tried all the instructed procedure and even the uninstall xgboost and removed manually from the folder and install again. But the error message is still pop up.
I import all the possible package as below;

import xgboost as xgb
from xgboost import XGBClassifier
from xgboost.sklearn import XGBClassifier

could you please help me to provide some possible solution.

thank you.


#14

Can you post your script? Make sure that you didn’t use xgb to name your XGBClassifier object.


#16

Thank you. I got what you mean. now the problem is solved.