randomForest没有预测连续样本

时间:2014-10-05 08:58:37

标签: r random-forest

我有data.frame TC,有13个变量的17744个观测值。最后一个变量是target:a Factor w/ 2 levels "0", "1"

我做:

n.col <- ncol(TC)

x.train.or <- TC[1:12000, -n.col]
y.train.or <- TC[1:12000, n.col]
x.test.or <- TC[12000:17000, -n.col]
y.test.or <- TC[12000:17000, n.col]
rf.or <- randomForest(y=y.train.or, x=x.train.or, ntree=500, mtry=5, 
                      importance=TRUE, keep.forest=TRUE,
                      na.action=na.roughfix, replace=FALSE)
pr.or <- predict(rf.or, x.test.or)
table(y.test.or, pr.or, dnn=c("Actual", "Predicted"))

#       Predicted
# Actual    0      1
#      0    2424  780
#      1    1056  741

非常糟糕的结果。

然后我用随机样本重复模型拟合:

set.seed <- 123
t.t <- holdout(TC[, n.col], ratio=3/5, mode = "random")
x.train.r <- TC[t.t$tr, - (n.col)]
y.train.r <- TC[t.t$tr, (n.col)] 
x.test.r <- TC[t.t$ts, - (n.col)]
rf.r <- randomForest(y=y.train.r, x=x.train.r, ntree=500, mtry=5,
                     importance=TRUE,  keep.forest=TRUE,    
                     na.action=na.roughfix, replace=FALSE)
pr.r <- predict(rf.r, x.test.r)
table(y.test.r, pr.r, dnn=c("Actual", "Predicted"))

#         Predicted
# Actual    0      1
# 0         4274  215
# 1         353   2257

非常好的结果,但取决于形成一个数据集的样本的方法。 我解决的问题只假设是连续样本。

拜托,帮助我!

回答问题: (1)当然可以:

library(randomForest)
library(rminer) 

(3)我再说一遍:

n.col           <-  ncol(TC)
x.train.or      <-  TC[1:12000, -n.col]
y.train.or      <-  TC[1:12000, n.col]
x.test.or       <-  TC[12001:17000, -n.col]
y.test.or       <-  TC[12001:17000, n.col]

并收到同样可怕的结果

      Predicted
Actual    0    1
     0 2413  790
     1 1049  748

(4)可能有问题吗?有些变量在[1:17000]上是随机的,但在[1:100]上不是随机的 (我没有绘画的权利)。

在这种情况下该怎么办?

1 个答案:

答案 0 :(得分:0)

首先,在不知道数据状态的情况下,它有点难以回答。如果观察以某种方式重复观察,有时您可以将您的测试集包括在您的火车组中。

验证结果的最佳方法之一是使用某种交叉验证技术,注意确保将测试和训练集完全分开。以下是一个值得观看的好视频。

http://vimeo.com/75432414

相关问题