Lint fix on consistent assignment
This commit is contained in:
parent
ce9d7045f9
commit
d7fce99564
@ -48,7 +48,7 @@ setMethod("predict", signature = "xgb.Booster",
|
|||||||
stop("predict: ntreelimit must be equal to or greater than 1")
|
stop("predict: ntreelimit must be equal to or greater than 1")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
option = 0
|
option <- 0
|
||||||
if (outputmargin) {
|
if (outputmargin) {
|
||||||
option <- option + 1
|
option <- option + 1
|
||||||
}
|
}
|
||||||
|
|||||||
@ -261,7 +261,7 @@ xgb.cv.mknfold <- function(dall, nfold, param, stratified, folds) {
|
|||||||
ret <- list()
|
ret <- list()
|
||||||
for (k in 1:nfold) {
|
for (k in 1:nfold) {
|
||||||
dtest <- slice(dall, folds[[k]])
|
dtest <- slice(dall, folds[[k]])
|
||||||
didx = c()
|
didx <- c()
|
||||||
for (i in 1:nfold) {
|
for (i in 1:nfold) {
|
||||||
if (i != k) {
|
if (i != k) {
|
||||||
didx <- append(didx, folds[[i]])
|
didx <- append(didx, folds[[i]])
|
||||||
|
|||||||
@ -124,15 +124,15 @@ xgb.cv <- function(params=list(), data, nrounds, nfold, label = NULL, missing =
|
|||||||
stop("xgb.cv: cannot assign two different objectives")
|
stop("xgb.cv: cannot assign two different objectives")
|
||||||
if (!is.null(params$objective))
|
if (!is.null(params$objective))
|
||||||
if (class(params$objective) == 'function') {
|
if (class(params$objective) == 'function') {
|
||||||
obj = params$objective
|
obj <- params$objective
|
||||||
params[['objective']] = NULL
|
params[['objective']] <- NULL
|
||||||
}
|
}
|
||||||
# if (!is.null(params$eval_metric) && !is.null(feval))
|
# if (!is.null(params$eval_metric) && !is.null(feval))
|
||||||
# stop("xgb.cv: cannot assign two different evaluation metrics")
|
# stop("xgb.cv: cannot assign two different evaluation metrics")
|
||||||
if (!is.null(params$eval_metric))
|
if (!is.null(params$eval_metric))
|
||||||
if (class(params$eval_metric)=='function') {
|
if (class(params$eval_metric)=='function') {
|
||||||
feval = params$eval_metric
|
feval <- params$eval_metric
|
||||||
params[['eval_metric']] = NULL
|
params[['eval_metric']] <- NULL
|
||||||
}
|
}
|
||||||
|
|
||||||
# Early Stopping
|
# Early Stopping
|
||||||
@ -144,9 +144,9 @@ xgb.cv <- function(params=list(), data, nrounds, nfold, label = NULL, missing =
|
|||||||
if (is.null(maximize))
|
if (is.null(maximize))
|
||||||
{
|
{
|
||||||
if (params$eval_metric %in% c('rmse','logloss','error','merror','mlogloss')) {
|
if (params$eval_metric %in% c('rmse','logloss','error','merror','mlogloss')) {
|
||||||
maximize = FALSE
|
maximize <- FALSE
|
||||||
} else {
|
} else {
|
||||||
maximize = TRUE
|
maximize <- TRUE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,16 +167,16 @@ xgb.cv <- function(params=list(), data, nrounds, nfold, label = NULL, missing =
|
|||||||
mat_pred <- FALSE
|
mat_pred <- FALSE
|
||||||
if (!is.null(obj_type) && obj_type == 'multi:softprob')
|
if (!is.null(obj_type) && obj_type == 'multi:softprob')
|
||||||
{
|
{
|
||||||
num_class = params[['num_class']]
|
num_class <- params[['num_class']]
|
||||||
if (is.null(num_class))
|
if (is.null(num_class))
|
||||||
stop('must set num_class to use softmax')
|
stop('must set num_class to use softmax')
|
||||||
predictValues <- matrix(0,xgb.numrow(dtrain),num_class)
|
predictValues <- matrix(0,xgb.numrow(dtrain),num_class)
|
||||||
mat_pred = TRUE
|
mat_pred <- TRUE
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
predictValues <- rep(0,xgb.numrow(dtrain))
|
predictValues <- rep(0,xgb.numrow(dtrain))
|
||||||
history <- c()
|
history <- c()
|
||||||
print.every.n = max(as.integer(print.every.n), 1L)
|
print.every.n <- max(as.integer(print.every.n), 1L)
|
||||||
for (i in 1:nrounds) {
|
for (i in 1:nrounds) {
|
||||||
msg <- list()
|
msg <- list()
|
||||||
for (k in 1:nfold) {
|
for (k in 1:nfold) {
|
||||||
|
|||||||
@ -125,7 +125,7 @@ treeDump <- function(feature_names, text, keepDetail){
|
|||||||
}
|
}
|
||||||
|
|
||||||
linearDump <- function(feature_names, text){
|
linearDump <- function(feature_names, text){
|
||||||
which(text == "weight:") %>% {a=.+1;text[a:length(text)]} %>% as.numeric %>% data.table(Feature = feature_names, Weight = .)
|
which(text == "weight:") %>% {a <- .+1; text[a:length(text)]} %>% as.numeric %>% data.table(Feature = feature_names, Weight = .)
|
||||||
}
|
}
|
||||||
|
|
||||||
# Avoid error messages during CRAN check.
|
# Avoid error messages during CRAN check.
|
||||||
|
|||||||
@ -81,7 +81,7 @@ xgb.model.dt.tree <- function(feature_names = NULL, filename_dump = NULL, model
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(!is.null(model)){
|
if(!is.null(model)){
|
||||||
text = xgb.dump(model = model, with.stats = T)
|
text <- xgb.dump(model = model, with.stats = T)
|
||||||
} else if(!is.null(filename_dump)){
|
} else if(!is.null(filename_dump)){
|
||||||
text <- readLines(filename_dump) %>% str_trim(side = "both")
|
text <- readLines(filename_dump) %>% str_trim(side = "both")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -140,27 +140,27 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(),
|
|||||||
warning('watchlist is provided but verbose=0, no evaluation information will be printed')
|
warning('watchlist is provided but verbose=0, no evaluation information will be printed')
|
||||||
}
|
}
|
||||||
|
|
||||||
dot.params = list(...)
|
dot.params <- list(...)
|
||||||
nms.params = names(params)
|
nms.params <- names(params)
|
||||||
nms.dot.params = names(dot.params)
|
nms.dot.params <- names(dot.params)
|
||||||
if (length(intersect(nms.params,nms.dot.params))>0)
|
if (length(intersect(nms.params,nms.dot.params))>0)
|
||||||
stop("Duplicated term in parameters. Please check your list of params.")
|
stop("Duplicated term in parameters. Please check your list of params.")
|
||||||
params = append(params, dot.params)
|
params <- append(params, dot.params)
|
||||||
|
|
||||||
# customized objective and evaluation metric interface
|
# customized objective and evaluation metric interface
|
||||||
if (!is.null(params$objective) && !is.null(obj))
|
if (!is.null(params$objective) && !is.null(obj))
|
||||||
stop("xgb.train: cannot assign two different objectives")
|
stop("xgb.train: cannot assign two different objectives")
|
||||||
if (!is.null(params$objective))
|
if (!is.null(params$objective))
|
||||||
if (class(params$objective)=='function') {
|
if (class(params$objective)=='function') {
|
||||||
obj = params$objective
|
obj <- params$objective
|
||||||
params$objective = NULL
|
params$objective <- NULL
|
||||||
}
|
}
|
||||||
if (!is.null(params$eval_metric) && !is.null(feval))
|
if (!is.null(params$eval_metric) && !is.null(feval))
|
||||||
stop("xgb.train: cannot assign two different evaluation metrics")
|
stop("xgb.train: cannot assign two different evaluation metrics")
|
||||||
if (!is.null(params$eval_metric))
|
if (!is.null(params$eval_metric))
|
||||||
if (class(params$eval_metric)=='function') {
|
if (class(params$eval_metric)=='function') {
|
||||||
feval = params$eval_metric
|
feval <- params$eval_metric
|
||||||
params$eval_metric = NULL
|
params$eval_metric <- NULL
|
||||||
}
|
}
|
||||||
|
|
||||||
# Early stopping
|
# Early stopping
|
||||||
@ -174,19 +174,19 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(),
|
|||||||
if (is.null(maximize))
|
if (is.null(maximize))
|
||||||
{
|
{
|
||||||
if (params$eval_metric %in% c('rmse','logloss','error','merror','mlogloss')) {
|
if (params$eval_metric %in% c('rmse','logloss','error','merror','mlogloss')) {
|
||||||
maximize = FALSE
|
maximize <- FALSE
|
||||||
} else {
|
} else {
|
||||||
maximize = TRUE
|
maximize <- TRUE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (maximize) {
|
if (maximize) {
|
||||||
bestScore = 0
|
bestScore <- 0
|
||||||
} else {
|
} else {
|
||||||
bestScore = Inf
|
bestScore <- Inf
|
||||||
}
|
}
|
||||||
bestInd = 0
|
bestInd <- 0
|
||||||
earlyStopflag = FALSE
|
earlyStopflag <- FALSE
|
||||||
|
|
||||||
if (length(watchlist)>1)
|
if (length(watchlist)>1)
|
||||||
warning('Only the first data set in watchlist is used for early stopping process.')
|
warning('Only the first data set in watchlist is used for early stopping process.')
|
||||||
@ -195,7 +195,7 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(),
|
|||||||
|
|
||||||
handle <- xgb.Booster(params, append(watchlist, dtrain))
|
handle <- xgb.Booster(params, append(watchlist, dtrain))
|
||||||
bst <- xgb.handleToBooster(handle)
|
bst <- xgb.handleToBooster(handle)
|
||||||
print.every.n=max( as.integer(print.every.n), 1L)
|
print.every.n <- max( as.integer(print.every.n), 1L)
|
||||||
for (i in 1:nrounds) {
|
for (i in 1:nrounds) {
|
||||||
succ <- xgb.iter.update(bst$handle, dtrain, i - 1, obj)
|
succ <- xgb.iter.update(bst$handle, dtrain, i - 1, obj)
|
||||||
if (length(watchlist) != 0) {
|
if (length(watchlist) != 0) {
|
||||||
@ -204,14 +204,14 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(),
|
|||||||
cat(paste(msg, "\n", sep=""))
|
cat(paste(msg, "\n", sep=""))
|
||||||
if (!is.null(early.stop.round))
|
if (!is.null(early.stop.round))
|
||||||
{
|
{
|
||||||
score = strsplit(msg,':|\\s+')[[1]][3]
|
score <- strsplit(msg,':|\\s+')[[1]][3]
|
||||||
score = as.numeric(score)
|
score <- as.numeric(score)
|
||||||
if ((maximize && score>bestScore) || (!maximize && score<bestScore)) {
|
if ((maximize && score>bestScore) || (!maximize && score<bestScore)) {
|
||||||
bestScore = score
|
bestScore <- score
|
||||||
bestInd = i
|
bestInd <- i
|
||||||
} else {
|
} else {
|
||||||
if (i-bestInd>=early.stop.round) {
|
if (i-bestInd>=early.stop.round) {
|
||||||
earlyStopflag = TRUE
|
earlyStopflag <- TRUE
|
||||||
cat('Stopping. Best iteration:',bestInd)
|
cat('Stopping. Best iteration:',bestInd)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -226,8 +226,8 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(),
|
|||||||
}
|
}
|
||||||
bst <- xgb.Booster.check(bst)
|
bst <- xgb.Booster.check(bst)
|
||||||
if (!is.null(early.stop.round)) {
|
if (!is.null(early.stop.round)) {
|
||||||
bst$bestScore = bestScore
|
bst$bestScore <- bestScore
|
||||||
bst$bestInd = bestInd
|
bst$bestInd <- bestInd
|
||||||
}
|
}
|
||||||
return(bst)
|
return(bst)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,30 +4,30 @@ context("basic functions")
|
|||||||
|
|
||||||
data(agaricus.train, package='xgboost')
|
data(agaricus.train, package='xgboost')
|
||||||
data(agaricus.test, package='xgboost')
|
data(agaricus.test, package='xgboost')
|
||||||
train = agaricus.train
|
train <- agaricus.train
|
||||||
test = agaricus.test
|
test <- agaricus.test
|
||||||
|
|
||||||
test_that("train and predict", {
|
test_that("train and predict", {
|
||||||
bst = xgboost(data = train$data, label = train$label, max.depth = 2,
|
bst <- xgboost(data = train$data, label = train$label, max.depth = 2,
|
||||||
eta = 1, nthread = 2, nround = 2, objective = "binary:logistic")
|
eta = 1, nthread = 2, nround = 2, objective = "binary:logistic")
|
||||||
pred = predict(bst, test$data)
|
pred <- predict(bst, test$data)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
test_that("early stopping", {
|
test_that("early stopping", {
|
||||||
res = xgb.cv(data = train$data, label = train$label, max.depth = 2, nfold = 5,
|
res <- xgb.cv(data = train$data, label = train$label, max.depth = 2, nfold = 5,
|
||||||
eta = 0.3, nthread = 2, nround = 20, objective = "binary:logistic",
|
eta = 0.3, nthread = 2, nround = 20, objective = "binary:logistic",
|
||||||
early.stop.round = 3, maximize = FALSE)
|
early.stop.round = 3, maximize = FALSE)
|
||||||
expect_true(nrow(res)<20)
|
expect_true(nrow(res)<20)
|
||||||
bst = xgboost(data = train$data, label = train$label, max.depth = 2,
|
bst <- xgboost(data = train$data, label = train$label, max.depth = 2,
|
||||||
eta = 0.3, nthread = 2, nround = 20, objective = "binary:logistic",
|
eta = 0.3, nthread = 2, nround = 20, objective = "binary:logistic",
|
||||||
early.stop.round = 3, maximize = FALSE)
|
early.stop.round = 3, maximize = FALSE)
|
||||||
pred = predict(bst, test$data)
|
pred <- predict(bst, test$data)
|
||||||
})
|
})
|
||||||
|
|
||||||
test_that("save_period", {
|
test_that("save_period", {
|
||||||
bst = xgboost(data = train$data, label = train$label, max.depth = 2,
|
bst <- xgboost(data = train$data, label = train$label, max.depth = 2,
|
||||||
eta = 0.3, nthread = 2, nround = 20, objective = "binary:logistic",
|
eta = 0.3, nthread = 2, nround = 20, objective = "binary:logistic",
|
||||||
save_period = 10, save_name = "xgb.model")
|
save_period = 10, save_name = "xgb.model")
|
||||||
pred = predict(bst, test$data)
|
pred <- predict(bst, test$data)
|
||||||
})
|
})
|
||||||
|
|||||||
@ -11,8 +11,8 @@ df <- data.table(Arthritis, keep.rownames = F)
|
|||||||
df[,AgeDiscret:= as.factor(round(Age/10,0))]
|
df[,AgeDiscret:= as.factor(round(Age/10,0))]
|
||||||
df[,AgeCat:= as.factor(ifelse(Age > 30, "Old", "Young"))]
|
df[,AgeCat:= as.factor(ifelse(Age > 30, "Old", "Young"))]
|
||||||
df[,ID:=NULL]
|
df[,ID:=NULL]
|
||||||
sparse_matrix = sparse.model.matrix(Improved~.-1, data = df)
|
sparse_matrix <- sparse.model.matrix(Improved~.-1, data = df)
|
||||||
output_vector = df[,Y:=0][Improved == "Marked",Y:=1][,Y]
|
output_vector <- df[,Y:=0][Improved == "Marked",Y:=1][,Y]
|
||||||
bst <- xgboost(data = sparse_matrix, label = output_vector, max.depth = 9,
|
bst <- xgboost(data = sparse_matrix, label = output_vector, max.depth = 9,
|
||||||
eta = 1, nthread = 2, nround = 10,objective = "binary:logistic")
|
eta = 1, nthread = 2, nround = 10,objective = "binary:logistic")
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user