gpt4 book ai didi

r - 如何获得 glmnet 多项逻辑回归的混淆矩阵?

转载 作者:行者123 更新时间:2023-12-05 07:39:14 27 4
gpt4 key购买 nike

我拟合多项逻辑回归模型,我想获得混淆矩阵以获得准确度

library("glmnet")
x=data.matrix(train[-1])
y= data.matrix(train[1])
x_test=data.matrix(test[-1])
y_test=unlist(test[1])

fit.glm=glmnet(x,y,family="multinomial",alpha = 1, type.multinomial = "grouped")
cvfit=cv.glmnet(x, y, family="multinomial", type.multinomial = "grouped", parallel = TRUE)
y_predict=unlist(predict(cvfit, newx = x_test, s = "lambda.min", type = "class"))

然后我使用 caret 库计算混淆矩阵

library("lattice")
library("ggplot2")
library("caret")
confusionMatrix(data=y_predict,reference=y_test)

我收到这个错误,我不知道如何解决

Error in confusionMatrix.default(data = y_predict, reference = y_test): The data must contain some levels that overlap the reference.

我只是发布了 y_predicty_test 的 str。他们可能会有所帮助

str(y_predict)
chr [1:301, 1] "6" "2" "7" "9" "3" "2" "3" "6" "6" "8" "6" "5" "6" ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "1"

str(y_test)
Factor w/ 10 levels "accessory","activation",..: 6 8 2 9 3 2 3 5 10 8 ...
- attr(*, "names")= chr [1:301] "category1" "category2" "category3" "category4" ...

我使用 unlist 来避免出现此错误 Error: x must be atomic for 'sort.list'

最佳答案

跟踪您的标签并使用它将结果从 glmnet 转换回标签并应用混淆矩阵是有意义的。我使用具有 3 个标签的 iris 数据集:

idx = sample(nrow(iris),100)
train = iris[idx,]
test = iris[-idx,]

我们将响应转换为数字:

x = data.matrix(train[,-5])
y = as.numeric(train[,5]) - 1
x_test = data.matrix(test[,-5])
y_test = as.numeric(test[,5]) - 1

拟合,这里有点不同,我们得到概率:

cvfit=cv.glmnet(x, y, family="multinomial")
y_predict=predict(cvfit, newx = x_test, s = "lambda.min", type = "response")

在此示例中,响应是 Species 列,在您的示例中它将是 test[,1] :

ref_labels = test$Species
pred_labels = levels(test$Species)[max.col(y_predict[,,1])]

caret::confusionMatrix(table(pred_labels,ref_labels))
Confusion Matrix and Statistics

ref_labels
pred_labels setosa versicolor virginica
setosa 20 0 0
versicolor 0 12 0
virginica 0 0 18

关于r - 如何获得 glmnet 多项逻辑回归的混淆矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47170250/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com