sklearn GBDT 中的叶子值是什么,如何获取它们?
What are the leaf values in sklearn GBDT, and How do I obtain them?
我可以使用 the tree.export_graphviz function 将 GBDT 的结构导出到图像:
``` Python3
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.ensemble import GradientBoostingClassifier
clf = GradientBoostingClassifier(n_estimators=1) # set to 1 for the sake of simplicity
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
tree.export_graphviz(clf.estimators_[0,0], out_file='tree.dot')
check_call(['dot','-Tpng','tree.dot','-o','tree.png'])
```
This is the obtained image.
我想知道叶子上的 value
是什么?以及如何获得它们?
我试过 apply
和 decision_function
函数,都不行。
您可以使用其内部对象 tree_
及其属性访问每棵树的休假属性。 export_graphviz
正是使用这种方法。
考虑这段代码。对于每个属性,它在所有树节点上给出其值的数组:
print(clf.estimators_[0,0].tree_.feature)
print(clf.estimators_[0,0].tree_.threshold)
print(clf.estimators_[0,0].tree_.children_left)
print(clf.estimators_[0,0].tree_.children_right)
print(clf.estimators_[0,0].tree_.n_node_samples)
print(clf.estimators_[0,0].tree_.value.ravel())
输出将是
[ 2 -2 -2]
[ 2.45000005 -2. -2. ]
[ 1 -1 -1]
[ 2 -1 -1]
[150 50 100]
[ 3.51570624e-16 2.00000000e+00 -1.00000000e+00]
即你的树有3个节点,第一个比较特征值2
与2.45
等
根节点、左叶和右叶中的值分别为3e-16
、2
和-1
。
虽然这些值解释起来并不明显,因为树试图预测 GBDT 损失函数的梯度。
我可以使用 the tree.export_graphviz function 将 GBDT 的结构导出到图像:
``` Python3
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.ensemble import GradientBoostingClassifier
clf = GradientBoostingClassifier(n_estimators=1) # set to 1 for the sake of simplicity
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
tree.export_graphviz(clf.estimators_[0,0], out_file='tree.dot')
check_call(['dot','-Tpng','tree.dot','-o','tree.png'])
```
This is the obtained image.
我想知道叶子上的 value
是什么?以及如何获得它们?
我试过 apply
和 decision_function
函数,都不行。
您可以使用其内部对象 tree_
及其属性访问每棵树的休假属性。 export_graphviz
正是使用这种方法。
考虑这段代码。对于每个属性,它在所有树节点上给出其值的数组:
print(clf.estimators_[0,0].tree_.feature)
print(clf.estimators_[0,0].tree_.threshold)
print(clf.estimators_[0,0].tree_.children_left)
print(clf.estimators_[0,0].tree_.children_right)
print(clf.estimators_[0,0].tree_.n_node_samples)
print(clf.estimators_[0,0].tree_.value.ravel())
输出将是
[ 2 -2 -2]
[ 2.45000005 -2. -2. ]
[ 1 -1 -1]
[ 2 -1 -1]
[150 50 100]
[ 3.51570624e-16 2.00000000e+00 -1.00000000e+00]
即你的树有3个节点,第一个比较特征值2
与2.45
等
根节点、左叶和右叶中的值分别为3e-16
、2
和-1
。
虽然这些值解释起来并不明显,因为树试图预测 GBDT 损失函数的梯度。