我正在尝试使用XGBOOST在python上运行回归问题:
import xgboost
global clf
clf = XGBRegressor(n_estimators = 500,
learning_rate = 0.05,
max_depth=6,
n_jobs=4,
alpha = 0.1)
clf.fit(X_train, y_train,
early_stopping_rounds = 5,
eval_set = validation, verbose=False)
predicted_test_tr = np.round(clf.predict(X_test))
但是经过几次迭代,它会引发以下错误:
XGBoostError: b'[10:56:23] src/objective/regression_obj.cc:43: Check failed: info.labels_.size() != 0U (0 vs. 0) label set cannot be empty\n\nStack trace returned 7 entries:\n[bt] (0) 0 libxgboost.dylib 0x0000001a1971b7a1 dmlc::StackTrace() + 305\n[bt] (1) 1 libxgboost.dylib 0x0000001a1971b52f dmlc::LogMessageFatal::~LogMessageFatal() + 47\n[bt] (2) 2 libxgboost.dylib 0x0000001a19792d21 xgboost::obj::RegLossObj<xgboost::obj::LinearSquareLoss>::GetGradient(xgboost::HostDeviceVector<float>*, xgboost::MetaInfo const&, int, xgboost::HostDeviceVector<xgboost::detail::GradientPairInternal<float> >*) + 257\n[bt] (3) 3 libxgboost.dylib 0x0000001a19717496 xgboost::LearnerImpl::UpdateOneIter(int, xgboost::DMatrix*) + 1014\n[bt] (4) 4 libxgboost.dylib 0x0000001a1973369f XGBoosterUpdateOneIter + 79\n[bt] (5) 5 libffi.6.dylib 0x0000000110308884 ffi_call_unix64 + 76\n[bt] (6) 6 ??? 0x00007ffee1b29950 0x0 + 140732684998992\n\n'
我试图用以下方法转换输入和输出:
.apply(pd.to_numeric)
但是仍然报告相同的错误;怎么解决?
答案 0 :(得分:1)
请确保您的训练集和验证集都为所有输入(x)贴上标签(y)。您可以以DMatrix的形式存储输入和标签,然后将它们传递给模型。这些是评估所需的。
答案 1 :(得分:1)
对于我来说,当我在数据框中使用非ASCII字符时,也会发生相同的问题。如果删除了它,它将起作用。或者尝试使用lightboost gbm,它将引发确切的错误。
答案 2 :(得分:0)
此代码运行没有任何问题:
from xgboost import XGBRegressor
clf = XGBRegressor(n_estimators = 500,
learning_rate = 0.05,
max_depth=6,
n_jobs=1,
alpha = 0.1)
import numpy as np
X_train = np.random.uniform(size=(100,10))
y_train = np.zeros(100)
clf.fit(X_train, y_train, verbose=False)
请注意,我没有在clf.fit
中设置评估。您的变量validation
是什么?它应该是xgboost.DMatrix
和字符串的元组,例如:
dval = xgb.DMatrix(X_val, label=y_val)
validation = (dval, "validation")