xbs*_*bsd 4 r random-forest r-caret
我注意到在训练中使用配方和非配方方法会产生不同的结果.此外,配方方法所花费的时间几乎是非配方方法所用时间的10倍.这是预期的吗?
> z <- data.table(c1=sample(1:1000,1000, replace=T), c2=as.factor(sample(LETTERS, 1000, replace=T)))
# SYSTEM TIME WITH FORMULA METHOD
# -------------------------------
> system.time(r <- train(c1 ~ ., z, method="rf", importance=T))
user system elapsed
376.233 9.241 18.190
> r
1000 samples
1 predictors
No pre-processing
Resampling: Bootstrap (25 reps)
Summary of sample sizes: 1000, 1000, 1000, 1000, 1000, 1000, ...
Resampling results across tuning parameters:
mtry RMSE Rsquared RMSE SD Rsquared SD
2 295 0.00114 4.94 0.00154
13 300 0.00113 5.15 0.00151
25 300 0.00111 5.16 0.00146
RMSE was used to select the optimal model using the smallest value.
The final value used for the model was mtry = 2.
# SYSTEM TIME WITH NON-FORMULA METHOD
# -------------------------------
> system.time(r <- train(z[,2,with=F], z$c1, method="rf", importance=T))
user system elapsed
34.984 2.977 2.708
Warning message:
In randomForest.default(trainX, trainY, mtry = tuneValue$.mtry, :
invalid mtry: reset to within valid range
> r
1000 samples
1 predictors
No pre-processing
Resampling: Bootstrap (25 reps)
Summary of sample sizes: 1000, 1000, 1000, 1000, 1000, 1000, ...
Resampling results
RMSE Rsquared RMSE SD Rsquared SD
297 0.00152 6.67 0.00197
Tuning parameter 'mtry' was held constant at a value of 2
Run Code Online (Sandbox Code Playgroud)
top*_*epo 16
您有一个具有中等数量级别的分类预测器.当使用式接口,大多数建模功能(包括train,lm,glm,等等)在内部运行model.matrix来处理数据集.这将从任何因子变量创建虚拟变量.非公式接口不[1].
使用虚拟变量时,任何拆分中只使用一个因子级别.树方法以不同方式处理分类预测变量,但是,当不使用虚拟变量时,随机森林将根据结果对因子预测变量进行排序,并找到因子水平的双向分割[2].这需要更多时间.
马克斯
[1]我不想成为那些说"在我的书中我展示......"的人之一,但在这种情况下,我会.图14.2很好地说明了CART树的这个过程.
[2]上帝,我又在做了.第14.1节讨论了树的因子的不同表示,并且第14.7节中显示了一种数据集的两种方法之间的比较.