1

私は次のR構成を持っています:

OS:Linux R バージョン 3.0.1 (2013-05-16) rmr2 バージョン 2.2.1 rhdfs バージョン 1.0.6 hadoop バージョン 1.2.0

rmr2パッケージでhadoopを使用してsvmモデルの結果を変換するにはどうすればよいですか? だから私は通常のように構築されたモデルを使用することができます:

predict(svm1, "new data")

私は以下のコードを持っています:

# set eviremonet variables
Sys.setenv(HADOOP_CMD="~/Downloads/hadoop-1.2.0/bin/hadoop")
Sys.setenv(HADOOP_HOME="~/Downloads/hadoop-1.2.0/")

# start hadoop

# load librarys
library(rmr2)
library(rhdfs)
library(e1071)

# load sample data
data(iris)

# init hdfs
hdfs.init()

# push data to hdfs
iris.dfs <- to.dfs(iris)

# define map function
iris.map <- function(k, v) 
  {
  svm(v$Species ~ ., data=v)
  }

# rum mar job
iris.svm <- mapreduce(input=iris.dfs, map=iris.map)

# get result back
iris.res <- from.dfs(iris.svm)

svm1 <- svm(iris$Species ~ ., data=iris)

class(iris.res)
class(svm1)

両方のクラスの結果は次のとおりです。

> class(iris.res)
[1] "list"
> class(svm1)
[1] "svm.formula" "svm"

