2017-07-26 7 views
0

私はRでh2o.randomforestを使用して2つのグループに分類子を作成していましたが、グループ "A" &グループ "B"と言います。以下に示すように、一例として、私がランダムサンプルデータセットを生成し、h2oframeにそれを変換:ラベルはh2o.randomforestで役割を果たしますか?

a <- sample(0:1,10000,replace=T) 
    b <- sample(0:1,10000,replace=T) 
    c <- sample(1:10,10000,replace=T) 
    d <- sample(0:1,10000,replace=T) 
    e <- sample(0:1,10000,replace=T) 
    f <- sample(0:1,10000,replace=T) 

基本的に、それらは因数分解されるであろうと、すべてがC以外は、2つのレベルを有し、10 levels.The第5000を有しています行には「A」というラベルが割り当てられ、残りにはラベル「B」が割り当てられました。また、最初の5000行では「B」、それ以外では「A」というnlabelという別の列がありました。ここで

は、最初の10行と私のデータセットの最後の10行である:私はランダムデータセットを生成するので、私はすべての良い分類器を得ることができる以外、私はしませんでした

  a b c d e f label nlabel 
    1  0 0 5 0 1 0  A  B 
    2  0 1 5 1 1 1  A  B 
    3  0 0 6 0 0 1  A  B 
    4  0 0 8 0 0 1  A  B 
    5  1 1 1 1 1 1  A  B 
    6  1 1 6 1 0 1  A  B 
    7  1 0 3 1 1 1  A  B 
    8  1 1 9 1 0 1  A  B 
    9  1 0 8 1 0 1  A  B 
    10 0 0 1 0 1 1  A  B 
    ............. 
    9991 1 1 3 0 0 1  B  A 
    9992 0 0 7 1 0 0  B  A 
    9993 1 0 9 0 1 1  B  A 
    9994 0 1 3 0 0 0  B  A 
    9995 1 1 8 0 1 0  B  A 
    9996 0 1 8 0 1 0  B  A 
    9997 1 1 9 0 1 0  B  A 
    9998 0 0 5 1 0 1  B  A 
    9999 0 1 9 1 1 0  B  A 
    10000 0 1 10 1 0 1  B  A 

(または私ができました世界で一番幸運な男)。私はランダムな推測のようなものを除いています。ここで私はRで「ランダムフォレスト」のパッケージを使用して得た1つの結果である:

> rf <- randomForest(label ~ a + b + c + e + f, 
    +       data = test, 
           ntree = 100) 
    > rf 

     Call: 
     randomForest(formula = label ~ a + b + c + e + f, data = test,  ntree = 100) 
         Type of random forest: classification 
          Number of trees: 100 
     No. of variables tried at each split: 2 

       OOB estimate of error rate: 50.17% 
     Confusion matrix: 
      A B class.error 
     A 2507 2493  0.4986 
     B 2524 2476  0.5048 

はしかし、同じデータセットでh2o.randomforest使用することによって、私は別の結果を得ました。私はh2o.randomforestと"nlabel"の代わり"label"を使用した場合、私はまだAさんを予測する上で高いエラー率を得、また

 > TEST <- as.h2o(test) 
     > rfh2o <- h2o.randomForest(y = "label", 
            x = c("a","b", 
             "c","d", 
             "e","f"), 
            training_frame = TEST, 
            ntrees = 100) 
    > rfh2o 
    Model Details: 
    ============== 

    H2OBinomialModel: drf 
    Model ID: DRF_model_R_1501015614001_1029 
    Model Summary: 
     number_of_trees number_of_internal_trees model_size_in_bytes min_depth max_depth mean_depth min_leaves 
    1    100      100    366582   7  14 11.33000   1 
     max_leaves mean_leaves 
    1  319 286.52000 


    H2OBinomialMetrics: drf 
    ** Reported on training data. ** 
    ** Metrics reported on Out-Of-Bag training samples ** 

    MSE: 0.2574374 
    RMSE: 0.5073829 
    LogLoss: 0.7086906 
    Mean Per-Class Error: 0.5 
    AUC: 0.4943865 
    Gini: -0.01122696 

    Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold: 
      A  B Error   Rate 
    A  0 5000 1.000000 =5000/5000 
    B  0 5000 0.000000  =0/5000 
    Totals 0 10000 0.500000 =5000/10000 

    Maximum Metrics: Maximum metrics at their respective thresholds 
          metric threshold value idx 
    1      max f1 0.231771 0.666667 399 
    2      max f2 0.231771 0.833333 399 
    3     max f0point5 0.231771 0.555556 399 
    4     max accuracy 0.459704 0.506800 251 
    5    max precision 0.723654 0.593750 10 
    6     max recall 0.231771 1.000000 399 
    7    max specificity 0.785389 0.999800 0 
    8    max absolute_mcc 0.288276 0.051057 389 
    9 max min_per_class_accuracy 0.500860 0.488000 200 
    10 max mean_per_class_accuracy 0.459704 0.506800 251 

