gpt4 book ai didi

python - 模块 'xgboost' 没有属性 'DMatrix'

转载 作者:太空宇宙 更新时间:2023-11-04 02:42:46 27 4
gpt4 key购买 nike

我提取了一些在 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())

收到以下错误:

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

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

最佳答案

我们可能有同样的问题。

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

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

这是我使用的命令:

export PYTHONPATH=~/xgboost/python-package

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

关于python - 模块 'xgboost' 没有属性 'DMatrix',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46010755/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com