[英]Apply glm to iris dataset in caret R
I try to apply the glm algorithm on the Iris dataset, using the following code:我尝试使用以下代码在 Iris 数据集上应用 glm 算法:
library(tidyverse)
library(caret)
dataset <- iris
tt_index <- createDataPartition(dataset$Sepal.Length, times = 1, p = 0.9, list = FALSE)
train_set <- dataset[tt_index, ]
test_set <- dataset[-tt_index, ]
model_glm <- train(Species ~.,
data = train_set,
method = "glm")
But it returned me this alert:但它给了我这个警报:
Something is wrong; all the Accuracy metric values are missing:
Accuracy Kappa
Min. : NA Min. : NA
1st Qu.: NA 1st Qu.: NA
Median : NA Median : NA
Mean :NaN Mean :NaN
3rd Qu.: NA 3rd Qu.: NA
Max. : NA Max. : NA
NA's :1 NA's :1
Perhaps I am missing something, please your help will be greatly appreciated.也许我错过了一些东西,请您的帮助将不胜感激。
You're trying to train a binary (ie binomial) classification model to data with a response variable that has more than 2 levels.您正在尝试将二元(即二项式)分类 model 训练为具有超过 2 个级别的响应变量的数据。 The warnings that you are getting and that you can see if you type warnings()
will tell you that您收到的警告以及您在输入warnings()
时可以看到的警告会告诉您
glm models can only use 2-class outcomes glm 模型只能使用 2 类结果
So this won't work.所以这行不通。
An option is to omit one of the outcomes, eg do一种选择是省略其中一个结果,例如做
dataset <- subset(iris, Species != "virginica")
dataset <- transform(dataset, Species = droplevels(Species))
tt_index <- createDataPartition(
dataset$Sepal.Length, times = 1, p = 0.5, list = FALSE)
train_set <- dataset[tt_index, ]
test_set <- dataset[-tt_index, ]
model_glm <- train(
Species ~.,
data = train_set,
method = "glm",
family = "binomial")
This will still give warnings, but they have a different origin.这仍然会发出警告,但它们的来源不同。 Bottom line is, that this is probably not a very good example for testing glm
-based binomial classification.底线是,这可能不是测试基于glm
的二项式分类的一个很好的例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.