2017-03-04 7 views
2

모두, 나는 최고의 매개 변수를 검색하여 루프를 시도하고있다. 그러나 그 결과는 정말 혼란 스럽습니다. 다음 코드는 "mtry"매개 변수가 동일하므로 동일한 결과를 제공해야합니다. 최고의 임의의 포리 스트 매개 변수에 대한 검색에 대한 r


for(i in c(2)){ 
    RFModel = randomForest(Churn ~ ., 
        data = Trainingds, 
        ntree = 30, 
        mtry = i, 
        importance = TRUE, 
        replace = FALSE) 
    print(RFModel$confusion) 
} 

    No Yes class.error 
No 3 2   0.4 
Yes 2 3   0.4 

  1. 코드 1 및 CODE2
    RFModel = randomForest(Churn ~ ., 
            data = ggg, 
            ntree = 30, 
            mtry = 2, 
            importance = TRUE, 
            replace = FALSE) 
    print(RFModel$confusion) 
    
        No Yes class.error 
    No 4 1   0.2 
    Yes 1 4   0.2 
    
     gender Partner tenure Churn 
    3521  Male  No 0.992313 Yes 
    2525.1 Male  No 4.276666 No 
    567  Male  Yes 2.708050 No 
    8381 Female  No 4.202127 Yes 
    6258 Female  No 0.000000 Yes 
    6569  Male  Yes 2.079442 No 
    27410 Female  No 1.550804 Yes 
    6429 Female  No 1.791759 Yes 
    412 Female  Yes 3.828641 No 
    4655 Female  Yes 3.737670 No 
    

    는 같은 출력을 제공한다.
+0

는이'임의 randomForest'의 결과가 아닌가? –

답변

2

임의성이 알고리즘에 내장되어 있기 때문에 매번 약간 다른 결과가 나옵니다. 각 트리를 구축하기 위해 알고리즘은 데이터 프레임을 다시 샘플링하고 무작위로 mtry 열을 선택하여 리샘플링 된 데이터 프레임에서 트리를 만듭니다. 동일한 매개 변수 (예 : mtry, ntree)로 작성된 모델이 매번 동일한 결과를 제공하도록하려면 임의 시드를 설정해야합니다.

예를 들어, randomForest을 10 회 실행하고 각 실행에서 평균 제곱 오류의 평균을 확인하십시오. 평균 MSE마다 다르다는 점을 참고 : 위의 코드를 실행하면

library(randomForest) 

replicate(10, mean(randomForest(mpg ~ ., data=mtcars)$mse)) 
[1] 5.998530 6.307782 5.791657 6.125588 5.868717 5.845616 5.427208 6.112762 5.777624 6.150021 

, 당신은 위의 값과 다른 또 다른 10의 값을 얻을 수 있습니다.

주어진 모델이 동일한 매개 변수 (예 : mtryntree)로 실행되는 결과를 재현하려면 임의의 시드를 설정할 수 있습니다. 예를 들어 :

set.seed(5) 
mean(randomForest(mpg ~ ., data=mtcars)$mse) 
[1] 6.017737 

당신은 동일한 시드 값을 사용하는 경우 동일한 결과를 얻을 수 있지만, 그렇지 않으면 다른 결과 것입니다. ntree의 더 큰 값을 사용하면 모델 실행 사이의 가변성은 감소하지만 제거되지는 않습니다.

업데이트 : 제공 한 데이터 샘플을 사용하여 코드를 실행할 때 매번 동일한 결과가 나오는 것은 아닙니다. 데이터 프레임 결과 replace=TRUE은 교체하지 않고 샘플링되고 심지어 함께, 열마다 다를 수 있습니다 트리를 구축하기 위해 선택 :

> randomForest(Churn ~ ., 
+    data = ggg, 
+    ntree = 30, 
+    mtry = 2, 
+    importance = TRUE, 
+    replace = FALSE) 

Call: 
randomForest(formula = Churn ~ ., data = ggg, ntree = 30, mtry = 2,  importance = TRUE, replace = FALSE) 
       Type of random forest: classification 
        Number of trees: 30 
No. of variables tried at each split: 2 

     OOB estimate of error rate: 30% 
