R 랜덤 포레스트

Jmnote bot (토론 | 기여)님의 2020년 11월 20일 (금) 01:30 판 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

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 같이 보기[ | ]