"R XGBoost"의 두 판 사이의 차이

13번째 줄: 13번째 줄:
model = xgboost(data = train$data, label = train$label, max.depth = 2,
model = xgboost(data = train$data, label = train$label, max.depth = 2,
               eta = 1, nthread = 2, nround = 2, objective = "binary:logistic")
               eta = 1, nthread = 2, nround = 2, objective = "binary:logistic")
options(echo=T)
# 모델 정보
# 모델 정보
model
model

2020년 5월 9일 (토) 17:53 판

1 개요

R XGBoost

2 2 분류

data(agaricus.train, package='xgboost')
data(agaricus.test,  package='xgboost')
train = agaricus.train
test  = agaricus.test

# 모델 적합
library(xgboost)
model = xgboost(data = train$data, label = train$label, max.depth = 2,
               eta = 1, nthread = 2, nround = 2, objective = "binary:logistic")

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

# 테스트
pred = predict(model, test$data)
pred = ifelse(pred>0.5, 1, 0)
# 분류표
table(pred, test$label)
# 정분류율(accuracy)
sum(pred==test$label)/length(pred)

3 3 분류

set.seed(42) # 랜덤값 고정

# 데이터 준비
df = iris
num_class = 3
names = levels(df$Species)

# 데이터 분할
library(caret, quietly=T)
idx = createDataPartition(df$Species, list=F, p=0.8)
Train = df[ idx,]
Test  = df[-idx,]

train.data  = as.matrix(Train[,names(df)!="Species"])
test.data   = as.matrix(Test[, names(df)!="Species"])
train.label = as.integer(Train$Species) - 1 # 0기반
test.label  = as.integer(Test$Species ) - 1 # 0기반

# 모델 적합
library(xgboost)
dtrain = xgb.DMatrix(data=train.data, label=train.label)
dtest  = xgb.DMatrix(data=test.data , label=test.label )
watchlist = list(train=dtrain, eval=dtest)
param = list(max_depth=2, eta=1, verbose=0, nthread=2,
             objective="multi:softprob", eval_metric="mlogloss", num_class=num_class)
model = xgb.train(param, dtrain, nrounds=2, watchlist)

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

# 테스트
pred = as.data.frame(predict(model,test.data,reshape=T))
colnames(pred) = names
pred$prediction = apply(pred,1,function(x) names[which.max(x)])
pred$class = Test$Species
pred
# 분류표
table(pred$prediction, pred$class)
# 정분류율(accuracy)
sum(pred$prediction==pred$class)/nrow(pred)

4 같이 보기

5 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}