当我的测试集有我的火车数据没有的数据时出现错误?
Error comes up, when my test set has data which my train data doesn't have?
我有一个数据集并将其拆分为 train (80%) 和 test (20%) 集。
第一步是设置决策树,然后我使用我的测试集进行预测。
tree <- rpart(train$number ~ ., train, method = "class")
pred <- predict(tree,test, type ="class")
在 运行 之后,我得到一个 错误 :
Error in model.frame.default(Terms, newdata, na.action = na.action,
xlev = attr(object, : Faktor 'orderland' hat neue Stufen Zypern
这基本上意味着,我在我的测试集中有土地 "Zypern",但在我的训练集中没有。为了解决这个问题,我在谷歌上搜索并通过将因子水平设置为相等来尝试解决这个问题。
train$orderland <- factor(train$orderland, levels=levels(test$orderland))
测试和训练数据汇总:
> summary(train)
number orderland lenkung transmission IntervalRange
Length:54616 NA's:54616 Length:54616 Length:54616 1: 7893
Class :character Class :character Class :character 2:39528
Mode :character Mode :character Mode :character 3: 7195
> summary(test)
number orderland lenkung transmission IntervalRange
Length:13655 Length:13655 Length:13655 Length:13655 1:1959
Class :character Class :character Class :character Class :character 2:9904
Mode :character Mode :character Mode :character Mode :character 3:1792
但我遇到了同样的错误...知道为什么吗?
我认为您需要强制训练集和测试集包含分类变量中的所有可能值。我不确定您的数据集的结构,但假设 lenkung
是您的土地变量。
一种解决方法是:
train_test = function(x,train_per=0.7){
smp_size = floor(train_per*nrow(x))
train_ind = sample(seq_len(nrow(x)),size = smp_size)
re = list()
re$train = x[train_ind,]
re$test = x[-train_ind,]
return(re)
}
splitted_data = split(data,data$lekung)
new_list = lapply(splitted_data,train_test)
这里我们定义了一个函数,将数据框 (x) 拆分为训练集和测试集。我们还使用 split()
函数将您的原始数据拆分为多个数据帧,其中每个数据帧仅包含 lekung 的一个可能值。假设这些值可以是 "A"、"B" 或 "C"。在那种情况下 splitted_data
将是一个包含 3 个数据框的列表,第一个包含 lekung = "A" 的所有观察结果,第二个包含 lekung = "B" 等的所有观察结果......
然后,我们将上面定义的函数应用于splitted_data
。现在 new_list
包含 lekung 的每个可能值的 2 个数据帧,一个火车和一个测试。
所以最后我们只需要将每个训练数据帧的行绑定在一起,并对测试数据帧执行相同的操作。
train = new_list[[1]][[1]]
test = new_list[[1]][[2]]
for(i in 2:length(a)){ # Then we use this loop to bind the data together
train = rbind(train,new_list[[i]][[1]])
test = rbind(test,new_list[[i]][[2]])
}
new_list
是 2 个数据帧列表的列表。所以我们使用 new_list[[1]]
来访问 lekung 的第一个值对应的 2 个数据帧和
new_list[[1]][[1]]
访问那里的第一个数据帧。
虽然可能有更好的方法来做到这一点。
我有一个数据集并将其拆分为 train (80%) 和 test (20%) 集。 第一步是设置决策树,然后我使用我的测试集进行预测。
tree <- rpart(train$number ~ ., train, method = "class")
pred <- predict(tree,test, type ="class")
在 运行 之后,我得到一个 错误 :
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = attr(object, : Faktor 'orderland' hat neue Stufen Zypern
这基本上意味着,我在我的测试集中有土地 "Zypern",但在我的训练集中没有。为了解决这个问题,我在谷歌上搜索并通过将因子水平设置为相等来尝试解决这个问题。
train$orderland <- factor(train$orderland, levels=levels(test$orderland))
测试和训练数据汇总:
> summary(train)
number orderland lenkung transmission IntervalRange
Length:54616 NA's:54616 Length:54616 Length:54616 1: 7893
Class :character Class :character Class :character 2:39528
Mode :character Mode :character Mode :character 3: 7195
> summary(test)
number orderland lenkung transmission IntervalRange
Length:13655 Length:13655 Length:13655 Length:13655 1:1959
Class :character Class :character Class :character Class :character 2:9904
Mode :character Mode :character Mode :character Mode :character 3:1792
但我遇到了同样的错误...知道为什么吗?
我认为您需要强制训练集和测试集包含分类变量中的所有可能值。我不确定您的数据集的结构,但假设 lenkung
是您的土地变量。
一种解决方法是:
train_test = function(x,train_per=0.7){
smp_size = floor(train_per*nrow(x))
train_ind = sample(seq_len(nrow(x)),size = smp_size)
re = list()
re$train = x[train_ind,]
re$test = x[-train_ind,]
return(re)
}
splitted_data = split(data,data$lekung)
new_list = lapply(splitted_data,train_test)
这里我们定义了一个函数,将数据框 (x) 拆分为训练集和测试集。我们还使用 split()
函数将您的原始数据拆分为多个数据帧,其中每个数据帧仅包含 lekung 的一个可能值。假设这些值可以是 "A"、"B" 或 "C"。在那种情况下 splitted_data
将是一个包含 3 个数据框的列表,第一个包含 lekung = "A" 的所有观察结果,第二个包含 lekung = "B" 等的所有观察结果......
然后,我们将上面定义的函数应用于splitted_data
。现在 new_list
包含 lekung 的每个可能值的 2 个数据帧,一个火车和一个测试。
所以最后我们只需要将每个训练数据帧的行绑定在一起,并对测试数据帧执行相同的操作。
train = new_list[[1]][[1]]
test = new_list[[1]][[2]]
for(i in 2:length(a)){ # Then we use this loop to bind the data together
train = rbind(train,new_list[[i]][[1]])
test = rbind(test,new_list[[i]][[2]])
}
new_list
是 2 个数据帧列表的列表。所以我们使用 new_list[[1]]
来访问 lekung 的第一个值对应的 2 个数据帧和
new_list[[1]][[1]]
访问那里的第一个数据帧。
虽然可能有更好的方法来做到这一点。