关于加快随机森林的建议

时间:2011-10-20 01:46:57

标签: r random-forest

我正在使用randomForest包进行一些工作,虽然它运行良好,但这可能非常耗时。任何人都有加快速度的建议吗?我正在使用带有双核AMD芯片的Windows 7机箱。我知道R不是多线程/处理器,但是如果任何并行包(rmpisnowsnowfall等)适用于randomForest,我很好奇东西。感谢。

编辑:

我正在使用rF进行某些分类工作(0和1)。数据有大约8-12个可变列,训练集是10k行的样本,因此它的体积适中但不疯狂。我正在运行500棵树和2,3或4的mtry。

编辑2: 这是一些输出:

> head(t22)
  Id Fail     CCUse Age S-TFail         DR MonInc #OpenLines L-TFail RE M-TFail Dep
1  1    1 0.7661266  45       2 0.80298213   9120         13       0  6       0   2
2  2    0 0.9571510  40       0 0.12187620   2600          4       0  0       0   1
3  3    0 0.6581801  38       1 0.08511338   3042          2       1  0       0   0
4  4    0 0.2338098  30       0 0.03604968   3300          5       0  0       0   0
5  5    0 0.9072394  49       1 0.02492570  63588          7       0  1       0   0
6  6    0 0.2131787  74       0 0.37560697   3500          3       0  1       0   1
> ptm <- proc.time()
> 
> RF<- randomForest(t22[,-c(1,2,7,12)],t22$Fail
+                    ,sampsize=c(10000),do.trace=F,importance=TRUE,ntree=500,,forest=TRUE)
Warning message:
In randomForest.default(t22[, -c(1, 2, 7, 12)], t22$Fail, sampsize = c(10000),  :
  The response has five or fewer unique values.  Are you sure you want to do regression?
> proc.time() - ptm
   user  system elapsed 
 437.30    0.86  450.97 
> 

4 个答案:

答案 0 :(得分:33)

foreach包的手册有一节关于平行随机森林 (Using The foreach Package,第5.1节):

> library("foreach")
> library("doSNOW")
> registerDoSNOW(makeCluster(4, type="SOCK"))

> x <- matrix(runif(500), 100)
> y <- gl(2, 50)

> rf <- foreach(ntree = rep(250, 4), .combine = combine, .packages = "randomForest") %dopar%
+    randomForest(x, y, ntree = ntree)
> rf
Call:
randomForest(x = x, y = y, ntree = ntree)
Type of random forest: classification
Number of trees: 1000

如果我们想要创建一个包含1000棵树的随机森林模型,而我们的计算机有四棵 核心,我们可以通过执行randomForest函数四次将问题分成四部分,ntree参数设置为250.当然,我们必须合并生成的randomForest个对象,但randomForest包附带了一个名为combine的函数。

答案 1 :(得分:8)

有两个“开箱即用”的选项可以解决这个问题。首先,插入符包包含一个方法'parRF',可以优雅地处理它。我通常使用这16个核心,效果很好。 randomShrubbery软件包还利用Revolution R上的多个内核进行RF。

答案 2 :(得分:5)

为什么不使用已经并行化和优化的随机森林实现?使用MPI查看SPRINT。 http://www.r-sprint.org/

答案 3 :(得分:5)

为什么你没有使用Python(即scikit-learn和多处理模块)来实现这个?使用joblib,我已经在相似大小的数据集上训练了随机森林,只花费了R的一小部分时间。即使没有多处理,随机森林在Python中也要快得多。以下是培训RF分类器并在Python中交叉验证的快速示例。您还可以轻松提取要素重要性并可视化树木。

import numpy as np
from sklearn.metrics import *
from sklearn.cross_validation import StratifiedKFold
from sklearn.ensemble import RandomForestClassifier

#assuming that you have read in data with headers
#first column corresponds to response variable 
y = data[1:, 0].astype(np.float)
X = data[1:, 1:].astype(np.float)

cm = np.array([[0, 0], [0, 0]])
precision = np.array([])
accuracy = np.array([])
sensitivity = np.array([])
f1 = np.array([])
matthews = np.array([])

rf = RandomForestClassifier(n_estimators=100, max_features = 5, n_jobs = 2)

#divide dataset into 5 "folds", where classes are equally balanced in each fold
cv = StratifiedKFold(y, n_folds = 5)
for i, (train, test) in enumerate(cv):
        classes = rf.fit(X[train], y[train]).predict(X[test])
        precision = np.append(precision, (precision_score(y[test], classes)))
        accuracy = np.append(accuracy, (accuracy_score(y[test], classes)))
        sensitivity = np.append(sensitivity, (recall_score(y[test], classes)))
        f1 = np.append(f1, (f1_score(y[test], classes)))
        matthews = np.append(matthews, (matthews_corrcoef(y[test], classes)))
        cm = np.add(cm, (confusion_matrix(y[test], classes)))

print("Accuracy: %0.2f (+/- %0.2f)" % (accuracy.mean(), accuracy.std() * 2))
print("Precision: %0.2f (+/- %0.2f)" % (precision.mean(), precision.std() * 2))
print("Sensitivity: %0.2f (+/- %0.2f)" % (sensitivity.mean(), sensitivity.std() * 2))
print("F1: %0.2f (+/- %0.2f)" % (f1.mean(), f1.std() * 2))
print("Matthews: %0.2f (+/- %0.2f)" % (matthews.mean(), matthews.std() * 2))
print(cm)