Amit
Amit

Reputation: 7035

AttributeError: module ‘xgboost’ has no attribute ‘XGBRegressor’

I am trying to run xgboost using spyder and python, but I keep getting this error:

AttributeError: module ‘xgboost’ has no attribute ‘XGBRegressor’

Here is the code:

import xgboost as xgb 

xgb.XGBRegressor(max_depth=3, learning_rate=0.1, n_estimators=100, silent=True, 
                 objective='reg:linear', gamma=0, min_child_weight=1, 
                 max_delta_step=0, subsample=1, colsample_bytree=1, 
                 seed=0, missing=None)

Error is

Traceback (most recent call last):

  File "<ipython-input-33-d257a9a2a5d8>", line 1, in <module>
    xgb.XGBRegressor(max_depth=3, learning_rate=0.1, n_estimators=100, silent=True,

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

I have Python 3.5.2 :: Anaconda 4.2.0 (x86_64)

How do I solve this?

Upvotes: 5

Views: 39862

Answers (6)

Pasindu Ranasinghe
Pasindu Ranasinghe

Reputation: 237

Probably there are many files with the same name of xgboost.That is why python tries to load one of these instead of the original package file.

Tip Check your working directory and see if there are any py files with the same name of xgboost.py. If so, Change the name to something else.

Upvotes: 5

SJa
SJa

Reputation: 515

For my case, I solve this problem pretty easily using

from xgboost import XGBRegressor

Upvotes: 2

Crawford Collins
Crawford Collins

Reputation: 61

I had to make sure to follow all the download instructions from the xgboost website. After installing and compiling, i forgot to run these. https://xgboost.readthedocs.io/en/latest/build.html#python-package-installation

Upvotes: 0

Mike
Mike

Reputation: 913

I had the exact same problem with Python 3.6.2 and Anaconda 1.6.8 on windows10 64bits (fall creator update)

To get it to work, here is what I did :

1/ Uninstall xgboost from within anaconda, in the chosen environement.

2/ Manually deleted the xgboost directory in C:\ProgramData\Anaconda3

3/ Downloaded xgboost from This page

4/ From Anaconda, launch a command prompt from (from the environment you want xgboost into of course)

5/ CD to the directory you downloaded the whl file to and type : pip install xgboost‑0.6+20171121‑cp36‑cp36m‑win_amd64.whl (or the exact name of the file you downloaded)

I did all these steps and xgboost worked properly

Upvotes: 1

bitit1994
bitit1994

Reputation: 362

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

Here is the command I used:

export PYTHONPATH=PATH_TO_YOUR_setup.py_file

For me, PATH_TO_YOUR_setup.py_file is ~/xgboost/python-package

Upvotes: 6

Randy
Randy

Reputation: 14847

Since your dir call is missing basically everything, my suspicion is that wherever you're starting your script from has an xgboost subfolder with an empty __init__.py in it that is being found first by your import.

Upvotes: 2

Related Questions