> str(svm1)
List of 30
 $ call           : language svm(formula = iris$Species ~ ., data = iris)
 $ type           : num 0
 $ kernel         : num 2
 $ cost           : num 1
 $ degree         : num 3
 $ gamma          : num 0,25
 $ coef0          : num 0
 $ nu             : num 0,5
 $ epsilon        : num 0,1
 $ sparse         : logi FALSE
 $ scaled         : logi [1:4] TRUE TRUE TRUE TRUE
 $ x.scale        :List of 2
  ..$ scaled:center: Named num [1:4] 5,84 3,06 3,76 1,20
  .. ..- attr(*, "names")= chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
  ..$ scaled:scale : Named num [1:4] 0,828 0,436 1,765 0,762
  .. ..- attr(*, "names")= chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
 $ y.scale        : NULL
 $ nclasses       : int 3
 $ levels         : chr [1:3] "setosa" "versicolor" "virginica"
 $ tot.nSV        : int 51
 $ nSV            : int [1:3] 8 22 21
 $ labels         : int [1:3] 1 2 3
 $ SV             : num [1:51, 1:4] -1,743 -1,864 -0,173 -0,535 -1,501 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:51] "9" "14" "16" "21" ...
  .. ..$ : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
 $ index          : int [1:51] 9 14 16 21 23 24 26 42 51 53 ...
 $ rho            : num [1:3] -0,0203 0,1312 -0,0629
 $ compprob       : logi FALSE
 $ probA          : NULL
 $ probB          : NULL
 $ sigma          : NULL
 $ coefs          : num [1:51, 1:2] 0,0891 0,0000 0,8652 0,0000 0,0000 ...
 $ na.action      : NULL
 $ fitted         : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
  ..- attr(*, "names")= chr [1:150] "1" "2" "3" "4" ...
 $ decision.values: num [1:150, 1:3] 1,20 1,06 1,18 1,11 1,19 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:150] "1" "2" "3" "4" ...
  .. ..$ : chr [1:3] "setosa/versicolor" "setosa/virginica" "versicolor/virginica"
 $ terms          :Classes 'terms', 'formula' length 3 iris$Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
  .. ..- attr(*, "variables")= language list(iris$Species, Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
  .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:5] "iris$Species" "Sepal.Length" "Sepal.Width" "Petal.Length" ...
  .. .. .. ..$ : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
  .. ..- attr(*, "term.labels")= chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
  .. ..- attr(*, "order")= int [1:4] 1 1 1 1
  .. ..- attr(*, "intercept")= num 0
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(iris$Species, Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
  .. ..- attr(*, "dataClasses")= Named chr [1:5] "factor" "numeric" "numeric" "numeric" ...
  .. .. ..- attr(*, "names")= chr [1:5] "iris$Species" "Sepal.Length" "Sepal.Width" "Petal.Length" ...
 - attr(*, "class")= chr [1:2] "svm.formula" "svm"
> str(iris.res)
List of 2
 $ key: NULL
 $ val:List of 30
  ..$ call           : language svm(formula = v$Species ~ ., data = v)
  ..$ type           : num 0
  ..$ kernel         : num 2
  ..$ cost           : num 1
  ..$ degree         : num 3
  ..$ gamma          : num 0,25
  ..$ coef0          : num 0
  ..$ nu             : num 0,5
  ..$ epsilon        : num 0,1
  ..$ sparse         : logi FALSE
  ..$ scaled         : logi [1:4] TRUE TRUE TRUE TRUE
  ..$ x.scale        :List of 2
  .. ..$ scaled:center: Named num [1:4] 5,84 3,06 3,76 1,20
  .. .. ..- attr(*, "names")= chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
  .. ..$ scaled:scale : Named num [1:4] 0,828 0,436 1,765 0,762
  .. .. ..- attr(*, "names")= chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
  ..$ y.scale        : NULL
  ..$ nclasses       : int 3
  ..$ levels         : chr [1:3] "setosa" "versicolor" "virginica"
  ..$ tot.nSV        : int 51
  ..$ nSV            : int [1:3] 8 22 21
  ..$ labels         : int [1:3] 1 2 3
  ..$ SV             : num [1:51, 1:4] -1,743 -1,864 -0,173 -0,535 -1,501 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:51] "9" "14" "16" "21" ...
  .. .. ..$ : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
  ..$ index          : int [1:51] 9 14 16 21 23 24 26 42 51 53 ...
  ..$ rho            : num [1:3] -0,0203 0,1312 -0,0629
  ..$ compprob       : logi FALSE
  ..$ probA          : NULL
  ..$ probB          : NULL
  ..$ sigma          : NULL
  ..$ coefs          : num [1:51, 1:2] 0,0891 0,0000 0,8652 0,0000 0,0000 ...
  ..$ na.action      : NULL
  ..$ fitted         : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
  .. ..- attr(*, "names")= chr [1:150] "1" "2" "3" "4" ...
  ..$ decision.values: num [1:150, 1:3] 1,20 1,06 1,18 1,11 1,19 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:150] "1" "2" "3" "4" ...
  .. .. ..$ : chr [1:3] "setosa/versicolor" "setosa/virginica" "versicolor/virginica"
  ..$ terms          :Classes 'terms', 'formula' length 3 v$Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
  .. .. ..- attr(*, "variables")= language list(v$Species, Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
  .. .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
  .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. ..$ : chr [1:5] "v$Species" "Sepal.Length" "Sepal.Width" "Petal.Length" ...
  .. .. .. .. ..$ : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
  .. .. ..- attr(*, "term.labels")= chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
  .. .. ..- attr(*, "order")= int [1:4] 1 1 1 1
  .. .. ..- attr(*, "intercept")= num 0
  .. .. ..- attr(*, "response")= int 1
  .. .. ..- attr(*, ".Environment")=<environment: 0xb639820> 
  .. .. ..- attr(*, "predvars")= language list(v$Species, Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
  .. .. ..- attr(*, "dataClasses")= Named chr [1:5] "factor" "numeric" "numeric" "numeric" ...
  .. .. .. ..- attr(*, "names")= chr [1:5] "v$Species" "Sepal.Length" "Sepal.Width" "Petal.Length" ...

しかし、結果リストを通常の smv 呼び出しと同じクラスに変換するにはどうすればよいでしょうか?

4

1 に答える 1

1

のように map 関数の呼び出しをラップsvm(v$Species ~ ., data=v)するだけです。マップは、リスト、マトリックス、ベクトル、およびデータ フレームのみを返すことができます。モデルを返した場合 (明らかに、意図的に実装したわけではありません)、リストに強制されます。たぶん、戻り値がサポートされている 4 つの値のいずれでもないときはいつでも防御的にそれを行うことができます。listlist(svm(v$Species ~ ., data=v))listしかし、頭が良すぎて推測しすぎないようにしたいと思います。あなたのアプローチのもう1つの問題は、マップ関数がデータセットの任意のサブセットで呼び出されることです(より大きなデータセットの場合)。そのため、出力でモデルのリストを取得します(出力で値を呼び出した後) . 複数の svm ができたので、それをアンサンブルとして扱いますか? ただし、マップ フェーズのサブセットは任意であり、ランダム化などの統計的特性はありません。rmr には svm 関数を並列分散させる超能力があると思われますが、そうではありません。データの異なるチャンクのクラスターで並列に呼び出すだけです。小さな例では、チャンクは 1 つしかありませんが、それは欺瞞的です。試すrmr.options(keyval.length = 3)非常に小さなチャンクで何が起こるかを確認します (本番用ではありません)。別のアプローチは、単一のマシンにロードできる最大のサンプルでモデルを構築し、予測を並行して実行することです。もちろん、それは学習段階ではそれほどスケーラブルではありませんが、私はそれを行う大きなスタートアップを知っています. Uri Laserson による Cloudera ブログのリサンプリングに関する記事を参照してください。良いアイデアが得られると思います。最後に、Google グループに rmr と関連パッケージ専用のフォーラムがあります。コミュニティへの参加を歓迎します。

于 2013-07-19T19:13:07.973 に答える