2017-06-14 2 views
2

私はいくつかの結果を予測するためにテキストを使用する、非常に単純なML学習問題を実装しようとしています。 Rには、いくつかの基本的な例は次のようになります。私のテキストのスパース行列表現を得るためにH20:テキストデータのグラデーションブーストを使用する方法は?

輸入いくつかの偽物が、面白いテキストデータ

library(caret) 
library(dplyr) 
library(text2vec) 

dataframe <- data_frame(id = c(1,2,3,4), 
         text = c("this is a this", "this is 
         another",'hello','what???'), 
         value = c(200,400,120,300), 
         output = c('win', 'lose','win','lose')) 

> dataframe 
# A tibble: 4 x 4 
    id   text value output 
    <dbl>   <chr> <dbl> <chr> 
1  1 this is a this 200 win 
2  2 this is another 400 lose 
3  3   hello 120 win 
4  4   what??? 300 lose 

使用text2vec

#these are text2vec functions to tokenize and lowercase the text 
prep_fun = tolower 
tok_fun = word_tokenizer 

#create the tokens 
train_tokens = dataframe$text %>% 
    prep_fun %>% 
    tok_fun 

it_train = itoken(train_tokens)  
vocab = create_vocabulary(it_train) 
vectorizer = vocab_vectorizer(vocab) 
dtm_train = create_dtm(it_train, vectorizer) 

> dtm_train 
4 x 6 sparse Matrix of class "dgCMatrix" 
    what hello another a is this 
1 .  .  . 1 1 2 
2 .  .  1 . 1 1 
3 .  1  . . . . 
4 1  .  . . . . 
(も https://github.com/dselivanov/text2vec/blob/master/vignettes/text-vectorization.Rmdを参照してください)

最後に、私のスパース行列を使用してoutputを予測するためにアルゴをトレーニングします(たとえば、caretを使用)。

mymodel <- train(x=dtm_train, y =dataframe$output, method="xgbTree") 

> confusionMatrix(mymodel) 
Bootstrapped (25 reps) Confusion Matrix 

(entries are percentual average cell counts across resamples) 

      Reference 
Prediction lose win 
     lose 17.6 44.1 
     win 29.4 8.8 

Accuracy (average) : 0.264 

私の問題は、次のとおりです。

私はspark_read_csvrsparklingas_h2o_frameを使用してh20にデータをインポートする方法を参照してください。 しかし、上記2と3のポイントについては、私は完全に失われています。

このアプローチが可能であれば、ヒントを教えてもらえますか、それともh2oで教えてください。

多くの感謝!

+1

'it_train'変数とは何ですか?私はあなたのコードのステップを逃したと思う(ほとんど再現可能だが、まだ)。 –

+0

ちょっと@ErinLeDellあなたは正しいです。 1秒間保持 –

+0

@ErinLeDell質問が更新されました! –

答えて

1

これは2つの方法のうちの1つを解決することができます。最初はRで、次にモデリングではH2Oに移動します。2. H2Oのword2vec実装を使用してH2Oで完全に移動します。

R data.framesとtext2vecを使用し、次にスパース行列をH2Oフレームに変換し、H2Oでモデル化します。

# Use same code as above to get to this point, then: 

# Convert dgCMatrix to H2OFrame, cbind the response col 
train <- as.h2o(dtm_train) 
train$y <- as.h2o(dataframe$output) 

# Train any H2O model (e.g GBM) 
mymodel <- h2o.gbm(y = "y", training_frame = train, 
        distribution = "bernoulli", seed = 1) 

それとも、H2Oに埋め込むword2vecを訓練疎行列と同等のものを得るためにあなたのテキストに適用することができます。次に、H2Oマシン学習モデル (GBM)を訓練する。この回答を後であなたのデータを使った実際の例で編集しようとしていますが、その間にexampleはRでH2Oのword2vec機能を使用しています。

+0

本当にクールな@ErinLeDellです。あなたの実例を楽しみにしています!ありがとう –

+1

しかし、問題のword2vecはどこで見つかりましたか? Text2vec!= Word2vec。質問はh2oに疎マトリックスをエクスポートする方法についてです!そしてそれを行う方法 - 行列をsvmlight形式に変換します。 –

+1

Noobieが質問したタスクは、H2Oのみでテキストモデルを訓練することに相当します.H2Oで同じタスク(Word2Vecを使用)を実行できるということです。上記の解決策では、text2vecを使い続けることができますが、Rメモリ(分散H2O w2vではなく)でテキスト処理計算を実行する必要がありますので、回避策としてH2O w2vを提案しました。 –

関連する問題