23

我正在使用该randomForest软件包进行一些工作,虽然它运行良好,但它可能很耗时。有人对加快速度有什么建议吗?我正在使用带有双核 AMD 芯片的 Windows 7 机器。我知道 R 不是多线程/处理器,但很好奇是否有任何并行包(、、、rmpisnowsnowfall适用于某些randomForest东西。谢谢。

编辑:

我正在使用 rF 进行一些分类工作(0 和 1)。数据有大约 8-12 个变量列,训练集是 10k 行的样本,所以它的大小合适但并不疯狂。我正在运行 500 棵树,mtry 为 2、3 或 4。

编辑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

4 回答 4

35

foreach包的手册有一个关于并行随机森林的部分(使用 foreach 包,第 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.

于 2011-10-20T06:17:03.510 回答
8

有两个“开箱即用”的选项可以解决这个问题。首先, caret 包包含一个方法“parRF”,可以优雅地处理这个问题。我通常将它与 16 核一起使用,效果很好。randomShrubbery 封装还利用了 Revolution R 上的 RF 多核。

于 2012-07-02T22:23:45.213 回答
5

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

于 2013-04-15T13:14:41.840 回答
5

您不使用 Python(即 scikit-learn 和多处理模块)来实现这一点有什么特别的原因吗?使用 joblib,我在类似大小的数据集上训练随机森林所需的时间只是 R 的一小部分。即使没有多处理,Python 中的随机森林也明显更快。这是一个在 Python 中训练 RF 分类器和交叉验证的快速示例。您还可以轻松提取特征重要性并可视化树。

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)
于 2013-09-24T07:39:42.037 回答