Confusion matrix: 
    No Yes class.error 
No 3 2   0.4 
Yes 1 4   0.2 
> randomForest(Churn ~ ., 
+    data = ggg, 
+    ntree = 30, 
+    mtry = 2, 
+    importance = TRUE, 
+    replace = FALSE) 

Call: 
randomForest(formula = Churn ~ ., data = ggg, ntree = 30, mtry = 2,  importance = TRUE, replace = FALSE) 
       Type of random forest: classification 
        Number of trees: 30 
No. of variables tried at each split: 2 

     OOB estimate of error rate: 20% 
Confusion matrix: 
    No Yes class.error 
No 4 1   0.2 
Yes 1 4   0.2 
> randomForest(Churn ~ ., 
+    data = ggg, 
+    ntree = 30, 
+    mtry = 2, 
+    importance = TRUE, 
+    replace = FALSE) 

Call: 
randomForest(formula = Churn ~ ., data = ggg, ntree = 30, mtry = 2,  importance = TRUE, replace = FALSE) 
       Type of random forest: classification 
        Number of trees: 30 
No. of variables tried at each split: 2 

     OOB estimate of error rate: 30% 
Confusion matrix: 
    No Yes class.error 
No 3 2   0.4 
Yes 1 4   0.2 

다음과 결과의 유사한 세트의 내장 iris 데이터 프레임 :

> randomForest(Species ~ ., data=iris, ntree=30, mtry=2, importance = TRUE, 
+    replace = FALSE) 

Call: 
randomForest(formula = Species ~ ., data = iris, ntree = 30,  mtry = 2, importance = TRUE, replace = FALSE) 
       Type of random forest: classification 
        Number of trees: 30 
No. of variables tried at each split: 2 

     OOB estimate of error rate: 3.33% 
Confusion matrix: 
      setosa versicolor virginica class.error 
setosa   50   0   0  0.00 
versicolor  0   47   3  0.06 
virginica  0   2  48  0.04 
> randomForest(Species ~ ., data=iris, ntree=30, mtry=2, importance = TRUE, 
+    replace = FALSE) 

Call: 
randomForest(formula = Species ~ ., data = iris, ntree = 30,  mtry = 2, importance = TRUE, replace = FALSE) 
       Type of random forest: classification 
        Number of trees: 30 
No. of variables tried at each split: 2 

     OOB estimate of error rate: 4.67% 
Confusion matrix: 
      setosa versicolor virginica class.error 
setosa   50   0   0  0.00 
versicolor  0   47   3  0.06 
virginica  0   4  46  0.08 
> randomForest(Species ~ ., data=iris, ntree=30, mtry=2, importance = TRUE, 
+    replace = FALSE) 

Call: 
randomForest(formula = Species ~ ., data = iris, ntree = 30,  mtry = 2, importance = TRUE, replace = FALSE) 
       Type of random forest: classification 
        Number of trees: 30 
No. of variables tried at each split: 2 

     OOB estimate of error rate: 6% 
Confusion matrix: 
      setosa versicolor virginica class.error 
setosa   50   0   0  0.00 
versicolor  0   47   3  0.06 
virginica  0   6  44  0.12 

각 모델 실행에 의해 생성 된 트리를 볼 수도 있으며 일반적으로 달라집니다. 예를 들어, 다음 코드를 세 번 실행하여 결과를 객체 m1, m2m3에 저장한다고 가정 해보십시오.

randomForest(Churn ~ ., 
      data = ggg, 
      ntree = 30, 
      mtry = 2, 
      importance = TRUE, 
      replace = FALSE) 

이제 각 모델 객체의 처음 네 개의 트리를 아래에서 붙여 보겠습니다. 출력은 목록입니다. 첫 번째 트리가 각 모델 실행마다 다릅니다. 두 번째 트리는 처음 두 모델 실행에서는 동일하지만 세 번째 모델에서는 다르다.

check.trees = lapply(1:4, function(i) { 
    lapply(list(m1=m1,m2=m2,m3=m3), function(model) getTree(model, i, labelVar=TRUE)) 
    }) 

check.trees 
[[1]] 
[[1]]$m1 
    left daughter right daughter split var split point status prediction 