Based on the result above, the confusion matrix is different from what I got from "randomForest" package. 

:ここで私が使用したコードと私が得た結果です。しかし、現在のモデルでは、Aは最後のモデルのBと同じです。ここでは、コードされ、結果は私が得た:結果の

> rfh2o_n <- h2o.randomForest(y = "nlabel", 
+       x = c("a","b", 
+         "c","d", 
+         "e","f"), 
+       training_frame = TEST, 
+       ntrees = 100) 

> rfh2o_n 
Model Details: 
============== 

H2OBinomialModel: drf 
Model ID: DRF_model_R_1501015614001_1113 
Model Summary: 
    number_of_trees number_of_internal_trees model_size_in_bytes min_depth max_depth mean_depth min_leaves 
1    100      100    365232  11  14 11.18000   1 
    max_leaves mean_leaves 
1  319 285.42000 


H2OBinomialMetrics: drf 
** Reported on training data. ** 
** Metrics reported on Out-Of-Bag training samples ** 

MSE: 0.2575674 
RMSE: 0.507511 
LogLoss: 0.7089465 
Mean Per-Class Error: 0.5 
AUC: 0.4923496 
Gini: -0.01530088 

Confusion Matrix (vertical: actual; across: predicted) for F1-optimal threshold: 
     A  B Error   Rate 
A  0 5000 1.000000 =5000/5000 
B  0 5000 0.000000  =0/5000 
Totals 0 10000 0.500000 =5000/10000 

Maximum Metrics: Maximum metrics at their respective thresholds 
         metric threshold value idx 
1      max f1 0.214495 0.666667 399 
2      max f2 0.214495 0.833333 399 
3     max f0point5 0.214495 0.555556 399 
4     max accuracy 0.617230 0.506600 74 
5    max precision 0.621806 0.541833 70 
6     max recall 0.214495 1.000000 399 
7    max specificity 0.749866 0.999800 0 
8    max absolute_mcc 0.733630 0.042465 6 
9 max min_per_class_accuracy 0.499186 0.486400 201 
10 max mean_per_class_accuracy 0.617230 0.506600 74 

このような種類のラベルがh2o.randomforestで役割を果たしている場合、私は疑問に思うしました。 私は頻繁にh2oを使用しませんが、上記の結果は本当に私を混乱させました。それは確かに確率なのでしょうか、あるいは私はちょっとばかげた間違いや何かをしましたか?

答えて

0

データは完全にランダムなので、H2Oがデフォルトでしきい値に使用するmax-f1統計は最終的な予測が有用な値を生成しないためだと思います。

しきい値を0.5にすると、以下のように、予想される動作が得られます。

また、H2O Flowを開いて、訓練されたモデルのROC曲線を見ると、予想通りにひどく直線に近くなります。

library(data.table) 
library(h2o) 

a <- sample(0:1,10000,replace=T) 
b <- sample(0:1,10000,replace=T) 
c <- sample(1:10,10000,replace=T) 
d <- sample(0:1,10000,replace=T) 
e <- sample(0:1,10000,replace=T) 
f <- sample(0:1,10000,replace=T) 
df = data.frame(a, b, c, d, e, f) 
dt = as.data.table(df) 
dt[1:5000, label := "A"] 
dt[5001:10000, label := "B"] 
dt$label = as.factor(dt$label) 
dt 

h2o.init() 
h2o_dt <- as.h2o(dt) 
model = h2o.randomForest(y = "label", 
         x = c("a", "b", "c", "d", "e", "f"), 
         training_frame = h2o_dt, 
         ntrees = 10, 
         model_id = "model") 
model 
h2o_preds = h2o.predict(model, h2o_dt) 
preds = as.data.table(h2o_preds) 
preds[, prediction := A > 0.5] 
table(preds$prediction) 

そして、最終的な出力は次のようになります。

FALSE TRUE 
5085 4915 

あなたはそれを倍の束を再実行し、値がランダムしかし5000それぞれの周りにグループ化された跳ね返る見ることができます。

関連する問題