Skip to content

Commit eb4ad30

Browse files
committed
xgboost 3.0.0 发布了,调整代码适应之
1 parent 59dfafc commit eb4ad30

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Imports:
6464
tensorflow,
6565
tidycensus,
6666
tikzDevice,
67-
xgboost,
67+
xgboost (>= 3.0.0),
6868
xml2,
6969
xts,
7070
VGAM
@@ -75,6 +75,7 @@ LinkingTo: BH (>= 1.75.0-0), StanHeaders (>= 2.32.6)
7575
SystemRequirements: pgf (>= 3.00), JAGS 4.x.y
7676
Additional_repositories:
7777
https://stan-dev.r-universe.dev,
78+
https://dmlc.r-universe.dev,
7879
https://inla.r-inla-download.org/R/stable
7980
Encoding: UTF-8
8081
License: CC NC ND 4.0

classification-problems.qmd

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,33 @@ iris_test <- list(
414414

415415
```{r}
416416
library(xgboost)
417-
iris_xgb <- xgboost(
418-
data = iris_train$data,
419-
label = iris_train$label,
420-
objective = "multi:softmax", # 学习任务
421-
eval_metric = "mlogloss", # 评估指标
422-
nrounds = 2, # 提升迭代的最大次数
423-
num_class = 3 # 分类数
417+
nthread <- 1
418+
data.table::setDTthreads(nthread)
419+
# 整理数据
420+
dtrain <- with(
421+
iris_train, xgb.DMatrix(data, label = label, nthread = nthread)
422+
)
423+
dtest <- with(
424+
iris_test, xgb.DMatrix(data, label = label, nthread = nthread)
425+
)
426+
# 训练集和测试集
427+
evals <- list(train = dtrain, eval = dtest)
428+
# 参数设置
429+
param <- xgb.params(
430+
max_depth = 2,
431+
learning_rate = 1,
432+
nthread = nthread,
433+
objective = "multi:softmax",
434+
eval_metric = "mlogloss",
435+
num_class = 3
436+
)
437+
# 训练数据
438+
iris_xgb <- xgb.train(
439+
params = param,
440+
data = dtrain,
441+
nrounds = 2,
442+
evals = evals,
443+
verbose = 0
424444
)
425445
```
426446

regression-problems.qmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,8 @@ Boston_test <- list(
671671
```{r}
672672
library(xgboost)
673673
Boston_xgb <- xgboost(
674-
data = Boston_train$data,
675-
label = Boston_train$label,
674+
x = Boston_train$data,
675+
y = Boston_train$label,
676676
objective = "reg:squarederror", # 学习任务
677677
eval_metric = "rmse", # 评估指标
678678
nrounds = 6

time-series-regression.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ data_size <- nrow(air_passengers)
487487
# 拆分数据集
488488
train_size <- floor(data_size * 0.67)
489489
train_data <- air_passengers[1:train_size, ]
490-
test_data <- air_passengers[train_size:data_size, ]
490+
test_data <- air_passengers[-(1:train_size), ]
491491
492492
# 随机数种子对结果的影响非常大 试试 set.seed(20232023)
493493
set.seed(20222022)

0 commit comments

Comments
 (0)