问题
I am trying to run a regression problem on python using XGBOOST:
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))
But it raises the following error, after a few iterations:
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'
I tried to convert the inputs and output with:
.apply(pd.to_numeric)
But is still reports the same error; how could it be fixed?
回答1:
This code runs without any problems:
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)
Note that I don't have am eval set in clf.fit
. What is your variable validation
? It shoud be a tuple of xgboost.DMatrix
and string, e.g:
dval = xgb.DMatrix(X_val, label=y_val)
validation = (dval, "validation")
回答2:
Please ensure that both your train set and validation set has labels (y) for all inputs (x). You can store the inputs and labels in form of DMatrix and then pass them to the model. These are needed for the evaluation purpose.
回答3:
In my case, the same issue occurs when I had non-ascii characters in the dataframe. If you have remove it, it will work. Or try lightboost gbm it will throw an exact error.
来源:https://stackoverflow.com/questions/51187225/xgboost-error-info-labels-size-0u-0-vs-0