对比误差,监督分类

时间:2016-12-23 18:08:51

标签: r classification r-caret concept

我尝试使用以下数据集关注this tutorialMushroom Classification。我正在寻找监督分类问题,我想我明白了。

运行以下代码后......

library(caret)

dataset = read.csv("mushrooms.csv")
dim(dataset)
sapply(dataset, class)
head(dataset)
levels(dataset$class)

set.seed(100)
inTrain <- createDataPartition(y=dataset$class,p=.75,list=FALSE)
str(inTrain)
training <- dataset[inTrain,]
testing <- dataset[-inTrain,]
nrow(training)
nrow(testing)

control <- trainControl(method="cv", number=10)
metric <- "Accuracy"

train.lda <- train(class ~., data=training, method="lda", trControl=control)

...我看到数据集有8124行和22个变量-plus分类器 - 。

dim(dataset)
[1] 8124   23

然而,当执行train时,我收到以下错误:

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

环顾网络,甚至在Stack Overflow中,我发现的解释是我的预测器只有一个因子级别。就像class变量只取一个值一样?尽管如此,之前在代码中我检查了该变量的级别,并且我得到它的级别为2,因为它需要两个值。

levels(dataset$class)
[1] "e" "p"

因此,我不明白为什么我会收到错误。我的推理有什么问题?我做错了什么?

谢谢。

样品申请:

