R - Hadoop - rmr2 - SVM 模型 - 将结果"list"类转换为原始类"svm.formula" "svm"



我有以下R配置:

操作系统:LinuxR 3.0.1版(2013-05-16)rmr2版本2.2.1rhdfs 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调用相同的类呢?

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

相关内容

  • 没有找到相关文章

最新更新