'XGBClassifier' object has no attribute 'DMatrix'

[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!)

1 Like

Make sure that xgb refers to the XGBoost module:

import xgboost as xgb
1 Like

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?

What’s the value of print(xgb)?

1 Like

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

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.

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

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

1 Like

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'>

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

1 Like

It is this:

<class 'xgboost.core.DMatrix'>

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.

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!

@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.

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

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

A post was split to a new topic: Found input variables with inconsistent numbers of samples