library(xgboost)
# install.packages("caret")
library(caret)
getwd()
setwd("/Users/tssc7/Desktop")
d <- read.csv("column_3C_weka.csv")
View(d)
#劃分訓練、測試資料
trainlist <- createDataPartition(d$class,p = 0.7, list = FALSE)
trainset <- d[trainlist,]
testset <- d[-trainlist,]
#1.資料格式轉換(1-1~1-5)
library(Matrix)
#=====================================================================
#1-1自變量轉為matrix(自變量為2-7列)
traindata1 <- data.matrix(trainset[,c(2:7)])
#1-2 sparse=TRUE(稀疏矩陣)
# traindata2 <- Matrix(traindata1,sparse = T)
#1-3應變量
train_y <- trainset[,1]
#1-4自變量、應變量組合list
traindata <- list(data = traindata1,label = trainset[,1])
#1-5構件模型需要的xgb.DMatrix
dtrain <- xgb.DMatrix(data = traindata$data, label = traindata$label)
#=====================================================================
#2.測試資料
testset1 <- data.matrix(testset[,c(2:7)])
# testset2 <- Matrix(testset1,sparse = T)
test_y <- testset[,1]
testset <- list(data = testset1, label = testset[,1])
dtest <- xgb.DMatrix(data = testset$data, label = testset$label)
#3.建立模型
model_xgb <- xgboost(data = dtrain, booster = 'gbtree', max_depth = 20, eta = 0.5, num_class = 2, nround = 200)
#4.模型預測
pre <- predict(model_xgb,newdata = dtest)
#5.模型評估
library(caret)
xgb.cf <- caret::confusionMatrix(as.factor(pre),as.factor(test_y))
xgb.cf
我想在這支xgboost程式中用迴圈去做參數的調整,再用k-fold交叉驗證來驗證模型,可是沒有甚麼頭緒,本人算是個菜鳥,希望有人可以幫幫我,萬分感謝!!!
主要就是先把你想測的參數範圍定好
然後將參數做組合
再引用至 Loop 中調用
# 匯入套件,crossing 要用的
library(tidyr)
# 產生參數的數列 (要測多少參數自己設)
max_depth_param <- seq(from=5, to=10, by=1)
eta_param <- seq(from=0, to=1, by=0.5)
n_round_param <- seq(from=10, to=20, by=10)
# 建立參數組合
params_table <- crossing(max_depth_param, eta_param, n_round_param)
# 於迴圈中調用參數組合 (Loop 中自行套用各個參數)
for (i in 1:nrow(params_table)){
# 你的 Model
model_xgb <- xgboost(data = dtrain,
booster = 'gbtree',
max_depth = params_table$max_depth_param,
eta = params_table$eta_param,
num_class = 2,
nround = params_table$n_round_param)
}