Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R与Matlab之间的随机森林差异&;python_Python_R_Matlab_Machine Learning_Random Forest - Fatal编程技术网

R与Matlab之间的随机森林差异&;python

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”: 我还试图在编程语言中保持所有模型参数的一致性(树的数量、整个样本的引导抽样、在每次分割时随机抽样作为候选变量的变量数量、衡量分割质量的

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

  • Matlab2015a(与2012a相同),使用“Treebagger”命令(统计和机器学习工具箱的一部分)
  • R使用“随机森林”软件包:
  • Python使用sklearn.ensemble中的“RandomForestClassifier”
  • 我还试图在编程语言中保持所有模型参数的一致性(树的数量、整个样本的引导抽样、在每次分割时随机抽样作为候选变量的变量数量、衡量分割质量的标准)

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

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

    我猜在R中有一些不同的默认模型参数,我不知道,或者在底层的randomForest包中硬编码

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

    Matlab:

     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代码