2017-10-13 17 views
0

다음 슬라이드는 현재 link입니다. 슬라이드 121/128에 있으며 AUC를 복제하는 방법을 알고 싶습니다. 저자는 그렇게하는 방법을 설명하지 않았습니다 (슬라이드 124에서도 동일). 두 번째로 슬라이드 125에서 다음 코드가 생성됩니다.xgboost 모델에서 AUC 플로팅 R

bestRound = which.max(as.matrix(cv.res)[,3]-as.matrix(cv.res)[,4]) 
bestRound 

다음과 같은 오류가 나타납니다. as.matrix (cv.res)에

오류 [2] : 경계

다음 코드에 대한 데이터를 아웃 첨자 here에서 다운로드 될 수 있으며, I는 아래의 코드를 생성 한 너의 참고.

질문 : 어떻게 AUC를 작성자로 만들 수 있으며 아래 첨자가 범위를 벗어나는 이유는 무엇입니까?

는 ----- 코드 ------

# Kaggle Winning Solutions 

train <- read.csv('train.csv', header = TRUE) 
test <- read.csv('test.csv', header = TRUE) 
y <- train[, 1] 
train <- as.matrix(train[, -1]) 
test <- as.matrix(test) 

train[1, ] 

#We want to determin who is more influencial than the other 

new.train <- cbind(train[, 12:22], train[, 1:11]) 
train = rbind(train, new.train) 
y <- c(y, 1 - y) 

x <- rbind(train, test) 

(dat[,i]+lambda)/(dat[,j]+lambda) 

A.follow.ratio = calcRatio(x,1,2) 
A.mention.ratio = calcRatio(x,4,6) 
A.retweet.ratio = calcRatio(x,5,7) 
A.follow.post = calcRatio(x,1,8) 
A.mention.post = calcRatio(x,4,8) 
A.retweet.post = calcRatio(x,5,8) 
B.follow.ratio = calcRatio(x,12,13) 
B.mention.ratio = calcRatio(x,15,17) 
B.retweet.ratio = calcRatio(x,16,18) 
B.follow.post = calcRatio(x,12,19) 
B.mention.post = calcRatio(x,15,19) 
B.retweet.post = calcRatio(x,16,19) 

x = cbind(x[,1:11], 
      A.follow.ratio,A.mention.ratio,A.retweet.ratio, 
      A.follow.post,A.mention.post,A.retweet.post, 
      x[,12:22], 
      B.follow.ratio,B.mention.ratio,B.retweet.ratio, 
      B.follow.post,B.mention.post,B.retweet.post) 

AB.diff = x[,1:17]-x[,18:34] 
x = cbind(x,AB.diff) 
train = x[1:nrow(train),] 
test = x[-(1:nrow(train)),] 

set.seed(1024) 
cv.res <- xgb.cv(data = train, nfold = 3, label = y, nrounds = 100, verbose = FALSE, 
       objective = 'binary:logistic', eval_metric = 'auc') 

플롯 여기 AUC 그래프 여기

set.seed(1024) 
cv.res = xgb.cv(data = train, nfold = 3, label = y, nrounds = 3000, 
       objective='binary:logistic', eval_metric = 'auc', 
       eta = 0.005, gamma = 1,lambda = 3, nthread = 8, 
       max_depth = 4, min_child_weight = 1, verbose = F, 
       subsample = 0.8,colsample_bytree = 0.8) 

내가

건너 코드에 휴식입니다
#bestRound: - subscript out of bounds 

bestRound <- which.max(as.matrix(cv.res)[,3]-as.matrix(cv.res)[,4]) 
bestRound 
cv.res 

cv.res[bestRound,] 

set.seed(1024) bst <- xgboost(data = train, label = y, nrounds = 3000, 
          objective='binary:logistic', eval_metric = 'auc', 
          eta = 0.005, gamma = 1,lambda = 3, nthread = 8, 
          max_depth = 4, min_child_weight = 1, 
          subsample = 0.8,colsample_bytree = 0.8) 
preds <- predict(bst,test,ntreelimit = bestRound) 

