XGBOOST - DMATRIX

时间:2017-09-02 05:26:03

标签: python machine-learning xgboost

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

##### 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())

收到以下错误:

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

这很奇怪,因为我一直将Linux机器的xgboost模型拉到窗口。我找不到任何关于如何修复互联网的信息,所以我想知道是否有人知道如何解决?

1 个答案:

答案 0 :(得分:2)

我们可能遇到同样的问题。

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

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

这是我使用的命令:

export PYTHONPATH=~/xgboost/python-package

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