【问题标题】:SparkR summary() extractingSparkR summary() 提取
【发布时间】:2017-05-03 08:15:00
【问题描述】:

我对 SparkR 中使用随机森林回归的 summary() 方法有疑问。模型构建过程运行良好,但我对算法结果之一的 featureImportance 很感兴趣。我想将 featureImportance 变量存储到 SparkDataFrame 中以可视化它们,但我不知道如何传输/提取它。

model <- spark.randomForest(x1, x2 , x3, type = "regression", maxDepth = 30, maxBins = 50, numTrees=50, impurity="variance", featureSubsetStrategy="all")

summaryRF <- summary(model)

summaryRF$feature:
1. 'x1'
2. 'x2'
3. 'x3'

summaryRF$featureImportances: 
'(3,[0,1,2],[0.01324152135,0.0545454422,0.0322122334])'

是否有任何解决方案可以从列表对象中获取 featureImportance 值并将其存储在 SparkDataFrame 中?

使用 collect() 方法给出以下错误代码:

(函数(类,fdef,mtable)中的错误:无法为签名“字符”的函数“收集”找到继承的方法

【问题讨论】:

    标签: r apache-spark sparkr


    【解决方案1】:

    summaryRF 不再是SparkDataFrame,这就是为什么collect 不起作用:)

    summaryRF$featureImportances 是一个character string(在Spark 一侧它是一个SparseVector,目前(v.2.1.0)不能序列化到R,我猜这就是它的原因被强制转换为string)。

    据我所知,您必须通过直接操作字符串来提取相关位:

    # extract the feature indexes and feature importances strings:
    fimpList <- strsplit(gsub("\\(.*?\\[","",summaryRF$featureImportances),"\\],\\[")
    
    # split the index and feature importances strings into vectors (and remove "])" from the last record):
    fimp <- lapply(fimpList, function(x) strsplit(gsub("\\]\\)","",x),","))
    
    # it's now a list of lists, but you can make this into a dataframe if you like:
    fimpDF <- as.data.frame(do.call(cbind,(fimp[[1]])))
    

    eta:顺便说一下,Spark 中的索引从 0 开始,所以如果你想在加入 summaryRf$features 中的特征名称时合并 summaryRF$featureImportances 中的特征索引,你必须考虑到这一点:

    featureNameAndIndex <- data.frame(featureName = unlist(summaryRf$features),
                                      featureIndex = c(0:(length(summaryRf$features)-1))),
                                      stringsAsFactors = FALSE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      相关资源
      最近更新 更多