result <- data.frame(Id = 1:nrow(test), Choice = preds) 
write.csv(result,'submission.csv',quote=FALSE,row.names=FALSE) 

답변

1

코드 h 여기 나에게 거의 의미 집결지 만하면 제공하는 데이터 모델을 구축 최소한의 예입니다

데이터 :

train <- read.csv('train.csv', header = TRUE) 
y <- train[, 1] 
train <- as.matrix(train[, -1]) 

모델 :

library(xgboost) 
cv.res <- xgb.cv(data = train, nfold = 3, label = y, nrounds = 100, verbose = FALSE, 
       objective = 'binary:logistic', eval_metric = 'auc', prediction = T) 

하나를 지정해야합니다 교차 검증 예측을 얻으려면 xgb.cv으로 전화하면 prediction = T입니다.

it = which.max(cv.res$evaluation_log$test_auc_mean) 
best.iter = cv.res$evaluation_log$iter[it] 

가 교차 검증 결과 ROC 곡선을 플롯 :

가장 반복을 구

library(pROC) 
plot(pROC::roc(response = y, 
       predictor = cv.res$pred, 
       levels=c(0, 1)), 
    lwd=1.5) 

enter image description here

가 혼동 행렬을 얻기 위해 (0.5 확률값을 가정하는 임계 값은) :

library(caret) 
confusionMatrix(ifelse(cv.res$pred <= 0.5, 0, 1), y) 
#output 
      Reference 
Prediction 0 1 
     0 2020 638 
     1 678 2164 

       Accuracy : 0.7607   
       95% CI : (0.7492, 0.772) 
    No Information Rate : 0.5095   
    P-Value [Acc > NIR] : <2e-16   

        Kappa : 0.5212   
Mcnemar's Test P-Value : 0.2823   

      Sensitivity : 0.7487   
      Specificity : 0.7723   
     Pos Pred Value : 0.7600   
     Neg Pred Value : 0.7614   
      Prevalence : 0.4905   
     Detection Rate : 0.3673   
    Detection Prevalence : 0.4833   
     Balanced Accuracy : 0.7605   

     'Positive' Class : 0 

한 조정 등 colsample_bylevel 등 ETA, 감마, 람다, 표본, colsample_bytree, 가장 쉬운 방법은 당신이 expand.grid을 사용하여 그리드 검색을 구성하는

같은 교차 검증과 하이퍼 파라미터를 목표로한다 상기 되 그게 하이퍼 매개 변수의 모든 조합 및 xgb.cv 사용자 지정 기능의 일부로 눈금에 lapply 사용). 자세한 내용이 필요하면 의견을 말하십시오.

+0

AUC 플롯을 이용해 주셔서 감사합니다. "교차 검증 예측을 얻으려면 xgb.cv를 호출 할 때 predict = T를 지정해야합니다"라는 오류가 발생했습니다. – user113156

+0

내가 복제하려고하는 또 다른 요점은 슬라이드 121/128에 있습니다. 저자는 "교육 및 테스트 세트에서 AUC의 추세를 볼 수 있습니다."라고 말했습니다. 테스트 세트에서 이것을 복제하는 방법은 무엇입니까? 이것을 테스트 세트에 복제하는 목적은 무엇입니까? – user113156

+0

@ user113156 그럼 xgboost 모델을 훈련하는 것이 훨씬 더 있습니다. 사람들은 일을하는 방식을 선호합니다. 일반적으로 교차 검증 과정에서 하이퍼 매개 변수, 데이터 변환, 업/다운 샘플링, 변수 선택, 확률 임계 값 최적화, 비용 함수 선택이 수행됩니다. 일반적으로 CV의 반복은 아니지만 예를 들어 3-4 배 CV의 5 번의 반복. 그리고이 모든 것들 중에서 가장 좋은 세트를 골라 내면 모든 기차 데이터로 모델을 훈련시키고 테스트 세트에서 검증합니다. 이것은 과도한 피팅을 피하기 위해 모두 완료되었습니다. – missuse