在决策树中为每个数据点查找相应的叶节点(scikit-learn)

时间:2015-05-23 01:42:12

标签: python machine-learning scikit-learn decision-tree

我在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/

在一天结束时,我只想拥有一个函数Ge​​tLeafNode(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'

2 个答案:

答案 0 :(得分:4)

由于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])

答案 1 :(得分:3)

我终于开始工作了。以下是基于我在scikit-learn邮件列表中的message信函的解决方案:

在scikit-learn版本0.16.1之后,apply方法在clf.tree_中实现,因此,我按照以下步骤操作:

  1. 更新scikit - 了解最新版本(0.16.1),以便您可以使用apply
  2. 中的clf.tree_方法
  3. 使用:X_train
  4. 将输入数据数组(X_validafloat64)从float32转换为X_train = X_train.astype('float32')
  5. 现在您可以通过以下方式使用apply方法:clf.tree_.apply(X_train),您将获得每个数据点的叶节点ID。
  6. 以下是最终代码:

    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])