"R 랜덤 포레스트"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
(다른 사용자 한 명의 중간 판 21개는 보이지 않습니다)
2번째 줄: 2번째 줄:
;R Random Forest
;R Random Forest
;R 랜덤 포레스트
;R 랜덤 포레스트
* [[R randomForest()|randomForest()]] 또는 [[R cforest()|cforest()]] 사용


<source lang='r' run>
==randomForest()==
<syntaxhighlight lang='r' run>
set.seed(42) # 랜덤값 고정
set.seed(42) # 랜덤값 고정
library(rpart) # stagec
data(stagec, package='rpart')
df = stagec
df = stagec
df = na.omit(df) # 결측치 제거
df = na.omit(df) # 결측치 제거
15번째 줄: 17번째 줄:
testData  = df[-idx,]
testData  = df[-idx,]


# 모델
# 모델 적합
library(randomForest, quietly=T)
library(randomForest, warn.conflict=F)
model = randomForest(ploidy ~ ., trainData, ntree=100, proximity=T)
model = randomForest(ploidy ~ ., trainData, ntree=100, proximity=T)


26번째 줄: 28번째 줄:
# 시각화
# 시각화
plot(model)
plot(model)
varImpPlot(model)


# 테스트
# 테스트
33번째 줄: 36번째 줄:
# 정분류율
# 정분류율
sum(pred==testData$ploidy)/nrow(testData)
sum(pred==testData$ploidy)/nrow(testData)
</source>
</syntaxhighlight>
 
==cforest()==
<syntaxhighlight lang='r' run>
set.seed(42) # 랜덤값 고정
data(stagec, package='rpart')
df = stagec
df = na.omit(df) # 결측치 제거
 
# 데이터 분할
library(caret, quietly=T)
idx = createDataPartition(df$ploidy, p=0.7, list=FALSE)
trainData = df[ idx,]
testData  = df[-idx,]
 
# 모델 적합
library(party, quietly=T)
model = cforest(ploidy ~ ., trainData)
 
options(echo=T)
# 모델 정보
model
 
# 테스트
pred = predict(model, newdata=testData, OOB=T, type="response")
# 분류표
table(pred, testData$ploidy)
# 정분류율
sum(pred==testData$ploidy)/nrow(testData)
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[R 배깅]]
* [[R 배깅]]
* [[R 부스팅]]
* [[R 부스팅]]
* [[R XGBoost]]
* [[R 이진분류 모델 비교 구현]]
* [[랜덤 포레스트]]
* [[랜덤 포레스트]]


[[분류: R 통계적 분류]]
[[분류: R 통계적 분류]]
[[분류: R 데이터 분석]]
[[분류: R 데이터 분석]]
[[분류: R randomForest]]
[[분류: R stagec]]
[[분류: R stagec]]
[[분류:랜덤 포레스트]]

2020년 11월 20일 (금) 01:30 기준 최신판

1 개요[ | ]

R Random Forest
R 랜덤 포레스트

2 randomForest()[ | ]

set.seed(42) # 랜덤값 고정
data(stagec, package='rpart')
df = stagec
df = na.omit(df) # 결측치 제거

# 데이터 분할
library(caret, quietly=T)
idx = createDataPartition(df$ploidy, p=0.7, list=FALSE)
trainData = df[ idx,]
testData  = df[-idx,]

# 모델 적합
library(randomForest, warn.conflict=F)
model = randomForest(ploidy ~ ., trainData, ntree=100, proximity=T)

options(echo=T)
# 모델 정보
model
model$importance

# 시각화
plot(model)
varImpPlot(model)

# 테스트
pred = predict(model, testData)
# 분류표
table(pred, testData$ploidy)
# 정분류율
sum(pred==testData$ploidy)/nrow(testData)

3 cforest()[ | ]

set.seed(42) # 랜덤값 고정
data(stagec, package='rpart')
df = stagec
df = na.omit(df) # 결측치 제거

# 데이터 분할
library(caret, quietly=T)
idx = createDataPartition(df$ploidy, p=0.7, list=FALSE)
trainData = df[ idx,]
testData  = df[-idx,]

# 모델 적합
library(party, quietly=T)
model = cforest(ploidy ~ ., trainData)

options(echo=T)
# 모델 정보
model

# 테스트
pred = predict(model, newdata=testData, OOB=T, type="response")
# 분류표
table(pred, testData$ploidy)
# 정분류율
sum(pred==testData$ploidy)/nrow(testData)

4 같이 보기[ | ]