随机森林R:;为什么打印预测不等于存储的预测

随机森林R:;为什么打印预测不等于存储的预测,r,machine-learning,classification,random-forest,R,Machine Learning,Classification,Random Forest,我正在运行随机森林模型来预测黑名单。我遇到了一个我自己无法解决的实际问题。 我的代码如下 library(xlsx) library(randomForest) setwd('E:\\laolai') rm(list=ls()) accepts <- read.xlsx('fuckdata.xlsx',sheetIndex = 1) set.seed(10) ind <- sample(2,nrow(accepts),replace=TRUE,prob=c(0.5,0.5)) tr

我正在运行随机森林模型来预测黑名单。我遇到了一个我自己无法解决的实际问题。 我的代码如下

library(xlsx)
library(randomForest) 
setwd('E:\\laolai')
rm(list=ls())
accepts <- read.xlsx('fuckdata.xlsx',sheetIndex = 1)
set.seed(10)
ind <- sample(2,nrow(accepts),replace=TRUE,prob=c(0.5,0.5))
train_data <- accepts[ind==1,]
test_data <- accepts[ind==2,]   
rf <- randomForest(formula,data=train_data,ntree=1500,na.action=na.roughfix)
plot(rf,main='randomforest')
print(rf)
source_3pred <- predict(rf,test_data)
plot(source_3pred,main='predict',xlab='trees',type='l')
价值观:

source_3pred Named num [1:1648] NA NA NA NA NA.......
然后


获取从2到3279的数据。例如3218 NA,3224 0.7609778。

随机森林软件包附带一个名为
重要性()
的函数,该函数可以为使用同一软件包构建的随机森林模型计算两个鲁棒性度量。第一种方法是将每棵树的预测精度与同一棵树进行比较,但使用随机排列的预测器。另一种方法使用基尼指数/残差平方和来测量树中每个分割节点杂质的减少

以下是如何访问随机林模型重要性的代码示例:

rf <- randomForest(formula, data=train_data, ntree=1500, na.action=na.roughfix,
                   importance=TRUE)
# for permutation measure of accuracy
importance(rf, type=1)

# for split measure of accuracy
importance(rf, type=2)

rf您似乎对随机林的工作方式有误解。与大多数其他机器学习方法不同,对于随机森林,我们没有明确使用数据的训练集。相反,我们在构建模型时包含所有数据。其原因是随机林在构建模型时隐含地交叉验证。因为第一,Python熊猫,然后,R,模型。多么恼人的Excel。所以,我认为从一开始就有单独的测试和训练数据是错误的,你应该只使用你的整个数据集。但是我如何测试模型呢?我的意思是,和性能。
print(source_3pred)
rf <- randomForest(formula, data=train_data, ntree=1500, na.action=na.roughfix,
                   importance=TRUE)
# for permutation measure of accuracy
importance(rf, type=1)

# for split measure of accuracy
importance(rf, type=2)