
實時航班追蹤背后的技術:在線飛機追蹤器的工作原理
library(dplyr) #數據處理使用
library(data.table) #數據讀取使用
library(xgboost) #模型使用
library(Matrix) #模型數據處理使用
library(caret) # 調參和計算模型評價參數使用
library(pROC) #繪圖使用
library(ggplot2) #繪圖使用
library(ggpubr) #繪圖使用
library(ggprism) #繪圖使用
# 讀取數據
data <- fread("./XGBoost.txt",data.table = F) # 替換為你的數據文件名或路徑
數據長這個樣子,一共35727行,214列。每一行代表一個樣本,第一列是樣本標簽malignant
或normal
,后面213列是213個特征。我們想根據213個特征,使用RF訓練出一個能夠對樣本進行精準分類的模型。
構建XGBoost模型
# 將分類轉換成0和1
data <- data %>% mutate(type = ifelse(type == "normal",1,0))
# 分割數據為訓練集和測試集
set.seed(123) # 設置隨機種子,保證結果可復現
split <- sample.split(data$type, SplitRatio = 0.8) # 將數據按照指定比例分割
train_data <- subset(data, split == TRUE) # 訓練集
test_data <- subset(data, split == FALSE) # 測試集
# 定義訓練集特征和目標變量
X_train <- train_data[, -1]
y_train <- train_data[, 1]
# 將特征和目標變量轉換為DMatrix格式
dtrain <- xgb.DMatrix(data = as.matrix(X_train), label = y_train)
# 設置XGBoost參數
params <- list(objective = "binary:logistic", eval_metric = "logloss", eta = 0.1, max_depth = 3)
# 設置迭代輪數(樹的數量)
nrounds <- 100
# 訓練XGBoost模型
xgb_model <- xgboost(params = params, data = dtrain, nrounds = nrounds)
# 在訓練集上進行預測
train_predictions <- predict(xgb_model, newdata = dtrain)
train_predictions <- ifelse(train_predictions > 0.5,1,0)
# 計算準確率
accuracy <- mean(train_predictions == y_train)
print(paste("訓練集準確率:", accuracy))
# 在測試集上進行預測
X_test <- test_data[, -1]
y_test <- as.factor(test_data[, 1])
dtest <- xgb.DMatrix(data = as.matrix(X_test))
test_predictions <- predict(xgb_model, newdata = dtest)
test_predictions <- ifelse(test_predictions > 0.5,1,0)
# 計算準確率
accuracy <- mean(test_predictions == y_test)
print(paste("測試集準確率:", accuracy))
從accuracy來看,初始模型在訓練集和測試集中表現的都挺好的。
caret包
中,XGBoost模型有七個參數可以進行調節。
##參數調整
# 將數據集轉換為trainControl對象
ctrl <- trainControl(
method = "cv", # 交叉驗證
number = 5, # 5折交叉驗證
verboseIter = FALSE)
# 設置參數網格
param_grid <- expand.grid(
nrounds = c(100, 200), # 迭代輪數(nrounds)
max_depth = c(3, 6), # 最大樹深度(max_depth)
eta = c(0.1), # 學習率(eta)
gamma = c(0, 0.1), # 樹分裂所需的最小損失減少值
colsample_bytree = c(0.8), # 特征子采樣比例(colsample_bytree)
min_child_weight = c(1, 3), # 葉子節點的最小權重和(min_child_weight)
subsample = c(0.8)) # 和樣本子采樣比例(subsample)
# 使用train()函數進行參數調優
xgb_model <- train(
x = X_train,
y = y_train,
method = "xgbTree",
trControl = ctrl,
tuneGrid = param_grid)
# 輸出最佳參數配置
print(xgb_model$bestTune)
使用最佳參數訓練模型
# 設置最佳XGBoost參數
params <- list(objective = "binary:logistic", eval_metric = "logloss",
eta = 0.1, max_depth = 3, gamma = 0.1,
colsample_bytree = 0.8,
min_child_weight = 1,
subsample = 0.8)
# 訓練模型
xgb_model_final <- xgb.train(params = params, data = dtrain, nrounds = 200)
# 在訓練集上進行預測
train_predictions <- predict(xgb_model_final, newdata = dtrain)
train_predictions <- ifelse(train_predictions > 0.5,1,0)
# 計算準確率
accuracy <- mean(train_predictions == y_train)
print(paste("訓練集準確率:", accuracy))
# 在測試集上進行預測
X_test <- test_data[, -1]
y_test <- as.factor(test_data[, 1])
dtest <- xgb.DMatrix(data = as.matrix(X_test))
test_predictions <- predict(xgb_model_final, newdata = dtest)
test_predictions <- ifelse(test_predictions > 0.5,1,0)
# 計算準確率
accuracy <- mean(test_predictions == y_test)
print(paste("測試集準確率:", accuracy))
調參之后的模型比初始模型表現提升了一些。
本文章轉載微信公眾號@Bio小菜鳥