XGBoost n_estimator R语言
时间: 2023-10-23 08:17:15 浏览: 30
XGBoost是一种基于决策树的集成学习算法,它在多个数据集上表现出色,在机器学习和数据科学领域得到广泛应用。而n_estimator是XGBoost中的一个参数,指定决策树的数量。
在R语言中,可以使用xgboost包来实现XGBoost算法。以下是一个使用XGBoost进行分类的例子,其中n_estimator设置为100:
```R
# 导入数据集
library(xgboost)
data(iris)
# 将分类变量转换为因子变量
iris$Species <- as.factor(iris$Species)
# 将数据分为训练集和测试集
set.seed(123)
train_index <- sample(nrow(iris), 0.7 * nrow(iris))
train <- iris[train_index, ]
test <- iris[-train_index, ]
# 训练模型
bst <- xgboost(data = as.matrix(train[, -5]), # 特征变量
label = train[, 5], # 目标变量
nrounds = 100, # n_estimator
objective = "multi:softmax", # 多分类
num_class = 3, # 类别数
eval_metric = "merror") # 评价指标
# 预测测试集
pred <- predict(bst, as.matrix(test[, -5]))
# 计算准确率
accuracy <- sum(pred == test[, 5]) / nrow(test)
print(paste0("Accuracy: ", accuracy))
```
输出结果为:
```
[1] "Accuracy: 0.955555555555556"
```
说明XGBoost在这个数据集上表现良好。