【发布时间】:2017-02-20 05:54:08
【问题描述】:
在 QDA(二次判别分析)中,我需要保持训练和测试数据的长度完全相同吗?如果没有,在这种情况下如何找到混淆矩阵?
这里是psuedo data。
因为如果我保留不同长度的训练数据和测试数据集,则会出现错误(使用 R Studio): “表中的错误(pred,true):所有参数必须具有相同的长度”。
尝试在两个数据集以及 pred 和 true 上使用 na.omit() 删除 NA;并为 qda() 使用 na.action = na.exclude,但它不起作用。
将数据集精确地分成两半后;一半作为训练,一半作为测试;它在 pred 和 true 上的 na.omit() 之后完美运行。
以下是用于任一方法的代码。在方法 2 中,将数据分成相等的两半,它工作得非常好。
#Approach 1: divide data age-wise
train <- vif_data$Age < 30
# there are around 400 values passing (TRUE) above condition and around 50 failing (FALSE)
train_vif <- vif_data[train,]
test_vif <- vif_data[!train,]
#taking QDA
zone_qda <- qda(train_vif$Awareness~train_vif$Zone, na.action = na.exclude)
#compare QDA against test data
zone_pred <- predict(zone_qda, test_vif)
#omitting nulls
pred <- na.omit(zone_pred$class)
true <- na.omit(test_vif$Awareness)
length(pred) # result: 399
length(true) # result: 47
#that's where it throws error: "Error in table(zone_pred$class, train_vif) : all arguments must have the same length"
zone_aware <- table(zone_pred$class, train_vif)
# OR
zone_aware <- table(pred, true)
accur <- mean(zone_pred$class==test_vif$Awareness)
###############################
#Approach 2: divide data into random halves
train <- splitSample(dataset = vif_data, div = 2, path = "./", type = "csv")
train_data <- read.csv("splitSample_s1.csv")
test_data <- read.csv("splitSample_s2.csv")
#taking QDA
zone_qda <- qda(train_vif$Awareness~train_vif$Zone, na.action = na.exclude)
#compare QDA against test data
zone_pred <- predict(zone_qda, test_vif)
#omitting nulls
pred <- na.omit(zone_pred$class)
true <- na.omit(test_vif$Awareness)
length(train_vif)
# this works fine
zone_aware <- table(zone_pred$class, train_vif)
# OR
zone_aware <- table(pred, true)
accur <- mean(zone_pred$class==test_vif$Awareness)
想知道是否有任何方法可以得到一个混淆矩阵,其中数据集不均等地分为训练数据集和测试数据集。
谢谢!
【问题讨论】:
标签: r statistics classification logistic-regression data-science