Random Forest is an ensemble classifier of decision trees.
랜덤 포리스트는 디시젼 트리 앙상블 분류기이다.
Wikipedia has a nice explanation on the learning algorithm.
위키에 이 학습 알고리즘에 대한 좋은 설명이 있다.
The key idea is to use bagging which uses m models where each of which is fit from n’ samples picked from data of size n.
일정수의 샘플을 사용해서 일련의 모델을 만드는 배깅 방법이 핵심이다.
Plus, when choosing criteria for decision tree node splits, one feature is chosen from random subset of features.
그리고, 트리 노드의 분기를 위해서 필요한 기준 설정을 할때 무작위 추출된 데이터 부분 집합이 사용되는 것이 하나의 특성이다.
Each decision tree is fully grown (no pruning).
각각의 트리는 가지치기 없이 가능한 최대로 만들어진다.
When making a prediction, mode of predictions of all tress or average of them are used.
R has a randomForest package for it.
예측 실행시 모든 트리를 사용한 예측이나 트리들의 예측의 평균이 사용되며, R은 randomForest라는 패키지를 가지고 있다.
Here, we’ll use iris.
아이리스 데이터를 사용할 것이다.
Sepal.Length Sepal.Width Petal.Length Petal.Width Species |
Prepare training and test set.
데이터 셋을 먼저 준비함
> test = iris[ c (1:10, 51:60, 101:110), ] |
> train = iris[ c (11:50, 61:100, 111:150), ] |
Build a random forest using randomForest().
랜덤포리스트 함수를 사용해서 랜덤 포리스트를 생성한다.
> r = randomForest (Species ~., data=train, importance= TRUE , do.trace=100) |
100: 5.83% 0.00% 7.50% 10.00% |
200: 5.83% 0.00% 10.00% 7.50% |
300: 5.83% 0.00% 10.00% 7.50% |
400: 5.00% 0.00% 10.00% 5.00% |
500: 5.83% 0.00% 10.00% 7.50% |
*** 주석: 함수를 사용한다면 일반적인 트리 생성에서와차이나는 부분은 trace에 대해 조건을 주는 부분만 다르다.
|
randomForest (formula = Species ~ ., data = train, importance = TRUE , do.trace = 100) |
Type of random forest: classification |
No. of variables tried at each split: 2 |
OOB estimate of error rate: 5.83% |
setosa versicolor virginica class.error |
We see that predicting setosa works great. Let’s see how it works with test set.
setosa가 제대로 예측되고 있음을 볼 수 있다.
테스트 셋을 사용해서 얼마나 잘 되었는지를 살펴보자.
> iris.predict = predict (r, test) |
setosa setosa setosa setosa setosa setosa setosa |
setosa setosa setosa versicolor versicolor versicolor versicolor |
versicolor versicolor versicolor versicolor versicolor versicolor virginica |
102 103 104 105 106 107 108 |
virginica virginica virginica virginica virginica versicolor virginica |
Levels: setosa versicolor virginica |
> t = table (observed=test[, 'Species' ], predict=iris.predict) |
observed setosa versicolor virginica |
observed setosa versicolor virginica |
As you can see, 10% of virginica was predicted as versicolor.
References)
보시다시피, virginica 10%가 versicolr로 예측되었다.
Package ‘randomForest’, Briedman and Cutler’s random forests for classification and regression.: reference manual.
Andy Liaw and Matthew Wiener, Classification and Regression by randomForest, R News, pp. 18-22.: good examples on classification, regression and clustering(really!), and some practical advice.