当你安装一个基于树模型,如决策树,随机森林,或梯度提高了树,是很有帮助的,可以复习功能重要性水平随着功能的名字。通常模型在SparkML符合管道的最后阶段。从管道中提取相关特征信息树模型,您必须提取正确的管道阶段。你可以提取特征的名称VectorAssembler对象:
%从pyspark.ml python。功能导入StringIndexer, VectorAssembler pyspark.ml。分类从pyspark进口DecisionTreeClassifier。毫升进口管道管道=管道(阶段=[索引器、汇编、decision_tree) DTmodel = pipeline.fit va = DTmodel(火车)。阶段[2]= DTmodel树。阶段[1]显示(树)#可视化决策树模型打印(tree.toDebugString) #打印决策树模型的节点列表(zip (va.getInputCols (), tree.featureImportances))
您还可以使用交叉验证器优化基于树模型在最后阶段的管道。可视化决策树并打印功能重要性水平,你提取bestModel从CrossValidator对象:
%从pyspark.ml python。优化进口ParamGridBuilder CrossValidator简历= CrossValidator(估计量= decision_tree estimatorParamMaps = paramGrid,评估者=评估者,numFolds = 3) pipelineCV =管道(阶段=[索引器、汇编、cv) DTmodelCV = pipelineCV.fit va = DTmodelCV(火车)。阶段[2]treeCV = DTmodelCV.stages [1]。bestModeldisplay(treeCV) #visualize the best decision tree model print(treeCV.toDebugString) #print the nodes of the decision tree model list(zip(va.getInputCols(), treeCV.featureImportances))