structure(list(class = structure(c(2L, 1L, 1L, 2L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("e", 
"p"), class = "factor"), cap.shape = structure(c(6L, 6L, 1L, 
6L, 6L, 6L, 1L, 1L, 6L, 1L, 6L, 6L, 1L, 6L, 6L, 5L, 3L, 6L, 6L, 
6L, 1L, 6L, 1L, 1L, 1L, 3L, 6L, 6L, 3L, 6L, 1L, 6L, 6L, 6L, 1L, 
6L, 5L, 6L, 6L, 1L, 1L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 3L, 6L, 
6L, 1L, 6L, 6L, 1L, 3L, 1L, 6L, 6L, 5L, 1L, 1L, 1L, 1L, 3L, 6L, 
3L, 6L, 6L, 3L, 1L, 3L, 6L, 1L, 3L, 6L, 3L, 6L, 3L, 6L, 6L, 3L, 
6L, 6L, 6L, 1L, 6L, 3L, 5L, 6L, 1L, 6L, 6L, 6L, 6L, 3L, 6L, 1L, 
6L), .Label = c("b", "c", "f", "k", "s", "x"), class = "factor"), 
    cap.surface = structure(c(3L, 3L, 3L, 4L, 3L, 4L, 3L, 4L, 
    4L, 3L, 4L, 4L, 3L, 4L, 1L, 1L, 1L, 3L, 4L, 3L, 3L, 4L, 4L, 
    4L, 3L, 3L, 4L, 4L, 1L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 4L, 
    1L, 3L, 4L, 4L, 1L, 4L, 3L, 4L, 4L, 3L, 4L, 4L, 4L, 3L, 3L, 
    4L, 3L, 4L, 1L, 3L, 3L, 4L, 1L, 4L, 3L, 4L, 4L, 3L, 3L, 4L, 
    4L, 1L, 1L, 4L, 1L, 4L, 3L, 3L, 3L, 4L, 4L, 4L, 3L, 4L, 1L, 
    1L, 4L, 3L, 3L, 3L, 4L, 1L, 1L, 3L, 4L, 4L, 3L, 3L, 4L, 3L, 
    3L, 4L), .Label = c("f", "g", "s", "y"), class = "factor"), 
    cap.color = structure(c(5L, 10L, 9L, 9L, 4L, 10L, 9L, 9L, 
    9L, 10L, 10L, 10L, 10L, 9L, 5L, 4L, 9L, 5L, 9L, 5L, 10L, 
    5L, 10L, 9L, 9L, 9L, 10L, 9L, 5L, 10L, 10L, 9L, 10L, 5L, 
    10L, 10L, 4L, 5L, 10L, 10L, 10L, 10L, 5L, 9L, 10L, 9L, 10L, 
    9L, 10L, 10L, 5L, 9L, 9L, 5L, 9L, 10L, 4L, 9L, 10L, 5L, 4L, 
    10L, 10L, 10L, 9L, 5L, 9L, 10L, 10L, 4L, 10L, 9L, 10L, 5L, 
    10L, 10L, 9L, 5L, 5L, 5L, 5L, 9L, 4L, 4L, 10L, 5L, 9L, 9L, 
    5L, 5L, 5L, 9L, 10L, 10L, 5L, 9L, 5L, 10L, 9L, 9L), .Label = c("b", 
    "c", "e", "g", "n", "p", "r", "u", "w", "y"), class = "factor"), 
    bruises = structure(c(2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 
    2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 
    1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 
    2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 
    2L), .Label = c("f", "t"), class = "factor"), odor = structure(c(7L, 
    1L, 4L, 7L, 6L, 1L, 1L, 4L, 7L, 1L, 4L, 1L, 1L, 7L, 6L, 6L, 
    6L, 7L, 7L, 7L, 1L, 7L, 4L, 1L, 4L, 7L, 1L, 4L, 6L, 1L, 4L, 
    7L, 4L, 4L, 4L, 4L, 6L, 7L, 1L, 4L, 1L, 4L, 6L, 7L, 1L, 1L, 
    4L, 4L, 4L, 4L, 1L, 4L, 4L, 7L, 7L, 1L, 6L, 1L, 4L, 1L, 6L, 
    1L, 4L, 4L, 4L, 6L, 4L, 1L, 1L, 6L, 4L, 4L, 4L, 1L, 1L, 4L, 
    4L, 4L, 7L, 1L, 6L, 7L, 6L, 6L, 4L, 6L, 1L, 4L, 4L, 6L, 6L, 
    4L, 1L, 4L, 6L, 1L, 4L, 1L, 1L, 1L), .Label = c("a", "c", 
    "f", "l", "m", "n", "p", "s", "y"), class = "factor"), gill.attachment = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("a", "f"), class = "factor"), 
    gill.spacing = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
    2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
    1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 
    2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
    1L, 1L), .Label = c("c", "w"), class = "factor"), gill.size = structure(c(2L, 
    1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 
    1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 
    2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 
    2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("b", "n"), class = "factor"), 
    gill.color = structure(c(5L, 5L, 6L, 6L, 5L, 6L, 3L, 6L, 
    8L, 3L, 3L, 6L, 11L, 5L, 6L, 5L, 5L, 6L, 6L, 5L, 5L, 6L, 
    5L, 11L, 3L, 6L, 6L, 11L, 5L, 6L, 3L, 5L, 6L, 8L, 6L, 11L, 
    5L, 11L, 8L, 5L, 6L, 6L, 3L, 8L, 11L, 6L, 5L, 11L, 6L, 11L, 
    11L, 5L, 5L, 5L, 5L, 11L, 6L, 11L, 5L, 8L, 5L, 5L, 3L, 3L, 
    6L, 5L, 6L, 11L, 11L, 8L, 8L, 3L, 11L, 8L, 5L, 8L, 6L, 8L, 
    11L, 6L, 5L, 11L, 6L, 6L, 11L, 5L, 11L, 6L, 11L, 6L, 6L, 
    5L, 3L, 3L, 6L, 3L, 8L, 6L, 3L, 3L), .Label = c("b", "e", 
    "g", "h", "k", "n", "o", "p", "r", "u", "w", "y"), class = "factor"), 
    stalk.shape = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
    2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
    1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 
    2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
    1L, 1L), .Label = c("e", "t"), class = "factor"), stalk.root = structure(c(4L, 
    3L, 3L, 4L, 4L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 3L, 4L, 3L, 3L, 3L, 4L, 3L, 3L, 4L, 2L, 3L, 
    4L, 3L, 5L, 3L, 2L, 4L, 4L, 2L, 3L, 3L, 5L, 4L, 4L, 3L, 3L, 
    3L, 3L, 5L, 5L, 5L, 3L, 3L, 4L, 4L, 3L, 4L, 3L, 3L, 5L, 4L, 
    3L, 3L, 3L, 3L, 4L, 3L, 5L, 3L, 4L, 2L, 3L, 2L, 5L, 3L, 2L, 
    2L, 5L, 4L, 5L, 4L, 4L, 4L, 4L, 5L, 4L, 3L, 3L, 5L, 4L, 4L, 
    3L, 3L, 3L, 4L, 3L, 5L, 3L, 3L, 3L), .Label = c("?", "b", 
    "c", "e", "r"), class = "factor"), stalk.surface.above.ring = structure(c(3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("f", "k", 
    "s", "y"), class = "factor"), stalk.surface.below.ring = structure(c(3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 3L, 
    3L, 3L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 
    3L, 4L, 3L, 4L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 3L, 4L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 3L), .Label = c("f", "k", 
    "s", "y"), class = "factor"), stalk.color.above.ring = structure(c(8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("b", "c", 
    "e", "g", "n", "o", "p", "w", "y"), class = "factor"), stalk.color.below.ring = structure(c(8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("b", "c", 
    "e", "g", "n", "o", "p", "w", "y"), class = "factor"), veil.type = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "p", class = "factor"), 
    veil.color = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L), .Label = c("n", "o", "w", "y"), class = "factor"), 
    ring.number = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("n", "o", "t"), class = "factor"), ring.type = structure(c(5L, 
    5L, 5L, 5L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 1L, 5L, 
    1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 1L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 1L, 5L, 5L, 1L, 5L, 1L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 1L, 5L, 5L, 5L, 5L, 5L), .Label = c("e", "f", 
    "l", "n", "p"), class = "factor"), spore.print.color = structure(c(3L, 
    4L, 4L, 3L, 4L, 3L, 3L, 4L, 3L, 3L, 4L, 3L, 4L, 4L, 3L, 4L, 
    4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 3L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 3L, 4L, 3L, 3L, 4L, 3L, 4L, 
    3L, 4L, 4L, 3L, 3L, 3L, 4L, 4L, 3L, 3L, 4L, 4L, 3L, 3L, 4L, 
    4L, 4L, 4L, 4L, 3L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, 3L, 3L, 4L, 
    7L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 3L, 3L, 3L, 4L, 3L, 4L, 4L, 
    3L, 3L, 3L, 4L, 4L, 4L, 3L, 4L, 3L), .Label = c("b", "h", 
    "k", "n", "o", "r", "u", "w", "y"), class = "factor"), population = structure(c(4L, 
    3L, 3L, 4L, 1L, 3L, 3L, 4L, 5L, 4L, 3L, 4L, 4L, 5L, 1L, 6L, 
    1L, 4L, 4L, 4L, 4L, 5L, 4L, 3L, 4L, 5L, 3L, 3L, 6L, 5L, 3L, 
    4L, 3L, 6L, 4L, 5L, 5L, 4L, 5L, 4L, 4L, 6L, 6L, 5L, 3L, 3L, 
    4L, 3L, 4L, 4L, 4L, 4L, 3L, 5L, 5L, 4L, 1L, 3L, 3L, 6L, 5L, 
    4L, 4L, 3L, 4L, 1L, 4L, 4L, 3L, 5L, 5L, 4L, 5L, 4L, 4L, 5L, 
    5L, 6L, 5L, 6L, 4L, 4L, 6L, 4L, 4L, 4L, 4L, 4L, 6L, 5L, 6L, 
    4L, 4L, 3L, 1L, 4L, 4L, 3L, 4L, 4L), .Label = c("a", "c", 
    "n", "s", "v", "y"), class = "factor"), habitat = structure(c(6L, 
    2L, 4L, 6L, 2L, 2L, 4L, 4L, 2L, 4L, 2L, 4L, 2L, 6L, 2L, 6L, 
    2L, 2L, 6L, 6L, 4L, 2L, 4L, 4L, 4L, 2L, 4L, 4L, 6L, 1L, 4L, 
    6L, 4L, 5L, 4L, 1L, 6L, 6L, 1L, 4L, 2L, 5L, 6L, 2L, 4L, 2L, 
    4L, 4L, 5L, 5L, 2L, 2L, 4L, 6L, 6L, 4L, 2L, 2L, 2L, 5L, 6L, 
    4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 6L, 1L, 4L, 1L, 5L, 2L, 1L, 
    1L, 5L, 6L, 2L, 2L, 2L, 6L, 2L, 2L, 2L, 2L, 2L, 2L, 6L, 6L, 
    2L, 2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("d", "g", 
    "l", "m", "p", "u", "w"), class = "factor")), .Names = c("class", 
"cap.shape", "cap.surface", "cap.color", "bruises", "odor", "gill.attachment", 
"gill.spacing", "gill.size", "gill.color", "stalk.shape", "stalk.root", 
"stalk.surface.above.ring", "stalk.surface.below.ring", "stalk.color.above.ring", 
"stalk.color.below.ring", "veil.type", "veil.color", "ring.number", 
"ring.type", "spore.print.color", "population", "habitat"), row.names = c(NA, 
100L), class = "data.frame")

