2017-10-13 9 views
0

現在、以下のlinkのスライドに従っています。私はスライド121/128にあり、AUCをどのように複製するかを知りたいと思います。作者はその方法を説明しませんでした(スライド124でも同じです)。第2に、スライド125上で次のコードが生成される。xgboostモデルからのAUCをプロットするR

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

次のエラーが表示されます。 as.matrixで

エラー(cv.res)[2]:添字境界

から次のコードのためのデータは、hereからダウンロードすることができ、私はのために以下のコードを生成しましたあなたの参照。

質問:著者として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私にはほとんど意味をaveのが、ここで提供されるデータとモデルを構築するための最小限の例です。

データ:

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) 

1を指定する必要があり、クロスバリデーション予測を取得するにはxgb.cvに電話するとprediction = Tとなります。クロスバリデーションの結果にROC曲線をプロットする

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

:最高の反復を得るため

混同行列を取得する

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

enter image description here

(0.5 PROBを想定する閾値であります):

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 

1をチューニングするために、このようなcolsample_bylevelなどエータ、ガンマ、ラムダ、サブサンプル、colsample_bytree、最も簡単な方法は、あなたが上expand.gridを使用するグリッドサーチを構築することである

としてクロスバリデーションとハイパーパラメータを目指すべきで言われていることハイパーパラメータのすべての組み合わせとカスタム関数の一部としてxgb.cvとグリッド上でlapplyを使用して)。詳細が必要な場合はコメントしてください。

+0

AUCのプロットがありがとうございました。 "クロスバリデーションの予測を得るには、xgb.cvを呼び出すときに予測= Tを指定する必要があります。 – user113156

+0

私が複製しようとしているもう一つのポイントは、スライド121/128にあります。作者は「トレーニングとテストセットにAUCの傾向が見える」と述べています。これをテストセットに複製するにはどうすればいいですか?これをテストセットに複製する目的は何ですか? – user113156

+0

@ user113156それから、xgboostモデルを訓練することはずっとあります。そして、人々は自分のやり方を好みます。一般に、ハイパーパラメータ、データ変換、アップ/ダウンサンプリング、変数選択、確率閾値最適化、コスト関数選択は、相互検証中に実行される。 CVの1回の反復だけではなく、例えば3〜4倍のCVの5回の反復など。これらすべてのものの中から最良のものを選ぶと、すべての列車データを使ってモデルを訓練し、テストセットで検証します。これは、オーバーフィットを避けるためにすべて行われます。 – missuse

関連する問題