如何在pyspark中可视化决策树模型/对象?

如何在pyspark中可视化决策树模型/对象?,pyspark,apache-spark-mllib,decision-tree,apache-spark-ml,Pyspark,Apache Spark Mllib,Decision Tree,Apache Spark Ml,是否有任何方法可以可视化/绘制使用pyspark中的mllib或ml库创建的决策树。还有如何获取叶节点中的记录数等信息。谢谢首先,您需要使用model.toDebugString在随机林模型上获得这样的输出: "RandomForestClassificationModel (uid=rfc_6c4ceb92ba78) with 20 trees Tree 0 (weight 1.0): If (feature 0 <= 3="" 10="" 1.0)="" if="" (f

是否有任何方法可以可视化/绘制使用pyspark中的mllib或ml库创建的决策树。还有如何获取叶节点中的记录数等信息。谢谢

首先,您需要使用model.toDebugString在随机林模型上获得这样的输出:

 "RandomForestClassificationModel (uid=rfc_6c4ceb92ba78) with 20 trees
  Tree 0 (weight 1.0):
    If (feature 0 <= 3="" 10="" 1.0)="" if="" (feature="" <="0.0)" predict:="" 0.0="" else=""> 6.0)
       Predict: 0.0
     Else (feature 10 > 0.0)
      If (feature 12 <= 12="" 63.0)="" predict:="" 0.0="" else="" (feature=""> 63.0)
       Predict: 0.0
    Else (feature 0 > 1.0)
     If (feature 13 <= 3="" 1.0)="" if="" (feature="" <="3.0)" predict:="" 0.0="" else=""> 3.0)
       Predict: 1.0
     Else (feature 13 > 1.0)
      If (feature 7 <= 7="" 1.0)="" predict:="" 0.0="" else="" (feature=""> 1.0)
       Predict: 0.0
  Tree 1 (weight 1.0):
    If (feature 2 <= 11="" 15="" 1.0)="" if="" (feature="" <="0.0)" predict:="" 0.0="" else=""> 0.0)
       Predict: 1.0
     Else (feature 15 > 0.0)
      If (feature 11 <= 11="" 0.0)="" predict:="" 0.0="" else="" (feature=""> 0.0)
       Predict: 1.0
    Else (feature 2 > 1.0)
     If (feature 12 <= 5="" 31.0)="" if="" (feature="" <="0.0)" predict:="" 0.0="" else=""> 0.0)
       Predict: 0.0
     Else (feature 12 > 31.0)
      If (feature 3 <= 3="" 4.0)="" predict:="" 0.0="" else="" (feature=""> 4.0)
       Predict: 0.0
  Tree 2 (weight 1.0):
    If (feature 8 <= 4="" 6="" 1.0)="" if="" (feature="" <="2.0)" predict:="" 0.0="" else=""> 10875.0)
       Predict: 1.0
     Else (feature 6 > 2.0)
      If (feature 1 <= 1="" 36.0)="" predict:="" 0.0="" else="" (feature=""> 36.0)
       Predict: 1.0
    Else (feature 8 > 1.0)
     If (feature 5 <= 4="" 0.0)="" if="" (feature="" <="4113.0)" predict:="" 0.0="" else=""> 4113.0)
       Predict: 1.0
     Else (feature 5 > 0.0)
      If (feature 11 <= 11="" 2.0)="" predict:="" 0.0="" else="" (feature=""> 2.0)
       Predict: 0.0
  Tree 3 ...
包含20棵树的随机森林分类模型(uid=rfc_6c4ceb92ba78) 树0(权重1.0): 如果(功能0.0) 如果(功能部件12 63.0) 预测:0.0 其他(功能0>1.0) 如果(功能部件13 1.0) 如果(功能7 1.0) 预测:0.0 树1(重量1.0): 如果(功能2 0.0) 如果(功能部件11 0.0) 预测:1.0 其他(功能2>1.0) 如果(功能12 31.0) 如果(功能3 4.0) 预测:0.0 树2(重量1.0): 如果(功能8 2.0) 如果(功能1 36.0) 预测:1.0 其他(功能8>1.0) 如果(功能部件5 0.0) 如果(功能部件11 2.0) 预测:0.0 树3。。。
将其保存在某个.txt文件下,然后使用:

您可以获得所有叶节点的统计数,如杂质、增益、基尼、模型数据文件分类到每个标签中的元素数组

数据文件位于保存模型/数据/

model.save(location)
modeldf = spark.read.parquet(location+"data/*")
此文件包含决策树甚至随机林所需的大部分元数据

noderows = modeldf.select("id","prediction","leftChild","rightChild","split").collect()
df = pd.Dataframe([[rw['id'],rw['gain],rw['impurity'],rw['gini']] for rw in noderows if rw['leftChild'] < 0 and rw['rightChild'] < 0])
df.show()
noderows=modeldf.select(“id”、“prediction”、“leftChild”、“righchild”、“split”).collect()
df=pd.如果rw['leftChild']<0且rw['righchild']<0,则节点视图中rw的数据帧([[rw['id']、rw['gain]、rw['杂质']、rw['gini']]
df.show()

使用python绘制pyspark决策树也很好。我们是否只在pyspark中使用了一些东西而不是使用d3.js。另外model.toDebugString不会给出进入每个节点的数据的百分比。