R与Matlab之间的随机森林差异&;python
我将三种不同编程语言中的随机森林算法应用于相同的伪样本数据集(1000 obs,二进制1/0因变量,10个数值解释变量):R与Matlab之间的随机森林差异&;python,python,r,matlab,machine-learning,random-forest,Python,R,Matlab,Machine Learning,Random Forest,我将三种不同编程语言中的随机森林算法应用于相同的伪样本数据集(1000 obs,二进制1/0因变量,10个数值解释变量): Matlab2015a(与2012a相同),使用“Treebagger”命令(统计和机器学习工具箱的一部分) R使用“随机森林”软件包: Python使用sklearn.ensemble中的“RandomForestClassifier”: 我还试图在编程语言中保持所有模型参数的一致性(树的数量、整个样本的引导抽样、在每次分割时随机抽样作为候选变量的变量数量、衡量分割质量的
b = TreeBagger(1000,X,Y, 'FBoot',1, 'NVarToSample',4, 'MinLeaf',1, 'Method', 'classification','Splitcriterion', 'gdi')
[~,scores,~] = predict(b,X);
clf = RandomForestClassifier(n_estimators=1000, max_features=4, bootstrap=True)
scores_fit = clf.fit(X, Y)
scores = pd.DataFrame(clf.predict_proba(X))
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)
Python:
b = TreeBagger(1000,X,Y, 'FBoot',1, 'NVarToSample',4, 'MinLeaf',1, 'Method', 'classification','Splitcriterion', 'gdi')
[~,scores,~] = predict(b,X);
clf = RandomForestClassifier(n_estimators=1000, max_features=4, bootstrap=True)
scores_fit = clf.fit(X, Y)
scores = pd.DataFrame(clf.predict_proba(X))
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:
b = TreeBagger(1000,X,Y, 'FBoot',1, 'NVarToSample',4, 'MinLeaf',1, 'Method', 'classification','Splitcriterion', 'gdi')
[~,scores,~] = predict(b,X);
clf = RandomForestClassifier(n_estimators=1000, max_features=4, bootstrap=True)
scores_fit = clf.fit(X, Y)
scores = pd.DataFrame(clf.predict_proba(X))
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)
results.rf当您在R
中对randomForest
对象调用predict
而不提供数据集时,它将返回出袋预测。在其他方法中,再次传递训练数据。我怀疑如果你在R版本中这样做,你的概率将是相似的:
scores <- predict(results.rf, X, type="prob",
norm.votes=FALSE, predict.all=FALSE, proximity=FALSE, nodes=FALSE)
得分非常感谢,我实施了您建议的解决方案,结果显著改善。准确地说,这三个软件包产生的结果基本相同。无论如何,我有一个快速跟进的问题:有人能解释一下R&Python与Matlab在计算时间上的巨大差异吗?R&Python一方面是即时获得结果,另一方面是Matlab,模型估计和预测都需要几秒钟的时间。R实现是用Fortran编写的。我相信scikit版本是Cython。我怀疑Matlab版本是纯Matlab?@Zelazny7-R实现实际上是用C(源代码:)编写的,尽管它是基于Breiman/Cutler最初的Fortran代码