hadoop-rmr2-svm model-将结果“list”类转换为原始类“svm.formula”“svm”

yfjy0ee7  于 2021-06-03  发布在  Hadoop
关注(0)|答案(1)|浏览(319)

我有以下r配置:
操作系统:linux r版本3.0.1(2013-05-16)rmr2版本2.2.1 rhdfs版本1.0.6 hadoop版本1.2.0
如何使用hadoop和rmr2包转换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调用相同的类呢?

fgw7neuy

fgw7neuy1#

包起来就行了 svm(v$Species ~ ., data=v) 在一个 list 调用map函数,如中所示 list(svm(v$Species ~ ., data=v)) . Map只能返回列表、矩阵、向量和Dataframe。如果您返回了一个模型(显然不是我故意实现的),它将被强制到一个列表中。也许我可以防御性地这样做,每当返回值不是四个支持的值之一时,只要一巴掌 list 但我不想太聪明,做太多的猜测。您的方法的另一个问题是map函数将在数据集的任意子集上被调用(对于较大的数据集),因此您将在输出中得到一个模型列表(在您对输出调用值之后)。所以现在你有多个支持向量机,你做什么,你把它当作一个陷阱?但是map阶段的子集是任意的,它们没有任何统计特性,比如随机化。在我看来,你认为rmr有超能力使svm函数并行分布,但它没有,它只会在不同数据块的集群上并行调用它。在一个小例子中,只有一个块,但这是骗人的。尝试 rmr.options(keyval.length = 3) 看看非常小的块会发生什么(不是为了生产)。另一种方法是在单机上加载的最大样本上构建模型,然后并行运行predict。当然,这在学习阶段没有那么大的可伸缩性,但我知道一些大的初创公司就是这样做的。看看乌里拉塞森(urilaserson)在cloudera博客上的重采样文章,我想它会给你一些好主意。最后,我们有一个专门的论坛,为rmr和谷歌集团的相关软件包,你非常欢迎加入我们的社区。

相关问题