在决策树中为每个数据点找到对应的叶节点 (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])

我已经阅读了scikit-learn邮件列表和SF上的相关问题,但仍然无法使其工作。以下是我在邮件列表中找到的一些提示,但仍然不起作用。

http://sourceforge.net/p/scikit-learn/mailman/message/31728624/

最终,我只想有一个函数GetLeafNode(clf, X_valida),其输出是对应叶节点的列表。以下是重现我收到的错误的代码。因此,任何建议都将非常受欢迎。

from sklearn.datasets import load_irisfrom sklearn import tree# load data and divide it to train and validationiris = load_iris()num_train = 100X_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 setclf = 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 pointclf.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邮件列表中的消息的一个解决方案:

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

  1. 更新scikit-learn到最新版本(0.16.1),这样你就可以使用clf.tree_中的apply方法
  2. 将输入数据数组(X_trainX_valida)从float64转换为float32,使用:X_train = X_train.astype('float32')
  3. 现在你可以这样使用apply方法:clf.tree_.apply(X_train),你将得到每个数据点的叶节点ID。

以下是最终代码:

from sklearn.datasets import load_irisfrom sklearn import tree# load data and divide it to train and validationiris = load_iris()num_train = 100X_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 float32X_train = X_train.astype('float32')# fit the decision tree using the train data setclf = 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 pointclf.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])

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注