不幸的是,我找不到任何方法直接在 PySpark 或 Spark (Scala API) 中访问节点。但是有一种方法可以从一个根节点开始,遍历到不同的节点。
(我刚刚在这里提到了杂质,但对于深度,可以很容易地用impurity 替换subtreeDepth。)
假设决策树模型实例为dt:
PySpark
root = dt.call("topNode")
root.impurity() # gives the impurity of the root node
现在如果我们看看适用于root的方法:
dir(root)
[u'apply', u'deepCopy', u'emptyNode', u'equals', 'getClass', u'getNode', u'hashCode', u'id', 'impurity', u'impurity_$eq', u'indexToLevel', u'initializeLogIfNecessary', u'isLeaf', u'isLeaf_$eq', u'isLeftChild', u'isTraceEnabled', u'leftChildIndex', u'leftNode', u'leftNode_$eq', u'log', u'logDebug', u'logError', u'logInfo', u'logName', u'logTrace', u'logWarning', u'maxNodesInLevel', u'notify', u'notifyAll', u'numDescendants', u'org$apache$spark$internal$Logging$$log_', u'org$apache$spark$internal$Logging$$log__$eq', u'parentIndex', u'predict', u'predict_$eq', u'rightChildIndex', u'rightNode', u'rightNode_$eq', u'split', u'split_$eq', u'startIndexInLevel', u'stats', u'stats_$eq', u'subtreeDepth', u'subtreeIterator', u'subtreeToString', u'subtreeToString$default$1', u'toString', u'wait']
我们可以这样做:
root.leftNode().get().impurity()
这可能会在树中更深,例如:
root.leftNode().get().rightNode().get().impurity()
由于在应用leftNode() 或rightNode() 之后,我们得到一个option,应用get 或getOrElseis necessary to get to the desiredNode` 类型。
如果你想知道我是怎么弄到这些奇怪的方法的,我得承认,我作弊了!!,即我首先研究了 Scala API:
火花
以下行与上面的行完全相同,并且假设dt 相同,则给出相同的结果:
val root = dt.topNode
root.impurity
我们可以这样做:
root.leftNode.get.impurity
这可能会在树中更深,例如:
root.leftNode.get.rightNode.get.impurity