.csv文件的前五行数据加上标题

class,cap-shape,cap-surface,cap-color,bruises,odor,gill-attachment,gill-spacing,gill-size,gill-color,stalk-shape,stalk-root,stalk-surface-above-ring,stalk-surface-below-ring,stalk-color-above-ring,stalk-color-below-ring,veil-type,veil-color,ring-number,ring-type,spore-print-color,population,habitat
p,x,s,n,t,p,f,c,n,k,e,e,s,s,w,w,p,w,o,p,k,s,u
e,x,s,y,t,a,f,c,b,k,e,c,s,s,w,w,p,w,o,p,n,n,g
e,b,s,w,t,l,f,c,b,n,e,c,s,s,w,w,p,w,o,p,n,n,m
p,x,y,w,t,p,f,c,n,n,e,e,s,s,w,w,p,w,o,p,k,s,u
e,x,s,g,f,n,f,w,b,k,t,e,s,s,w,w,p,w,o,e,n,a,g

1 个答案:

答案 0 :(得分:1)

您的数据集可能不是随机的,我要说的只是您的样本,而不是完整的数据集但是您的许多变量只有一个使用的值。输入summary(dataset),您会很快看到一些示例。例如,显示的一部分是:

 stalk.color.below.ring veil.type veil.color ring.number
 w      :100            p:100     n:  0      n:  0      
 b      :  0                      o:  0      o:100      
 c      :  0                      w:100      t:  0      
 e      :  0                      y:  0                 
 g      :  0                                            
 n      :  0                                            
 (Other):  0   

请注意,对于veil.type,只有一个可能的值。

levels(dataset$veil.type)
[1] "p"

我希望这是您的错误消息的来源。

Factors = which(sapply(dataset, class) == "factor")
sapply(dataset[,Factors], function(x) { length(levels(x)) })

显示veil.type是唯一只有一个可能级别的属性。