将scikit学习的DecisionTreeClassifier.tree_.value映射到预测的类 [英] mapping scikit-learn DecisionTreeClassifier.tree_.value to predicted class
问题描述
我在3类数据集上使用了scikit-learn DecissionTreeClassifier。在适合分类器之后,我访问tree_属性上的所有叶节点,以获得每个类在给定节点中结束的实例数量。
I am using a scikit-learn DecissionTreeClassifier on a 3 class dataset. After I fit the classifier I access all leaf nodes on the tree_ attribute in order to get the amount of instances that end up in a given node for each class.
clf = tree.DecisionTreeClassifier(max_depth=5)
clf.fit(X, y)
# lets assume there is a leaf node with id 5
print clf.tree_.value[5]
这将打印出来:
>>> array([[ 0., 1., 68.]])
但是...怎么办我知道该数组中的哪个位置属于哪个类?
分类器具有classes_属性,该属性也是一个列表
but ... how do I know which position in that array belongs to which class ? The classifier has a classes_ attribute which is also a list
>>> clf.classes_
array(['CLASS_1', 'CLASS_2', 'CLASS_3'], dtype=object)
也许值数组上的索引1与类数组的索引1上的类匹配,依此类推?
Maybe index 1 on the value array matches the class on index 1 of the classes array and so on?
推荐答案
在scikit-learm邮件列表中对此进行了询问,我的猜测是正确的。原来值数组上的索引1与class数组的索引1上的类匹配,依此类推
Asked about this on the scikit-learm mailing list and my guess was right. Turns out the index 1 on the value array matches the class on index 1 of the classes array and so on
这篇关于将scikit学习的DecisionTreeClassifier.tree_.value映射到预测的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!