训练样本大小和测试预测的差异
Difference in size of training sample size and test prediction
我用 2120x10 的样本训练了一个模型。现在我试图将相同的模型应用于测试数据集,但我在推导混淆矩阵时遇到了问题。
test_predictions <- predict(train_obj, test_data)
test_predictions <- ifelse(test_predictions > 5, 1, 0)
confusionMatrix(as.factor(test_predictions), test_data$outcome, positive="1")
我在计算混淆矩阵时出错,因为 test_data$outcome
有 2135 个值。如果我使用 test_data$outcome[1:2120]
,一切正常。
有没有更好的方法在不限制值个数的情况下计算混淆矩阵?限制 test_data$outcome
中值的数量是否正确?
听起来不对。如果 test_data
只有 2120 行,test_data$outcome
怎么会有 2135 个值?即使 test_data 的预测器中有 NA,它们也会被预测为 NA,然后被 confusionMatrix
.
忽略
dat=data.frame(a=rnorm(1000), b=rnorm(1000))
dat=dat %>%
mutate(c=5*(a+b)) %>%
mutate(d=ifelse(c>5, 1, 0))
set.seed(1)
i=sample(1:1000, 750, replace=FALSE)
train_data=dat[i,]
test_data=dat[-i,]
test_data[sample(1:250, 3),1:2]=NA
lr=lm(c ~ a + b, data=train_data)
test_predictions=predict(lr, test_data)
test_predictions=ifelse(test_predictions>5, 1, 0)
confusionMatrix(test_predictions, test_data$d)
Reference
Prediction 0 1
0 187 0
1 0 60
我用 2120x10 的样本训练了一个模型。现在我试图将相同的模型应用于测试数据集,但我在推导混淆矩阵时遇到了问题。
test_predictions <- predict(train_obj, test_data)
test_predictions <- ifelse(test_predictions > 5, 1, 0)
confusionMatrix(as.factor(test_predictions), test_data$outcome, positive="1")
我在计算混淆矩阵时出错,因为 test_data$outcome
有 2135 个值。如果我使用 test_data$outcome[1:2120]
,一切正常。
有没有更好的方法在不限制值个数的情况下计算混淆矩阵?限制 test_data$outcome
中值的数量是否正确?
听起来不对。如果 test_data
只有 2120 行,test_data$outcome
怎么会有 2135 个值?即使 test_data 的预测器中有 NA,它们也会被预测为 NA,然后被 confusionMatrix
.
dat=data.frame(a=rnorm(1000), b=rnorm(1000))
dat=dat %>%
mutate(c=5*(a+b)) %>%
mutate(d=ifelse(c>5, 1, 0))
set.seed(1)
i=sample(1:1000, 750, replace=FALSE)
train_data=dat[i,]
test_data=dat[-i,]
test_data[sample(1:250, 3),1:2]=NA
lr=lm(c ~ a + b, data=train_data)
test_predictions=predict(lr, test_data)
test_predictions=ifelse(test_predictions>5, 1, 0)
confusionMatrix(test_predictions, test_data$d)
Reference
Prediction 0 1
0 187 0
1 0 60