parent
add57f8880
commit
e50ccc4d3c
@ -120,11 +120,25 @@ XGB_DLL SEXP XGDMatrixCreateFromMat_R(SEXP mat, SEXP missing, SEXP n_threads) {
|
||||
ctx.nthread = asInteger(n_threads);
|
||||
std::int32_t threads = ctx.Threads();
|
||||
|
||||
if (is_int) {
|
||||
xgboost::common::ParallelFor(nrow, threads, [&](xgboost::omp_ulong i) {
|
||||
for (size_t j = 0; j < ncol; ++j) {
|
||||
data[i * ncol + j] = is_int ? static_cast<float>(iin[i + nrow * j]) : din[i + nrow * j];
|
||||
auto v = iin[i + nrow * j];
|
||||
if (v == NA_INTEGER) {
|
||||
data[i * ncol + j] = std::numeric_limits<float>::quiet_NaN();
|
||||
} else {
|
||||
data[i * ncol + j] = static_cast<float>(v);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
xgboost::common::ParallelFor(nrow, threads, [&](xgboost::omp_ulong i) {
|
||||
for (size_t j = 0; j < ncol; ++j) {
|
||||
data[i * ncol + j] = din[i + nrow * j];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
DMatrixHandle handle;
|
||||
CHECK_CALL(XGDMatrixCreateFromMat_omp(BeginPtr(data), nrow, ncol,
|
||||
asReal(missing), &handle, threads));
|
||||
|
||||
@ -56,6 +56,42 @@ test_that("xgb.DMatrix: basic construction", {
|
||||
expect_equal(raw_fd, raw_dgc)
|
||||
})
|
||||
|
||||
test_that("xgb.DMatrix: NA", {
|
||||
n_samples <- 3
|
||||
x <- cbind(
|
||||
x1 = sample(x = 4, size = n_samples, replace = TRUE),
|
||||
x2 = sample(x = 4, size = n_samples, replace = TRUE)
|
||||
)
|
||||
x[1, "x1"] <- NA
|
||||
|
||||
m <- xgb.DMatrix(x)
|
||||
xgb.DMatrix.save(m, "int.dmatrix")
|
||||
|
||||
x <- matrix(as.numeric(x), nrow = n_samples, ncol = 2)
|
||||
colnames(x) <- c("x1", "x2")
|
||||
m <- xgb.DMatrix(x)
|
||||
|
||||
xgb.DMatrix.save(m, "float.dmatrix")
|
||||
|
||||
iconn <- file("int.dmatrix", "rb")
|
||||
fconn <- file("float.dmatrix", "rb")
|
||||
|
||||
expect_equal(file.size("int.dmatrix"), file.size("float.dmatrix"))
|
||||
|
||||
bytes <- file.size("int.dmatrix")
|
||||
idmatrix <- readBin(iconn, "raw", n = bytes)
|
||||
fdmatrix <- readBin(fconn, "raw", n = bytes)
|
||||
|
||||
expect_equal(length(idmatrix), length(fdmatrix))
|
||||
expect_equal(idmatrix, fdmatrix)
|
||||
|
||||
close(iconn)
|
||||
close(fconn)
|
||||
|
||||
file.remove("int.dmatrix")
|
||||
file.remove("float.dmatrix")
|
||||
})
|
||||
|
||||
test_that("xgb.DMatrix: saving, loading", {
|
||||
# save to a local file
|
||||
dtest1 <- xgb.DMatrix(test_data, label = test_label)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user