Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
811 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...