2016-05-10 33 views
2

我可以设法显示我的决策树后显示我labels的总概率,例如,我有一个表:Spark MLib决策树:按特征标签的可能性?

Total Predictions : 
    65% impressions 
    30% clicks 
    5% conversions 

但我的问题是features发现概率(或计数)(按节点),例如:

if feature1 > 5 
    if feature2 < 10 
     Predict Impressions 
     samples : 30 Impressions 
    else feature2 >= 10 
     Predict Clicks 
     samples : 5 Clicks 

Scikit自动完成它,我试图找到一种方法做它Spark

+0

你可以使用Scala吗? –

+0

@DanieldePaula,没关系。 – RoyaumeIX

+0

我对Scala有一个想法。当我有一段时间时,我会与你分享 –

回答

2

注意:以下解决方案仅适用于Scala。我没有找到一种方法来在Python中完成它。

假设你只是想树的可视化表示在你的榜样,也许一个选项,以适应目前在星火的GitHub上Node.scala代码的方法subtreeToString包括概率在每个节点拆分,如以下片段:

def subtreeToString(rootNode: Node, indentFactor: Int = 0): String = { 
    def splitToString(split: Split, left: Boolean): String = { 
    split.featureType match { 
     case Continuous => if (left) { 
     s"(feature ${split.feature} <= ${split.threshold})" 
     } else { 
     s"(feature ${split.feature} > ${split.threshold})" 
     } 
     case Categorical => if (left) { 
     s"(feature ${split.feature} in ${split.categories.mkString("{", ",", "}")})" 
     } else { 
     s"(feature ${split.feature} not in ${split.categories.mkString("{", ",", "}")})" 
     } 
    } 
    } 
    val prefix: String = " " * indentFactor 
    if (rootNode.isLeaf) { 
    prefix + s"Predict: ${rootNode.predict.predict} \n" 
    } else { 
    val prob = rootNode.predict.prob*100D 
    prefix + s"If ${splitToString(rootNode.split.get, left = true)} " + f"(Prob: $prob%04.2f %%)" + "\n" + 
     subtreeToString(rootNode.leftNode.get, indentFactor + 1) + 
     prefix + s"Else ${splitToString(rootNode.split.get, left = false)} " + f"(Prob: ${100-prob}%04.2f %%)" + "\n" + 
     subtreeToString(rootNode.rightNode.get, indentFactor + 1) 
    } 
} 

我测试了我的Iris dataset运行模式,我已经得到了以下结果:

scala> println(subtreeToString(model.topNode)) 

If (feature 2 <= -0.762712) (Prob: 35.35 %) 
Predict: 1.0 
Else (feature 2 > -0.762712) (Prob: 64.65 %) 
If (feature 3 <= 0.333333) (Prob: 52.24 %) 
    If (feature 0 <= -0.666667) (Prob: 92.11 %) 
    Predict: 3.0 
    Else (feature 0 > -0.666667) (Prob: 7.89 %) 
    If (feature 2 <= 0.322034) (Prob: 94.59 %) 
    Predict: 2.0 
    Else (feature 2 > 0.322034) (Prob: 5.41 %) 
    If (feature 3 <= 0.166667) (Prob: 50.00 %) 
    Predict: 3.0 
    Else (feature 3 > 0.166667) (Prob: 50.00 %) 
    Predict: 2.0 
Else (feature 3 > 0.333333) (Prob: 47.76 %) 
    Predict: 3.0 

一个类似的应用程序roach可用于创建具有此信息的树结构。主要区别是将打印的信息(split.feature,split.threshold,predict.prob等)存储为val并使用它们来构建结构。

+0

这正是我想要的! – RoyaumeIX

+0

嘿,伙计们,你们中的任何一个都有使用DataFrame API的解决方案吗?看到这个问题的一些代表!:http://stackoverflow.com/questions/40558567/how-to-view-random-forest-statistics-in-spark-scala – rtcode