R 和 Matlab 之间的随机森林差异 & Python

Random Forest discrepancy between R and Matlab & Python

我将三种不同编程语言的运行dom森林算法应用于同一个伪样本数据集(1000个obs,二进制1/0因变量,10个数值解释变量):

  1. Matlab 2015a(与 2012a 相同)使用 "Treebagger" 命令(统计和机器学习工具箱的一部分)
  2. R 使用 "randomForest" 包:https://cran.r-project.org/web/packages/randomForest/index.html
  3. Python 使用 sklearn.ensemble 中的 "RandomForestClassifier"http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html

我还尝试在所有编程语言中保持所有模型参数相同(树的数量,bootstrap 整个样本的抽样,变量的数量 运行domly 作为每个样本的候选样本拆分,衡量拆分质量的标准)。

虽然 Matlab 和 Python 产生的结果基本相同(即概率),但 R 的结果却大不相同。

一方面是 R 生成的结果与另一方面是 Matlab & Python 生成的结果之间存在差异的可能原因是什么?

我想有一些默认模型参数在 R 中有所不同,我不知道或者在底层 运行domForest 包中硬编码。

我 运行 的确切代码如下所示:

Matlab:

 b = TreeBagger(1000,X,Y, 'FBoot',1, 'NVarToSample',4, 'MinLeaf',1, 'Method', 'classification','Splitcriterion', 'gdi')
 [~,scores,~] = predict(b,X);

Python:

 clf = RandomForestClassifier(n_estimators=1000, max_features=4, bootstrap=True)
 scores_fit = clf.fit(X, Y)
 scores = pd.DataFrame(clf.predict_proba(X))

R:

 results.rf <- randomForest(X,Y,  ntree=1000, type = "classification", sampsize = length(Y),replace=TRUE,mtry=4)
 scores <- predict(results.rf, type="prob",
    norm.votes=FALSE, predict.all=FALSE, proximity=FALSE, nodes=FALSE)

当您在 R 中的 randomForest 对象上调用 predict 而不提供数据集时,它会 returns out-of-bag 预测。在您的其他方法中,您将再次传递训练数据。我怀疑如果你在 R 版本中这样做,你的概率会相似:

 scores <- predict(results.rf, X, type="prob",
    norm.votes=FALSE, predict.all=FALSE, proximity=FALSE, nodes=FALSE)

另请注意,如果您想要无偏概率,则返回 OOB 预测的 R 方法是对训练数据进行预测时的最佳方法。