I am trying to follow the example code from XGBoost4J's tutorial, but am running into a check failure in the C++ implementation. The error message is not obvious what's wrong:
ml.dmlc.xgboost4j.java.XGBoostError: [21:28:54] /Users/phcho/tmp/xgboost/src/objective/regression_obj.cu:35: Check failed: info.labels.Shape(0) == info.num_row_ (0 vs. 329) : Invalid shape of labels.
Stack trace:
[bt] (0) 1 libxgboost4j3410071582095733624.dyl 0x000000013ac09a74 dmlc::LogMessageFatal::~LogMessageFatal() + 116
[bt] (1) 2 libxgboost4j3410071582095733624.dyl 0x000000013ad495b9 xgboost::obj::(anonymous namespace)::CheckRegInputs(xgboost::MetaInfo const&, xgboost::HostDeviceVector<float> const&) + 249
[bt] (2) 3 libxgboost4j3410071582095733624.dyl 0x000000013ad4dda8 xgboost::obj::RegLossObj<xgboost::obj::LogisticClassification>::GetGradient(xgboost::HostDeviceVector<float> const&, xgboost::MetaInfo const&, int, xgboost::HostDeviceVector<xgboost::detail::GradientPairInternal<float> >*) + 40
[bt] (3) 4 libxgboost4j3410071582095733624.dyl 0x000000013ace6f04 xgboost::LearnerImpl::UpdateOneIter(int, std::__1::shared_ptr<xgboost::DMatrix>) + 580
[bt] (4) 5 libxgboost4j3410071582095733624.dyl 0x000000013ac14b89 XGBoosterUpdateOneIter + 137
[bt] (5) 6 ??? 0x000000011671978c 0x0 + 4671510412
[bt] (6) 7 ??? 0x0000000116712b28 0x0 + 4671482664
[bt] (7) 8 ??? 0x0000000116712e90 0x0 + 4671483536
[bt] (8) 9 ??? 0x00000001167129ba 0x0 + 4671482298
The code:
List<float[]> rows = new ArrayList<>();
int trainRows = (int) (rows.size() * 0.7);
int testRows = rows.size() - trainRows;
int columnCount = rows.get(0).length;
float[] flatTrainMatrix = new float[trainRows * columnCount];
float[] flatTestMatrix = new float[testRows * columnCount];
for (int i = 0; i < rows.size(); i++) {
if (i < trainRows) {
System.arraycopy(rows.get(i), 0, flatTrainMatrix, i * columnCount, columnCount);
} else {
System.arraycopy(rows.get(i), 0, flatTestMatrix, (i - trainRows) * columnCount, columnCount);
}
}
var params = Map.<String, Object>of("max_depth", 4, "eta", 1.0, "objective", "binary:logistic", "eval_metric", "auc");
try {
DMatrix trainMatrix = new DMatrix(flatTrainMatrix, trainRows, columnCount, -1);
DMatrix testMatrix = new DMatrix(flatTestMatrix, testRows, columnCount, -1);
var booster = XGBoost.train(trainMatrix, params, 1, Map.of("train", trainMatrix, "test", testMatrix), null, null);
booster.saveModel("/tmp/model.bin");
} catch (XGBoostError e) {
throw new RuntimeException(e); // The error message from above is from here.
}
}
I am using the ml.dmlc:xgboost4j_2.12:1.6.1.