![](/img/trans.png)
[英]Where does scikit-learn hold the decision labels of each leaf node in its tree structure?
[英]Finding a corresponding leaf node for each data point in a decision tree (scikit-learn)
我正在使用python 3.4中的scikit-learn包中的決策樹分類器,我想為每個輸入數據點獲取相應的葉節點id。
例如,我的輸入可能如下所示:
array([[ 5.1, 3.5, 1.4, 0.2],
[ 4.9, 3. , 1.4, 0.2],
[ 4.7, 3.2, 1.3, 0.2]])
我們假設相應的葉節點分別為16,5和45。 我希望我的輸出是:
leaf_node_id = array([16, 5, 45])
我已經閱讀了關於SF的scikit-learn郵件列表和相關問題,但我仍然無法使其工作。 這是我在郵件列表中找到的一些提示,但仍然無效。
http://sourceforge.net/p/scikit-learn/mailman/message/31728624/
在一天結束時,我只想要一個函數GetLeafNode(clf,X_valida),使其輸出是相應葉節點的列表。 下面是重現我收到的錯誤的代碼。 所以,任何建議都將非常感激。
from sklearn.datasets import load_iris
from sklearn import tree
# load data and divide it to train and validation
iris = load_iris()
num_train = 100
X_train = iris.data[:num_train,:]
X_valida = iris.data[num_train:,:]
y_train = iris.target[:num_train]
y_valida = iris.target[num_train:]
# fit the decision tree using the train data set
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X_train, y_train)
# Now I want to know the corresponding leaf node id for each of my training data point
clf.tree_.apply(X_train)
# This gives the error message below:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-2ecc95213752> in <module>()
----> 1 clf.tree_.apply(X_train)
_tree.pyx in sklearn.tree._tree.Tree.apply (sklearn/tree/_tree.c:19595)()
ValueError: Buffer dtype mismatch, expected 'DTYPE_t' but got 'double'
由於scikit-learn 0.17,您可以使用DecisionTree對象的apply方法來獲取數據點在樹中結束的葉子的索引。 以neobot的答案為基礎:
from sklearn.datasets import load_iris
from sklearn import tree
# load data and divide it to train and validation
iris = load_iris()
num_train = 100
X_train = iris.data[:num_train,:]
X_valida = iris.data[num_train:,:]
y_train = iris.target[:num_train]
y_valida = iris.target[num_train:]
# fit the decision tree using the train data set
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X_train, y_train)
# Compute the leaf node id for each of my training data points
clf.apply(X_train)
產生輸出
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2])
我終於開始工作了。 這是一個基於我在scikit-learn郵件列表中的通信消息的解決方案:
在scikit-learn版本0.16.1之后,apply方法在clf.tree_
實現,因此,我按照以下步驟操作:
clf.tree_
apply
方法 X_train
, X_valida
)從float64
為float32
: X_train = X_train.astype('float32')
apply
方法: clf.tree_.apply(X_train)
,您將獲得每個數據點的葉節點ID。 這是最終的代碼:
from sklearn.datasets import load_iris
from sklearn import tree
# load data and divide it to train and validation
iris = load_iris()
num_train = 100
X_train = iris.data[:num_train,:]
X_valida = iris.data[num_train:,:]
y_train = iris.target[:num_train]
y_valida = iris.target[num_train:]
# convert data to float32
X_train = X_train.astype('float32')
# fit the decision tree using the train data set
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X_train, y_train)
# Now I want to know the corresponding leaf node id for each of my training data point
clf.tree_.apply(X_train)
# This gives the leaf node id:
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.