xgb.train_with_cv: xgb.train_with_cv

Description Usage Arguments Value Examples

View source: R/model_xgboost.R

Description

Train Xgboost with Cross-valiation

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
xgb.train_with_cv(
  df.train,
  df.valid,
  fnames,
  label,
  fnames.cat,
  id = NULL,
  rules = NULL,
  params = NULL,
  cv.verbose = 0,
  train.verbose = 1,
  cv = FALSE,
  nfold = 5,
  score.fun = Metrics::auc,
  ...
)

Arguments

df.train

data.frame for train set

df.valid

data.frame for valid set

fnames

all feature names

label

label name

fnames.cat

categorical feature names

id

colnames of id, if not provide, default to use rowname

rules

rules to index categorical features

params

list of params for xgboost

cv.verbose

whether verbose when cv

train.verbose

whether verbose when train

cv

whether perform a k-fold cross validation, default at FALSE

nfold

number of folds of cross validation, if cv is TRUE

score.fun

score functino for validation, defaults to auc

...

other xgboost parameters

Value

trained xgboost model

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
label <- 'label'
fnames <- c('Sex','Class','Age','Freq')
fnames.cat <- c('Sex','Age','Class')
df <- data.frame(Titanic)
df$label <- ifelse(df$Survived == "Yes", 1 ,0)
in.train <- runif(nrow(df)) < 0.8
df.train <- df[in.train, ]
df.valid <- df[!in.train, ]

params <- list(max_depth = 2, eta = 1,
               nthread = 2,objective = "binary:logistic",
               eval_metric = "auc")
bst <- xgb.train_with_cv(df.train, df.valid, fnames,
                       label, fnames.cat, params = params,
                       cv.verbose = 1, train.verbose = 1,
                       cv = TRUE, nfold = 2,
                       print_every_n = 5L,
                       early_stopping_rounds = 5L,
                       nrounds = 100L)

xgb.save_model(bst, 'saved_model','xgb_baseline')
bst_loaded <- xgb.load_model('saved_model','xgb_baseline')
preds <- xgb.predict(bst_loaded, df.valid)

6chaoran/suw documentation built on July 29, 2020, 5:31 p.m.