iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 19
1
影片教學

R語言-預測方法大全系列 第 19

[Day-19] 預測操作--xgb-上 (gb xgb in r)

  • 分享至 

  • xImage
  •  

您的訂閱是我製作影片的動力
訂閱點這裡~/images/emoticon/emoticon39.gif

Yes

若內容有誤,還請留言指正,謝謝您的指教/images/emoticon/emoticon07.gif

#影片程式碼
#GB
library(gbm)
set.seed(123)
model <- gbm(formula = Sepal.Length ~ .,distribution = "gaussian",
             data = iris,n.trees = 479,
             interaction.depth = 1,
             shrinkage = 0.1,n.minobsinnode = 15,
             bag.fraction = 1,
             train.fraction = 0.5,cv.folds = 10)
#分類用 bernoulli 迴歸用gaussian(RMSE)

#XGB
data("iris")
par(mfrow=c(1,4))
boxplot(iris$Sepal.Length)$out
boxplot(iris$Sepal.Width)$out
boxplot(iris$Petal.Length)$out
boxplot(iris$Petal.Width)$out
#離群值不多先不轉
n <- nrow(iris)
set.seed(1117)
subiris <- sample(seq_len(n), size = round(0.7 * n))
traindata <- iris[subiris,]
testdata <- iris[ - subiris,]

features <- setdiff(names(iris), "Sepal.Length")
library(vtreat)#單熱 
library(dplyr)
onehot <- vtreat::designTreatmentsZ(iris, features, verbose = FALSE)
單熱 <- onehot %>%
  magrittr::use_series(scoreFrame) %>%        
  dplyr::filter(code %in% c("clean", "lev")) %>% 
  magrittr::use_series(varName)     
trainx <- vtreat::prepare(onehot, traindata, varRestriction = 單熱) %>% as.matrix()
trainy <- traindata$Sepal.Length
testx <- vtreat::prepare(onehot, testdata, varRestriction = 單熱) %>% as.matrix()
testy <- testdata$Sepal.Length
library(xgboost)
set.seed(123)
model <-  xgboost(data = trainx,
                  label = trainy,
                  nrounds = 531,eta=0.1,
                  min_child_weight=3,max_depth=1,
                  subsample=1,colsample_bytree=0.9,
                  objective = "reg:squarederror",verbose = 0)

上一篇
[Day-18] 預測操作--隨機森林-下 (random forest tuning in r)
下一篇
[Day-20] 預測操作--xgb-中 (xgb tuning in r)
系列文
R語言-預測方法大全30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言