Skip to content

Commit

Permalink
added glmnet tune / predict / enseble for classification
Browse files Browse the repository at this point in the history
  • Loading branch information
gtesei committed Oct 13, 2015
1 parent 476b7f4 commit 2684f9d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
3 changes: 2 additions & 1 deletion R-package/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ Imports:
xgboost,
magrittr,
stringr,
e1071
e1071,
glmnet
34 changes: 25 additions & 9 deletions R-package/R/fastRegression.R
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,9 @@ ff.createEnsemble = function(Xtrain,
test_i = Xtrain[ indexOut[[i]] , ]

## classification
if (! regression && caretModelName != "libsvm" ) {
if (! regression &&
caretModelName != "libsvm" &&
substr(x = model.label, start = 1 , stop = nchar('glmnet_alpha_')) != 'glmnet_alpha_') {
y_i = y.cat[ index[[i]] ]
}

Expand All @@ -996,9 +998,14 @@ ff.createEnsemble = function(Xtrain,
}


if (caretModelName == "libsvm" && ! regression) {
if ( ! regression && caretModelName == "libsvm" ) {
model = e1071::svm(x = train_i , y = y_i , kernel = "radial" , gamma = bestTune$gamma , cost = bestTune$C)

} else if ( ! regression && substr(x = model.label, start = 1 , stop = nchar('glmnet_alpha_')) == 'glmnet_alpha_' ) {
alpha <- as.numeric(substr(x = model.label, start = (nchar('glmnet_alpha_')+1) , stop = nchar(model.label)))
stopifnot(!is.na(alpha))
model <- glmnet::glmnet(x = as.matrix(train_i) , y = y_i , alpha = alpha , lambda = bestTune$lambda.min , family = "binomial")

} else if (! is.null(bestTune) ) {
model <- caret::train(y = y_i, x = train_i ,
method = caretModelName,
Expand All @@ -1017,7 +1024,9 @@ ff.createEnsemble = function(Xtrain,

##
ret = NULL
if ( regression || (! regression && caretModelName == "libsvm") ) {
if (! regression && substr(x = model.label, start = 1 , stop = nchar('glmnet_alpha_')) == 'glmnet_alpha_') {
ret = as.numeric(predict(model, newx = as.matrix(test_i), s = bestTune$lambda.min, type = "response"))
} else if ( regression || (! regression && caretModelName == "libsvm") ) {
ret = predict(model,test_i)
} else {
ret = predict(model,test_i,type = "prob")[,fact.sign]
Expand Down Expand Up @@ -1078,16 +1087,21 @@ ff.createEnsemble = function(Xtrain,
internalControlObject = caret::trainControl(method = "none", summaryFunction = controlObject$summaryFunction , classProbs = TRUE)
}

ytrain = NULL
if (regression || (! regression && caretModelName == "libsvm") ) {
ytrain = y
} else {
ytrain = y
if (! regression &&
caretModelName != "libsvm" &&
substr(x = model.label, start = 1 , stop = nchar('glmnet_alpha_')) != 'glmnet_alpha_') {
ytrain = y.cat
}

if (caretModelName == "libsvm" && ! regression) {
if (!regression && caretModelName == "libsvm") {
model = e1071::svm(x = Xtrain , y = ytrain , kernel = "radial" , gamma = bestTune$gamma , cost = bestTune$C)

} else if (!regression && substr(x = model.label, start = 1 , stop = nchar('glmnet_alpha_')) == 'glmnet_alpha_') {
alpha <- as.numeric(substr(x = model.label, start = (nchar('glmnet_alpha_')+1) , stop = nchar(model.label)))
stopifnot(!is.na(alpha))
model <- glmnet::glmnet(x = as.matrix(Xtrain) , y = ytrain , alpha = alpha , lambda = bestTune$lambda.min , family = "binomial")

} else if (! is.null(bestTune) ) {
model <- caret::train(y = ytrain, x = Xtrain ,
method = caretModelName,
Expand All @@ -1105,7 +1119,9 @@ ff.createEnsemble = function(Xtrain,
}

##
if (regression || (! regression && caretModelName == "libsvm") ) {
if (!regression && substr(x = model.label, start = 1 , stop = nchar('glmnet_alpha_')) == 'glmnet_alpha_') {
predTest = as.numeric(predict(model, newx = as.matrix(Xtest), s = bestTune$lambda.min, type = "response"))
} else if (regression || (! regression && caretModelName == "libsvm") ) {
predTest = predict(model,Xtest)
} else {
predTest = predict(model,Xtest,type = "prob")[,fact.sign]
Expand Down

0 comments on commit 2684f9d

Please sign in to comment.