XGBoost是一种基于提升树算法的机器学习模型。提升树是一种集成学习方法,通过将多个弱学习器(通常是决策树)组合起来构建一个更强大的模型。XGBoost采用梯度提升的方法训练提升树模型。梯度提升是一种基于梯度下降的优化算法,通过最小化损失函数的梯度来更新模型参数,从而使模型在每一轮迭代中逐步逼近最优解。
XGBoost的算法设计具有以下几个特点:
正则化:XGBoost通过正则化技术来控制模型的复杂度,防止过拟合。它在损失函数中加入了正则化项,包括L1和L2正则化,以惩罚模型复杂度过高的情况。
并行化处理:XGBoost利用并行化处理技术加速模型训练过程。它可以利用多核处理器和分布式计算环境,同时训练多棵树,并通过精心设计的数据结构和算法来最大限度地减少计算时间。
XGBoost的特点
XGBoost具有许多引人注目的特点,使其成为了机器学习领域的明星:
高性能:XGBoost在许多数据集上表现出色,它通常能够获得比其他算法更好的预测性能。
鲁棒性:XGBoost对于缺失值、异常值和噪声数据具有较强的鲁棒性,能够处理各种复杂的数据情况。
灵活性:XGBoost支持回归、分类、排序等多种任务,并且可以处理结构化数据、文本数据和图像数据等不同类型的数据。
可解释性:XGBoost提供了丰富的特征重要性分析工具,可以帮助用户理解模型的预测过程和影响因素。
XGBoost 回归模型中有许多参数可以用来调整和优化模型的性能。下面是一些常用的 XGBoost 回归模型参数:
objective: 指定优化目标,常用的回归目标函数包括 "reg:squarederror"(均方误差)和 "reg:logistic"(逻辑回归)。
eta: 学习率,控制每一步的更新幅度。较小的学习率可以使得模型收敛更稳定,但可能需要更多的迭代次数。
max_depth: 每棵树的最大深度,用于控制树的复杂度。较大的值允许模型学习更复杂的特征关系,但可能导致过拟合。
min_child_weight: 叶子节点上的最小样本权重和。用于防止过拟合。较大的值可以使模型更加保守。
gamma: 损失函数的最小减少量,用于控制树的分裂。较大的值可以防止过拟合。
subsample: 训练数据的子样本比例。用于防止过拟合。
colsample_bytree: 每棵树的特征子采样率。用于防止过拟合。
lambda (reg_lambda): L2 正则化系数。
alpha (reg_alpha): L1 正则化系数。
nrounds (num_boost_round): 迭代次数或者树的数量。
early_stopping_rounds: 用于提前停止训练的迭代次数。如果在指定的迭代次数内模型性能没有提升,则停止训练。
eval_metric: 用于评估模型性能的指标,如 "rmse"(均方根误差)和 "mae"(平均绝对误差)。
seed: 随机种子,用于控制随机性。#XGboost模型学习
#Ryuyan sample
data(agaricus.train, package='xgboost')
data(agaricus.test, package='xgboost')
dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
watchlist <- list(train = dtrain, eval = dtest)
## A simple xgb.train example:
param <- list(max_depth = 2, eta = 1, verbose = 0, nthread = 2,
objective = "binary:logistic", eval_metric = "auc")
bst <- xgb.train(param, dtrain, nrounds = 2, watchlist)
## An xgb.train example where custom objective and evaluation metric are used:
logregobj <- function(preds, dtrain) {
labels <- getinfo(dtrain, "label")
preds <- 1/(1 + exp(-preds))
grad <- preds - labels
hess <- preds * (1 - preds)
return(list(grad = grad, hess = hess))
}
evalerror <- function(preds, dtrain) {
labels <- getinfo(dtrain, "label")
err <- as.numeric(sum(labels != (preds > 0)))/length(labels)
return(list(metric = "error", value = err))
}
# These functions could be used by passing them either:
# as 'objective' and 'eval_metric' parameters in the params list:
param <- list(max_depth = 2, eta = 1, verbose = 0, nthread = 2,
objective = logregobj, eval_metric = evalerror)
bst <- xgb.train(param, dtrain, nrounds = 2, watchlist)
# or through the ... arguments:
param <- list(max_depth = 2, eta = 1, verbose = 0, nthread = 2)
bst <- xgb.train(param, dtrain, nrounds = 2, watchlist,
objective = logregobj, eval_metric = evalerror)
# or as dedicated 'obj' and 'feval' parameters of xgb.train:
bst <- xgb.train(param, dtrain, nrounds = 2, watchlist,
obj = logregobj, feval = evalerror)
## An xgb.train example of using variable learning rates at each iteration:
param <- list(max_depth = 2, eta = 1, verbose = 0, nthread = 2,
objective = "binary:logistic", eval_metric = "auc")
my_etas <- list(eta = c(0.5, 0.1))
bst <- xgb.train(param, dtrain, nrounds = 2, watchlist,
callbacks = list(cb.reset.parameters(my_etas)))
## Early stopping:
bst <- xgb.train(param, dtrain, nrounds = 25, watchlist,
early_stopping_rounds = 3)
## An 'xgboost' interface example:
bst <- xgboost(data = agaricus.train$data, label = agaricus.train$label,
max_depth = 2, eta = 1, nthread = 2, nrounds = 2,
objective = "binary:logistic")
pred <- predict(bst, agaricus.test$data)
#王煜 2023.4.19
###原理 xgboost是Boost(提升)算法家族中的一员,Boost根本思想在于通过多个简单的弱分类器,
#构建出准确率很高的强分类器。简单地来说,Boost(提升)
#就是指每一步我都产生一个弱预测模型,通过加权累加到总模型中,可以用于回归和分类问题
#boosting集成学习,由多个相关联的决策树联合决策,什么叫相关联,举个例子,有一个样本[数据->标签]是[(2,4,5)-> 4],第一棵决策树用这个样本训练得预测为3.3,那么第二棵决策树训练时的输入,这个样本就变成了[(2,4,5)-> 0.7],也就是说,下一棵决策树输入样本会与前面决策树的训练和预测相关。其中,0.7是模型预测和样本标记的差,称为残差。我们每次要学习的目标是上次学习的残差,直到残差小到满足我们的要求或其他终止条件。这个残差就是一个加预测值后能得真实值的累加量
#xgboost的R语言过程
#安装R包
# 安装R包
install.packages("xgboost")
install.packages("dplyr")
install.packages("pROC")
install.packages("caret")
install.packages("lattice")
install.packages("ggplot2")
# 加载R包
library(xgboost)
library(dplyr)
library(pROC)
library(caret)
# 查询help
?xgboost
#构建数据集,导入光谱数据
getwd()
setwd("C:/Users/DELL/Desktop/R及R代码")
data<-read.csv("TP.csv",sep =",",header= TRUE,skip = 0)
set.seed(123)
#划分训练集和测试集
index <- sort(sample(nrow(data), nrow(data)*.7))
train <- data[index,]
test <- data[-index,]
#构建模型并预测
#XGboost 参数详解
#nrounds:基模型的数量,
# Train an XGBoost model on the training data
xgb_model <- xgboost(data = as.matrix(train),
label = train$TP,
nrounds = 100,
verbose = 1)
# Use the trained model to make predictions on the test data
predictions <- predict(xgb_model, as.matrix(test))
pre<-predict(xgb_model, as.matrix(train))
#计算MAE评估模型
mean_absolute_error <- function(actual, predicted) {
mean(abs(actual-predicted))
}
mean_absolute_error(test[,1], predictions)
#计算R2评估模型
RSQ<-function(actual, predicted){ 1 - sum((actual-predicted)^2) / sum((actual - mean(actual))^2) }
RSQ(test[,1], predictions)
RSQ(train[,1], pre)
write.table(pre,file = "pre.csv",sep=",",row.names = T,col.names = T,quote
没有合适的资源?快使用搜索试试~ 我知道了~
XGboost-预测.txt
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共1个文件
txt:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 5 1 下载量 51 浏览量
2024-04-01
16:32:01
上传
评论
收藏 4KB RAR 举报
温馨提示
XGboost-预测.txt
资源推荐
资源详情
资源评论
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/TXT.png)
共 1 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
lltanwh
- 粉丝: 228
- 资源: 38
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)