2017-12-14 14 views
0

私はPythonでH2Oを初めて使っています。私は、H2OのWebサイトのサンプルコードに従って、アンサンブルモデルを使用してデータをモデル化しようとしています。 (http://docs.h2o.ai/h2o/latest-stable/h2o-docs/data-science/stacked-ensembles.htmlfold_column引数を持つH2Oのアンサンブルモデル

私は基本モデルとしてGBMとRFを適用しました。スタッキングを使ってアンサンブルモデルでマージしようとしました。さらに、私の訓練データでは、使用するfoldという1つの追加の列を作成しました。fold_column = "fold"

私は10倍のCVを適用しました。私はcv1の結果を受け取りました。しかし、すべての予測は他の9個のCVSから来ており、空です。私はここで何が欠けていますか?ここで

は私のサンプルでは、​​データです:

enter image description here

コード:

import h2o 
from h2o.estimators.random_forest import H2ORandomForestEstimator 
from h2o.estimators.gbm import H2OGradientBoostingEstimator 
from h2o.estimators.stackedensemble import H2OStackedEnsembleEstimator 
from h2o.grid.grid_search import H2OGridSearch 
from __future__ import print_function 

h2o.init(port=23, nthreads=6) 

train = h2o.H2OFrame(ens_df) 
test = h2o.H2OFrame(test_ens_eq) 

x = train.drop(['Date','EQUITY','fold'],axis=1).columns 
y = 'EQUITY' 

cat_cols = ['A','B','C','D'] 
train[cat_cols] = train[cat_cols].asfactor() 
test[cat_cols] = test[cat_cols].asfactor() 

my_gbm = H2OGradientBoostingEstimator(distribution="gaussian", 
             ntrees=10, 
             max_depth=3, 
             min_rows=2, 
             learn_rate=0.2, 
             keep_cross_validation_predictions=True, 
             seed=1) 

my_gbm.train(x=x, y=y, training_frame=train, fold_column = "fold") 

その後、私は

my_gbm.cross_validation_prediとCVの結果を確認するときctions():

enter image description here

プラス私はテストセットでアンサンブルをしようとしたとき、私は以下の警告を得る:

# Train a stacked ensemble using the GBM and GLM above 
ensemble = H2OStackedEnsembleEstimator(model_id="mlee_ensemble", 
             base_models=[my_gbm, my_rf]) 
ensemble.train(x=x, y=y, training_frame=train) 

# Eval ensemble performance on the test data 
perf_stack_test = ensemble.model_performance(test) 

pred = ensemble.predict(test) 
pred 

/mgmt/data/conda/envs/python3.6_4.4/lib/python3.6/site-packages/h2o/job.py:69: UserWarning: Test/Validation dataset is missing column 'fold': substituting in a column of NaN 
    warnings.warn(w) 

を私はfold_columnについて何かが足りないのですか?

+0

公開されているデータセットを使用するようにサンプルを変更できますか? https://stackoverflow.com/help/mcveまた、CVプレッズをチェックする方法を示してください(ここで何をしているかを示すコードはありません)。 –

+0

@ErinLeDell CVプレッズに関連する行を入れます。また、私はサンプルデータセットを作成していますが、小さな質問が1つあります。私は、サンプルコードでは、fold_columnの乱数を代入する 'cars.kfold_column(n_folds = 5、seed = 1234)'を使用していることに気付きました。乱数を割り当てる代わりに、fold_columnのデータ(リストなど)を使用したいと思います。例えば、私は 'train ['fold'] .kfold_column()'を試しましたが、まだ乱数を割り当てています。 kfold_columnにデータを導入するにはどうすればよいですか?あるいは、 'kfold_column'を使用せずにトレーニングセットに 'fold'カラムを置くだけで十分ですか? –

答えて

1

ここでは、カスタム折り畳み列(リストから作成)を使用する方法の例を示します。これは、H2OユーザーガイドのStacked Ensembleページのexample Python codeの改訂版です。

import h2o 
from h2o.estimators.random_forest import H2ORandomForestEstimator 
from h2o.estimators.gbm import H2OGradientBoostingEstimator 
from h2o.estimators.stackedensemble import H2OStackedEnsembleEstimator 
from h2o.grid.grid_search import H2OGridSearch 
from __future__ import print_function 
h2o.init() 

# Import a sample binary outcome training set into H2O 
train = h2o.import_file("https://s3.amazonaws.com/erin-data/higgs/higgs_train_10k.csv") 

# Identify predictors and response 
x = train.columns 
y = "response" 
x.remove(y) 

# For binary classification, response should be a factor 
train[y] = train[y].asfactor() 

# Add a fold column, generate from a list 
# The list has 10 unique values, so there will be 10 folds 
fold_list = list(range(10)) * 1000 
train['fold_id'] = h2o.H2OFrame(fold_list) 


# Train and cross-validate a GBM 
my_gbm = H2OGradientBoostingEstimator(distribution="bernoulli", 
             ntrees=10, 
             keep_cross_validation_predictions=True, 
             seed=1) 
my_gbm.train(x=x, y=y, training_frame=train, fold_column="fold_id") 

# Train and cross-validate a RF 
my_rf = H2ORandomForestEstimator(ntrees=50, 
           keep_cross_validation_predictions=True, 
           seed=1) 
my_rf.train(x=x, y=y, training_frame=train, fold_column="fold_id") 

# Train a stacked ensemble using the GBM and RF above 
ensemble = H2OStackedEnsembleEstimator(base_models=[my_gbm, my_rf]) 
ensemble.train(x=x, y=y, training_frame=train) 

モデルで相互検証された予測を表示する方法に関する2番目の質問にお答えします。 .cross_validation_holdout_predictions()この方法は、訓練観測の元の順序で、交差検定予測の単一H2OFrameを返します:彼らは、しかし、あなたはおそらく使用する方法があり、2つの場所に保存されている

In [11]: my_gbm.cross_validation_holdout_predictions() 
Out[11]: 
    predict  p0  p1 
--------- -------- -------- 
     1 0.323155 0.676845 
     1 0.248131 0.751869 
     1 0.288241 0.711759 
     1 0.407768 0.592232 
     1 0.507294 0.492706 
     0 0.6417 0.3583 
     1 0.253329 0.746671 
     1 0.289916 0.710084 
     1 0.524328 0.475672 
     1 0.252006 0.747994 

[10000 rows x 3 columns] 

は、第2の方法.cross_validation_predictions()は、元のトレーニングフレームと同じ行数を有するH2OFrame内の各折り目からの予測を格納するリストであるが、その折り目でアクティブでない行はゼロの値を有する。これは通常、人々が最も有用と感じる形式ではないので、代わりに他の方法を使用することをお勧めします。

In [13]: type(my_gbm.cross_validation_predictions()) 
Out[13]: list 

In [14]: len(my_gbm.cross_validation_predictions()) 
Out[14]: 10 

In [15]: my_gbm.cross_validation_predictions()[0] 
Out[15]: 
    predict  p0  p1 
--------- -------- -------- 
     1 0.323155 0.676845 
     0 0   0 
     0 0   0 
     0 0   0 
     0 0   0 
     0 0   0 
     0 0   0 
     0 0   0 
     0 0   0 
     0 0   0 

[10000 rows x 3 columns] 
関連する問題