模块“xgboost”没有属性“DMatrix”

Jef*_*ist 4 python machine-learning xgboost

我提取了一些在 kaggle (linux) 上运行的 ML 代码,并尝试在 Windows 机器上的 jupyter notebook 中运行它。这是代码(其中一些):

##### 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())
Run Code Online (Sandbox Code Playgroud)

收到以下错误:

---------------------------------------------------------------------------
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'
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为我一直将 xgboost 模型从 linux 机器拉到 windows。我在互联网上找不到任何有关如何修复的信息,所以我想知道是否有人知道如何修复?

bit*_*994 6

我们可能有同样的问题。

我通过明确告诉 Python 在哪里可以找到 xgboost 库来解决它。

原因是我有多个名为 xgboost.py 的脚本。Python 可能错误地导入了其中之一,因此无法找到 'DMatrix' 的定义。

这是我使用的命令:

export PYTHONPATH=~/xgboost/python-package
Run Code Online (Sandbox Code Playgroud)

您应该将 '~/xgboost/python-package' 更改为 /xgboost/python-package/setup.py 文件所在的文件夹。