1    2    3 Partner 1.000000  1  <NA> 
2    4    5 gender 1.000000  1  <NA> 
3    0    0  <NA> 0.000000  -1   No 
4    0    0  <NA> 0.000000  -1  Yes 
5    6    7 tenure 2.634489  1  <NA> 
6    0    0  <NA> 0.000000  -1  Yes 
7    0    0  <NA> 0.000000  -1   No 

[[1]]$m2 
    left daughter right daughter split var split point status prediction 
1    2    3 gender 1.000000  1  <NA> 
2    0    0  <NA> 0.000000  -1  Yes 
3    4    5 tenure 1.850182  1  <NA> 
4    0    0  <NA> 0.000000  -1  Yes 
5    0    0  <NA> 0.000000  -1   No 

[[1]]$m3 
    left daughter right daughter split var split point status prediction 
1    2    3 tenure 2.249904  1  <NA> 
2    0    0  <NA> 0.000000  -1  Yes 
3    0    0  <NA> 0.000000  -1   No 


[[2]] 
[[2]]$m1 
    left daughter right daughter split var split point status prediction 
1    2    3 Partner   1  1  <NA> 
2    0    0  <NA>   0  -1  Yes 
3    0    0  <NA>   0  -1   No 

[[2]]$m2 
    left daughter right daughter split var split point status prediction 
1    2    3 Partner   1  1  <NA> 
2    0    0  <NA>   0  -1  Yes 
3    0    0  <NA>   0  -1   No 

[[2]]$m3 
    left daughter right daughter split var split point status prediction 
1    2    3 Partner   1  1  <NA> 
2    4    5 gender   1  1  <NA> 
3    0    0  <NA>   0  -1   No 
4    0    0  <NA>   0  -1  Yes 
5    0    0  <NA>   0  -1   No 


[[3]] 
[[3]]$m1 
    left daughter right daughter split var split point status prediction 
1    2    3 Partner   1  1  <NA> 
2    4    5 gender   1  1  <NA> 
3    0    0  <NA>   0  -1   No 
4    0    0  <NA>   0  -1  Yes 
5    0    0  <NA>   0  -1  Yes 

[[3]]$m2 
    left daughter right daughter split var split point status prediction 
1    2    3 Partner   1  1  <NA> 
2    0    0  <NA>   0  -1  Yes 
3    0    0  <NA>   0  -1   No 

[[3]]$m3 
    left daughter right daughter split var split point status prediction 
1    2    3 tenure 2.129427  1  <NA> 
2    0    0  <NA> 0.000000  -1  Yes 
3    0    0  <NA> 0.000000  -1   No 


[[4]] 
[[4]]$m1 
    left daughter right daughter split var split point status prediction 
1    2    3 tenure 1.535877  1  <NA> 
2    0    0  <NA> 0.000000  -1  Yes 
3    4    5 tenure 4.015384  1  <NA> 
4    0    0  <NA> 0.000000  -1   No 
5    6    7 tenure 4.239396  1  <NA> 
6    0    0  <NA> 0.000000  -1  Yes 
7    0    0  <NA> 0.000000  -1   No 

[[4]]$m2 
    left daughter right daughter split var split point status prediction 
1    2    3 Partner   1  1  <NA> 
2    0    0  <NA>   0  -1  Yes 
3    0    0  <NA>   0  -1   No 

[[4]]$m3 
    left daughter right daughter split var split point status prediction 
1    2    3 Partner   1  1  <NA> 
2    0    0  <NA>   0  -1  Yes 
3    0    0  <NA>   0  -1   No 
+0

하지만 첫 번째 코드를 10 번 실행하면 동일한 혼란 행렬을 얻게됩니다. – Frasher

+0

코드와 함께 작동하는 샘플 데이터를 제공하고 문제를 재현하십시오. 'dput'을 사용하여 데이터 샘플을 제공하십시오. – eipi10

+0

당신은 완전히 옳습니다. 귀하의 답변에 정말 감사드립니다. 일단 code1의 첫 번째 줄과 code2의 for 루프 안에 set.seed()를 추가하면 동일한 결과를 얻습니다. 정말 고마워. – Frasher