【发布时间】:2018-06-19 15:05:19
【问题描述】:
我有一个关于 rpart 和过拟合的问题。我的目标只是做好预测。我的数据集很大,将近 20000 个点。使用这些点中的大约 2.5% 作为训练,我得到了大约 50% 的预测误差。但是使用 97.5% 的数据作为训练,我得到了大约 30%。由于我使用大量数据进行训练,我想存在过度拟合的风险。
我使用随机训练/测试数据运行 1000 次 + 修剪树,如果我理解正确的话,这是一种交叉验证,我得到了非常稳定的结果(相同的预测误差和变量的重要性)。
即使我已经运行了 1000 次并且预测误差稳定,过拟合仍然是一个问题吗?
我还有一个关于解释变量之间相关性的问题。这可能是 CART 中的问题(与回归一样)吗?在回归中,我可能会使用套索来尝试修复相关性。如何修复与分类树的相关性?
当我绘制 cptree 时,我得到了这张图:
这是我正在运行的代码(我已经重复了 1000 次,每次都使用不同的随机拆分)。
set.seed(1) # For reproducability
train_frac = 0.975
n = dim(beijing_data)[1]
# Split into training and testing data
ii = sample(seq(1,dim(beijing_data)[1]),n*train_frac)
data_train = beijing_data[ii,]
data_test = beijing_data[-ii,]
fit = rpart(as.factor(PM_Dongsi_levels)~DEWP+HUMI+PRES+TEMP+Iws+
precipitation+Iprec+wind_dir+tod+pom+weekend+month+
season+year+day,
data = data_train, minsplit = 0, cp = 0)
plotcp(fit)
# Find the split with minimum CP and prune the tree
cp_fit = fit$cptable[which.min(fit$cptable[,"xerror"]),"CP"]
pfit = prune(fit, cp = cp_fit)
pp <- predict(pfit, newdata = data_test, type = "class")
err = sum(data_test[,"PM_Dongsi_levels"] != pp)/length(pp)
print(err)
链接到 beijing_data(作为 RData 文件,您可以复制我的示例) https://www.dropbox.com/s/6t3lcj7f7bqfjnt/beijing_data.RData?dl=0
【问题讨论】:
标签: r tree classification prediction rpart