随机林库:R和Python中的不同结果
下面的代码用R和python训练一个随机森林模型。正如您所注意到的,r1-0.27=0.73中的精度比python0.69中的精度更好。此外,特性的重要性在R和Python中是不同的 [编辑]是否有任何方法可以在python中复制R结果,或者有些事情是无法控制的?两个库中的一些可调参数不同,这使得很难进行匹配 其他人从Pyton和R的随机森林中得到不同的结果吗?区别是什么 R代码: 输出:随机林库:R和Python中的不同结果,python,r,random-forest,Python,R,Random Forest,下面的代码用R和python训练一个随机森林模型。正如您所注意到的,r1-0.27=0.73中的精度比python0.69中的精度更好。此外,特性的重要性在R和Python中是不同的 [编辑]是否有任何方法可以在python中复制R结果,或者有些事情是无法控制的?两个库中的一些可调参数不同,这使得很难进行匹配 其他人从Pyton和R的随机森林中得到不同的结果吗?区别是什么 R代码: 输出: Call: randomForest(formula = admit ~ gre + gpa + ra
Call:
randomForest(formula = admit ~ gre + gpa + rank, data = mydata,
ntree = 1000, importance = TRUE, replace = TRUE)
Type of random forest: classification
Number of trees: 1000
No. of variables tried at each split: 1
OOB estimate of error rate: 28.5%
Confusion matrix:
0 1 class.error
0 254 19 0.06959707
1 95 32 0.74803150
0 1 MeanDecreaseAccuracy MeanDecreaseGini
gre 0.01741400 0.01209596 0.01566284 31.45256
gpa 0.02565179 0.02467486 0.02527394 43.32355
rank 0.02570388 0.04844323 0.03283692 18.15780
RandomForestClassifier(bootstrap=True,
class_weight=None, criterion='gini',
max_depth=None, max_features='auto', max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=1000, n_jobs=1,
oob_score=True, random_state=None, verbose=0,
warm_start=False)
0.6925
>>> >>> >>> array([[229, 44],
[ 79, 48]])
array([ 0.34573918, 0.53783772, 0.11642309])
Python代码
输出:
Call:
randomForest(formula = admit ~ gre + gpa + rank, data = mydata,
ntree = 1000, importance = TRUE, replace = TRUE)
Type of random forest: classification
Number of trees: 1000
No. of variables tried at each split: 1
OOB estimate of error rate: 28.5%
Confusion matrix:
0 1 class.error
0 254 19 0.06959707
1 95 32 0.74803150
0 1 MeanDecreaseAccuracy MeanDecreaseGini
gre 0.01741400 0.01209596 0.01566284 31.45256
gpa 0.02565179 0.02467486 0.02527394 43.32355
rank 0.02570388 0.04844323 0.03283692 18.15780
RandomForestClassifier(bootstrap=True,
class_weight=None, criterion='gini',
max_depth=None, max_features='auto', max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=1000, n_jobs=1,
oob_score=True, random_state=None, verbose=0,
warm_start=False)
0.6925
>>> >>> >>> array([[229, 44],
[ 79, 48]])
array([ 0.34573918, 0.53783772, 0.11642309])
我在上发现了一个类似的问题,它链接到不同的基准…与许多机器学习模型一样,训练随机森林模型的过程高度依赖于模型的超参数值,以及初始随机种子,这些种子将影响训练期间做出的随机选择
在您的例子中,我假设python和R库中选择的默认超参数是不同的,从而导致模型的不同行为。通过手动将所有超参数设置为相等,可以测试行为之间是否确实存在差异。我猜这之后剩下的差异可能是由于数值问题,或者是由于训练中的随机性,在同一数据上训练的不同随机林永远不会相同。所以你的问题是。。。?您想知道为什么会有这种差异吗?看起来randomForest默认值比sklearn默认值更适用于此特定数据。我不会再多读了。