Why does `predict` in R when using a glmnet model return a high dimensional prediction?
Here's a small example reproducing the issue:
model <- glmnet(matrix(rnorm(3*100), 100, 3), rbernoulli(100))
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
# 100 60
But since the predicted variable is bernoulli, I'd expect the output to be either 1 or 2 dimensional (probability of 1, or probability of each class).
I've looked at the documentation for glmnet and for predict but I can't find anything that describes this behavior. What I'm looking for is to simply fit a model to some training data, and then compute class probabilities so that I can compute AUC.
I'm mainly asking about this behavior because this does not happen if for example I use the rpart package together with predict, that is for example
df <- data.frame(cbind(matrix(rnorm(3*100), 100, 3), rbernoulli(100)))
model <- rpart(X4 ~ ., df)
length(predict(model, data.frame(matrix(rnorm(3*100), 100, 3))))
# 100, as expected
Coming from Python, I'm finding a lot of this confusing, since the predict function seems to be general, though it obviously behaves differently for two binary classifiers.
r machine-learning glm glmnet
add a comment |
Here's a small example reproducing the issue:
model <- glmnet(matrix(rnorm(3*100), 100, 3), rbernoulli(100))
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
# 100 60
But since the predicted variable is bernoulli, I'd expect the output to be either 1 or 2 dimensional (probability of 1, or probability of each class).
I've looked at the documentation for glmnet and for predict but I can't find anything that describes this behavior. What I'm looking for is to simply fit a model to some training data, and then compute class probabilities so that I can compute AUC.
I'm mainly asking about this behavior because this does not happen if for example I use the rpart package together with predict, that is for example
df <- data.frame(cbind(matrix(rnorm(3*100), 100, 3), rbernoulli(100)))
model <- rpart(X4 ~ ., df)
length(predict(model, data.frame(matrix(rnorm(3*100), 100, 3))))
# 100, as expected
Coming from Python, I'm finding a lot of this confusing, since the predict function seems to be general, though it obviously behaves differently for two binary classifiers.
r machine-learning glm glmnet
3
looks like you are getting predictions for mutliple values of lambda -- if you want one prediction you need to select one lambda value i.e. cv.glmnet
– user20650
Jan 1 at 19:16
1
I don't have an answer, but the relevant docs are not at?predict, but at?predict.glmnet. Based on user20650's comment, thesparameter seems relevant!
– Axeman
Jan 1 at 19:18
?predict.glmnetprovidespredict(object, newx, s = NULL, type=c("link","response","coefficients","nonzero","class"), exact = FALSE, newoffset, ...). Then, fors:Value(s) of the penalty parameter lambda at which predictions are required. Default is the entire sequence used to create the model.Because you have not supplieds, it is by default runningpredicton each lambda produced byglmnet.
– hmhensen
Jan 1 at 19:22
Thanks for the responses guys, tho I'm a bit confused. Shouldn't the lambda be determined at training?
– Jakub Arnold
Jan 1 at 19:42
@JakubArnold As mentioned in the first statement, you should usecv.glmnetinstead ofglmnetto determine the best lambda.
– hmhensen
Jan 1 at 22:16
add a comment |
Here's a small example reproducing the issue:
model <- glmnet(matrix(rnorm(3*100), 100, 3), rbernoulli(100))
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
# 100 60
But since the predicted variable is bernoulli, I'd expect the output to be either 1 or 2 dimensional (probability of 1, or probability of each class).
I've looked at the documentation for glmnet and for predict but I can't find anything that describes this behavior. What I'm looking for is to simply fit a model to some training data, and then compute class probabilities so that I can compute AUC.
I'm mainly asking about this behavior because this does not happen if for example I use the rpart package together with predict, that is for example
df <- data.frame(cbind(matrix(rnorm(3*100), 100, 3), rbernoulli(100)))
model <- rpart(X4 ~ ., df)
length(predict(model, data.frame(matrix(rnorm(3*100), 100, 3))))
# 100, as expected
Coming from Python, I'm finding a lot of this confusing, since the predict function seems to be general, though it obviously behaves differently for two binary classifiers.
r machine-learning glm glmnet
Here's a small example reproducing the issue:
model <- glmnet(matrix(rnorm(3*100), 100, 3), rbernoulli(100))
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
# 100 60
But since the predicted variable is bernoulli, I'd expect the output to be either 1 or 2 dimensional (probability of 1, or probability of each class).
I've looked at the documentation for glmnet and for predict but I can't find anything that describes this behavior. What I'm looking for is to simply fit a model to some training data, and then compute class probabilities so that I can compute AUC.
I'm mainly asking about this behavior because this does not happen if for example I use the rpart package together with predict, that is for example
df <- data.frame(cbind(matrix(rnorm(3*100), 100, 3), rbernoulli(100)))
model <- rpart(X4 ~ ., df)
length(predict(model, data.frame(matrix(rnorm(3*100), 100, 3))))
# 100, as expected
Coming from Python, I'm finding a lot of this confusing, since the predict function seems to be general, though it obviously behaves differently for two binary classifiers.
r machine-learning glm glmnet
r machine-learning glm glmnet
edited Jan 1 at 19:27
desertnaut
18.5k73874
18.5k73874
asked Jan 1 at 19:09
Jakub ArnoldJakub Arnold
39.5k77199297
39.5k77199297
3
looks like you are getting predictions for mutliple values of lambda -- if you want one prediction you need to select one lambda value i.e. cv.glmnet
– user20650
Jan 1 at 19:16
1
I don't have an answer, but the relevant docs are not at?predict, but at?predict.glmnet. Based on user20650's comment, thesparameter seems relevant!
– Axeman
Jan 1 at 19:18
?predict.glmnetprovidespredict(object, newx, s = NULL, type=c("link","response","coefficients","nonzero","class"), exact = FALSE, newoffset, ...). Then, fors:Value(s) of the penalty parameter lambda at which predictions are required. Default is the entire sequence used to create the model.Because you have not supplieds, it is by default runningpredicton each lambda produced byglmnet.
– hmhensen
Jan 1 at 19:22
Thanks for the responses guys, tho I'm a bit confused. Shouldn't the lambda be determined at training?
– Jakub Arnold
Jan 1 at 19:42
@JakubArnold As mentioned in the first statement, you should usecv.glmnetinstead ofglmnetto determine the best lambda.
– hmhensen
Jan 1 at 22:16
add a comment |
3
looks like you are getting predictions for mutliple values of lambda -- if you want one prediction you need to select one lambda value i.e. cv.glmnet
– user20650
Jan 1 at 19:16
1
I don't have an answer, but the relevant docs are not at?predict, but at?predict.glmnet. Based on user20650's comment, thesparameter seems relevant!
– Axeman
Jan 1 at 19:18
?predict.glmnetprovidespredict(object, newx, s = NULL, type=c("link","response","coefficients","nonzero","class"), exact = FALSE, newoffset, ...). Then, fors:Value(s) of the penalty parameter lambda at which predictions are required. Default is the entire sequence used to create the model.Because you have not supplieds, it is by default runningpredicton each lambda produced byglmnet.
– hmhensen
Jan 1 at 19:22
Thanks for the responses guys, tho I'm a bit confused. Shouldn't the lambda be determined at training?
– Jakub Arnold
Jan 1 at 19:42
@JakubArnold As mentioned in the first statement, you should usecv.glmnetinstead ofglmnetto determine the best lambda.
– hmhensen
Jan 1 at 22:16
3
3
looks like you are getting predictions for mutliple values of lambda -- if you want one prediction you need to select one lambda value i.e. cv.glmnet
– user20650
Jan 1 at 19:16
looks like you are getting predictions for mutliple values of lambda -- if you want one prediction you need to select one lambda value i.e. cv.glmnet
– user20650
Jan 1 at 19:16
1
1
I don't have an answer, but the relevant docs are not at
?predict, but at ?predict.glmnet. Based on user20650's comment, the s parameter seems relevant!– Axeman
Jan 1 at 19:18
I don't have an answer, but the relevant docs are not at
?predict, but at ?predict.glmnet. Based on user20650's comment, the s parameter seems relevant!– Axeman
Jan 1 at 19:18
?predict.glmnet provides predict(object, newx, s = NULL, type=c("link","response","coefficients","nonzero","class"), exact = FALSE, newoffset, ...). Then, for s: Value(s) of the penalty parameter lambda at which predictions are required. Default is the entire sequence used to create the model. Because you have not supplied s, it is by default running predict on each lambda produced by glmnet.– hmhensen
Jan 1 at 19:22
?predict.glmnet provides predict(object, newx, s = NULL, type=c("link","response","coefficients","nonzero","class"), exact = FALSE, newoffset, ...). Then, for s: Value(s) of the penalty parameter lambda at which predictions are required. Default is the entire sequence used to create the model. Because you have not supplied s, it is by default running predict on each lambda produced by glmnet.– hmhensen
Jan 1 at 19:22
Thanks for the responses guys, tho I'm a bit confused. Shouldn't the lambda be determined at training?
– Jakub Arnold
Jan 1 at 19:42
Thanks for the responses guys, tho I'm a bit confused. Shouldn't the lambda be determined at training?
– Jakub Arnold
Jan 1 at 19:42
@JakubArnold As mentioned in the first statement, you should use
cv.glmnet instead of glmnet to determine the best lambda.– hmhensen
Jan 1 at 22:16
@JakubArnold As mentioned in the first statement, you should use
cv.glmnet instead of glmnet to determine the best lambda.– hmhensen
Jan 1 at 22:16
add a comment |
1 Answer
1
active
oldest
votes
In R, you would find many examples in which you get an output based on dimension/ class etc. of your input to a function.
For glmnet, by default you supply a range of lambda's:
lambda (i.e. shrinkage factor) is a hyper parameter for regularized regression model (glmnet).
set.seed(1)
model <- glmnet(matrix(rnorm(3*100), 100, 3), purrr::rbernoulli(100))
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
#[1] 100 61
length(model$lambda)
[1] 61
You need to tune it based on a desired performance measure, to find the optimal/ best value for your model. Once you have it, you can use it to get final predictions. Something like:
model <- glmnet(matrix(rnorm(3*100), 100, 3), purrr::rbernoulli(100),
lambda = 0.19) # assuming its an optimal value
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
# [1] 100 1
while rpart doesn't require a hyper parameter by default as it fits a complete tree without pruning which is equivalent of supplying a single hyper parameter value that corresponds to fitting data till the leaf nodes. Therefore you obtain a single set of predictions. The downside of using this current classifier being that its not generalized.
Therefore if you are shifting from python to R for a applied machine learning tasks its best to utilize the caret package its a homogeneous framework combining multiple statistical models under a unified modeling approach.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998198%2fwhy-does-predict-in-r-when-using-a-glmnet-model-return-a-high-dimensional-pred%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In R, you would find many examples in which you get an output based on dimension/ class etc. of your input to a function.
For glmnet, by default you supply a range of lambda's:
lambda (i.e. shrinkage factor) is a hyper parameter for regularized regression model (glmnet).
set.seed(1)
model <- glmnet(matrix(rnorm(3*100), 100, 3), purrr::rbernoulli(100))
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
#[1] 100 61
length(model$lambda)
[1] 61
You need to tune it based on a desired performance measure, to find the optimal/ best value for your model. Once you have it, you can use it to get final predictions. Something like:
model <- glmnet(matrix(rnorm(3*100), 100, 3), purrr::rbernoulli(100),
lambda = 0.19) # assuming its an optimal value
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
# [1] 100 1
while rpart doesn't require a hyper parameter by default as it fits a complete tree without pruning which is equivalent of supplying a single hyper parameter value that corresponds to fitting data till the leaf nodes. Therefore you obtain a single set of predictions. The downside of using this current classifier being that its not generalized.
Therefore if you are shifting from python to R for a applied machine learning tasks its best to utilize the caret package its a homogeneous framework combining multiple statistical models under a unified modeling approach.
add a comment |
In R, you would find many examples in which you get an output based on dimension/ class etc. of your input to a function.
For glmnet, by default you supply a range of lambda's:
lambda (i.e. shrinkage factor) is a hyper parameter for regularized regression model (glmnet).
set.seed(1)
model <- glmnet(matrix(rnorm(3*100), 100, 3), purrr::rbernoulli(100))
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
#[1] 100 61
length(model$lambda)
[1] 61
You need to tune it based on a desired performance measure, to find the optimal/ best value for your model. Once you have it, you can use it to get final predictions. Something like:
model <- glmnet(matrix(rnorm(3*100), 100, 3), purrr::rbernoulli(100),
lambda = 0.19) # assuming its an optimal value
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
# [1] 100 1
while rpart doesn't require a hyper parameter by default as it fits a complete tree without pruning which is equivalent of supplying a single hyper parameter value that corresponds to fitting data till the leaf nodes. Therefore you obtain a single set of predictions. The downside of using this current classifier being that its not generalized.
Therefore if you are shifting from python to R for a applied machine learning tasks its best to utilize the caret package its a homogeneous framework combining multiple statistical models under a unified modeling approach.
add a comment |
In R, you would find many examples in which you get an output based on dimension/ class etc. of your input to a function.
For glmnet, by default you supply a range of lambda's:
lambda (i.e. shrinkage factor) is a hyper parameter for regularized regression model (glmnet).
set.seed(1)
model <- glmnet(matrix(rnorm(3*100), 100, 3), purrr::rbernoulli(100))
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
#[1] 100 61
length(model$lambda)
[1] 61
You need to tune it based on a desired performance measure, to find the optimal/ best value for your model. Once you have it, you can use it to get final predictions. Something like:
model <- glmnet(matrix(rnorm(3*100), 100, 3), purrr::rbernoulli(100),
lambda = 0.19) # assuming its an optimal value
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
# [1] 100 1
while rpart doesn't require a hyper parameter by default as it fits a complete tree without pruning which is equivalent of supplying a single hyper parameter value that corresponds to fitting data till the leaf nodes. Therefore you obtain a single set of predictions. The downside of using this current classifier being that its not generalized.
Therefore if you are shifting from python to R for a applied machine learning tasks its best to utilize the caret package its a homogeneous framework combining multiple statistical models under a unified modeling approach.
In R, you would find many examples in which you get an output based on dimension/ class etc. of your input to a function.
For glmnet, by default you supply a range of lambda's:
lambda (i.e. shrinkage factor) is a hyper parameter for regularized regression model (glmnet).
set.seed(1)
model <- glmnet(matrix(rnorm(3*100), 100, 3), purrr::rbernoulli(100))
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
#[1] 100 61
length(model$lambda)
[1] 61
You need to tune it based on a desired performance measure, to find the optimal/ best value for your model. Once you have it, you can use it to get final predictions. Something like:
model <- glmnet(matrix(rnorm(3*100), 100, 3), purrr::rbernoulli(100),
lambda = 0.19) # assuming its an optimal value
preds <- predict(model, matrix(rnorm(3*100), 100, 3))
dim(preds)
# [1] 100 1
while rpart doesn't require a hyper parameter by default as it fits a complete tree without pruning which is equivalent of supplying a single hyper parameter value that corresponds to fitting data till the leaf nodes. Therefore you obtain a single set of predictions. The downside of using this current classifier being that its not generalized.
Therefore if you are shifting from python to R for a applied machine learning tasks its best to utilize the caret package its a homogeneous framework combining multiple statistical models under a unified modeling approach.
edited Jan 2 at 14:01
answered Jan 1 at 20:49
Mankind_008Mankind_008
1,5582312
1,5582312
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998198%2fwhy-does-predict-in-r-when-using-a-glmnet-model-return-a-high-dimensional-pred%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
looks like you are getting predictions for mutliple values of lambda -- if you want one prediction you need to select one lambda value i.e. cv.glmnet
– user20650
Jan 1 at 19:16
1
I don't have an answer, but the relevant docs are not at
?predict, but at?predict.glmnet. Based on user20650's comment, thesparameter seems relevant!– Axeman
Jan 1 at 19:18
?predict.glmnetprovidespredict(object, newx, s = NULL, type=c("link","response","coefficients","nonzero","class"), exact = FALSE, newoffset, ...). Then, fors:Value(s) of the penalty parameter lambda at which predictions are required. Default is the entire sequence used to create the model.Because you have not supplieds, it is by default runningpredicton each lambda produced byglmnet.– hmhensen
Jan 1 at 19:22
Thanks for the responses guys, tho I'm a bit confused. Shouldn't the lambda be determined at training?
– Jakub Arnold
Jan 1 at 19:42
@JakubArnold As mentioned in the first statement, you should use
cv.glmnetinstead ofglmnetto determine the best lambda.– hmhensen
Jan 1 at 22:16