2017-11-12 10 views
0

私は以下のコード使用してキャレットで加重観察を適用したい:エラー(R)

model_weights <- ifelse(train$y == 0, 
         (1/table(train$y)[1]) * 0.5, 
         (1/table(train$y)[2]) * 0.5) 

xgbT <- train(x = as.matrix(train[,-21]), y = make.names(as.factor(train$y)), 
       method = "xgbTree", 
       trControl = cctrl1, 
       metric = "MCC", 
       maximize = TRUE, 
       weights = model_weights, 
       preProc = c("center", "scale"), 
       tuneGrid = expand.grid(nrounds = c(150), #number of trees 
            max_depth = c(7), #max tree depth 
            eta = c(0.03), #learning rate 
            gamma = c(0.3), #min split loss 
            colsample_bytree = c(0.7), 
            min_child_weight = c(10, 1, 5), #min number of instances in the leaf 
            subsample = c(0.6)), #subsample ratio of the training instance 
       early_stop_round = c(3), #if no improvements over specified rounds 
       objective = c("binary:logistic"), 
       silent = 0) 

はしかし、それは私に、このエラーを与える:Error in model.frame.default(formula = .outcome ~ ., data = dat, weights = wts) : variable lengths differ (found for '(weights)')

私はそれらの長さは以下のコードと同じであることが確認されているものの:

> table(model_weights) 
model_weights 
0.0000277654375832963 0.000231481481481481 
       18008     2160 
> table(train$y) 

    0  1 
18008 2160 

任意のアイデアどのようにこの問題を解決するには?

注:weightsパラメータなしでtrain機能を実行できます。

+0

、私はここでそれを確認:https://topepo.github.io/caret /train-models-by-tag.html#Accepts_Case_Weights –

+0

どちらですか?リンクを共有してください –

+0

申し訳ありません、朝...私はリンクしていると仮定しました:[ここ](https://github.com/topepo/caret/blob/master/RegressionTests/Code/xgbTree.R)それは - 'test_class_cv_form_weight 'example – missuse

答えて

0

さらにデバッグした後、cctrl1samplingを適用しているため、問題が見つかった。したがって、再サンプリングを適用する前に生成するので、weightsの長さは異なります。

trControlからsamplingを削除するだけで解決できます。あなたはまだリサンプリングを適用したい場合、あなたは、コードの下に実行する前にデータを再サンプリングする必要があり:はい@missuse

model_weights <- ifelse(train$y == 0, 
        (1/table(train$y)[1]) * 0.5, 
        (1/table(train$y)[2]) * 0.5) 
+0

このような重量で得られたモデルは意味がありますか?モデルが両方のクラスを予測するためには、重みの1つが1である必要があることが分かりました。 – missuse

+0

モデルに影響を与えているかのように理解してください。答えは「はい」です。しかし、今私は、再サンプリングを適用すると、重み付けを適用する必要がないかもしれないことを認識しています。 –

+0

SMOTE/ROSEのようなサンプリングやオーバーラップやオーバーサンプリングを適用している場合は、ウェイトは良い考えではありません。しかし、一般的に私は体重を使う方が好きです。彼らは実装が簡単で、より簡単です。 – missuse