glmnet:将预测概率转换为预测类

时间:2017-11-14 12:29:10

标签: r glmnet

我使用泊松分布运行cv.glmnet以获得二元结果。 predict函数返回预测的概率,但不返回预测的类。如何将概率转换为类,以便生成confusionMatrix来确定AUC等? NB。至少一个预测概率> 1。

cv <- cv.glmnet(deriv.x, deriv.y, foldid = foldid, weights = wts, family = "poisson")
pred <- predict.cv.glmnet(cv, newx = valid.x, s = "lambda.min", "response")
confusionMatrix(pred, valid.y)
Error in confusionMatrix.default(pred, valid.y) : 
  the data cannot have more levels than the reference

1 个答案:

答案 0 :(得分:0)

这是因为你返回的是概率,而不是类,所以试试吧:

cv <- cv.glmnet(deriv.x, deriv.y, foldid = foldid, weights = wts, family = "poisson")
pred <- predict.cv.glmnet(cv, newx = valid.x, s = "lambda.min", "response")

pred <- ifelse(pred > 0.5,1,0) 

confusionMatrix(pred, valid.y)

您可以选择自己的值进行四舍五入。