【问题标题】:training a classifier with data within a partition用分区内的数据训练分类器
【发布时间】:2020-06-07 11:50:07
【问题描述】:

当分类算法依赖于分区索引时,如何使用分区内的实例训练分类器?例如,假设如下代码sn -p:

val data = MLUtils.loadLibSVMFile(sc, "path to SVM file")
val r = data.mapPartitionsWithIndex((index,localdata)=>{
  if (index % 2 == 0)
  {
    // train a NaiveBayes with localdata
    NaiveBayes.train(localdata)    // Error => found:iterator[LabeledPoint] , required: RDD[labeledPoint]
  }
  else
  {
    // train a DecisionTree classifier with localdata
    DecisionTree.train(localdata)    // Error => found:iterator[LabeledPoint] , required: RDD[labeledPoint]
  }
})

在我看来这个错误是正确的,因为这些任务是在它们分离的 JVM 中执行的,并且无法从映射任务中分发。这就是为什么我无法在我的任务中访问 SparkContext 的原因。但是,有没有人有其他建议来实现我的目的?

【问题讨论】:

  • 为什么不能给RDD本身添加分区索引,然后通过添加过滤器(index % 2==0)将单个RDD with index分成naivebayes rdddecision tree rdd。然后,您可以在 rdd 本身上应用相应的算法
  • 你的意思是我应该在我的驱动程序中为我的数据的每个元素附加一个分区索引?如果是,我如何获取每个元素的分区 ID?你能帮忙写一个小代码sn-p吗?
  • 你能告诉我用例吗?具体你为什么要根据分区号来选择算法
  • 我想设计一个异构集成分类器,它对不同的数据部分使用不同的学习算法。例如,分区 #1 的 NaiveBayes,分区 #2 的决策树,#3 的 SVm 等等。
  • 如何对数据进行分区?划分您的 rdd 分区的任何标准?

标签: scala apache-spark mapreduce


【解决方案1】:

基于上述 cmets 部分的讨论 - 你可以试试这个-

   val rdd = MLUtils.loadLibSVMFile(sc, "path to SVM file")

    // approach -1
    val nb = rdd.sample(withReplacement = false, fraction = 0.5) // sample 50% of the record
    val dt = rdd.sample(withReplacement = false, fraction = 0.5) // sample 50% of the record

    //or approach-2 
    val (nb, dt) = rdd.randomSplit(Array(0.5, 0.5))

    // apply algo
    NaiveBayes.train(nb)
    DecisionTree.train(dt, strategy= ..)

【讨论】:

    猜你喜欢
    • 2016-07-03
    • 2018-04-19
    • 2013-04-11
    • 1970-01-01
    • 2016-08-07
    • 2018-01-01
    • 2015-09-21
    • 1970-01-01
    • 2013-08-30
    相关资源
    最近更新 更多