随机森林分类器的决策路径

这是我在您的环境中运行的代码,我使用了RandomForestClassifier,并且正在尝试找出RandomForestClassifier中选定样本的decision_path

import numpy as npimport pandas as pdfrom sklearn.datasets import make_classificationfrom sklearn.ensemble import RandomForestClassifierX, y = make_classification(n_samples=1000,                           n_features=6,                           n_informative=3,                           n_classes=2,                           random_state=0,                           shuffle=False)# 创建数据框df = pd.DataFrame({'Feature 1':X[:,0],                                  'Feature 2':X[:,1],                                  'Feature 3':X[:,2],                                  'Feature 4':X[:,3],                                  'Feature 5':X[:,4],                                  'Feature 6':X[:,5],                                  'Class':y})y_train = df['Class']X_train = df.drop('Class',axis = 1)rf = RandomForestClassifier(n_estimators=50,                               random_state=0)rf.fit(X_train, y_train)

我目前进展到这一步:

# 提取实例i = 12的决策路径i_data = X_train.iloc[12].values.reshape(1,-1)d_path = rf.decision_path(i_data)print(d_path)

但输出结果不太合理:

(<1×7046 sparse matrix of type ‘<class ‘numpy.int64’>’with 486 stored elements in Compressed Sparse Row format>, array([ 0, 133, 282, 415, 588, 761, 910, 1041, 1182, 1309, 1432,1569, 1728, 1869, 2000, 2143, 2284, 2419, 2572, 2711, 2856, 2987,3128, 3261, 3430, 3549, 3704, 3839, 3980, 4127, 4258, 4389, 4534,4671, 4808, 4947, 5088, 5247, 5378, 5517, 5640, 5769, 5956, 6079,6226, 6385, 6524, 6655, 6780, 6925, 7046], dtype=int32))

我正在尝试找出数据框中某个样本的决策路径。有人能告诉我该怎么做吗?

理想的情况是像这个一样。


回答:

RandomForestClassifier.decision_path方法返回一个tuple,即(indicator, n_nodes_ptr)。请查看文档:这里

因此,您的变量node_indicator是一个元组,而不是您所认为的形式。元组对象没有名为’indices’的属性,这就是为什么当您执行以下操作时会出现错误:

node_index = node_indicator.indices[node_indicator.indptr[sample_id]:                                    node_indicator.indptr[sample_id + 1]]

请尝试:

(node_indicator, _) = rf.decision_path(X_train)

您还可以为森林中每个树的单个样本ID绘制决策树:

X_train = X_train.valuessample_id = 0for j, tree in enumerate(rf.estimators_):    n_nodes = tree.tree_.node_count    children_left = tree.tree_.children_left    children_right = tree.tree_.children_right    feature = tree.tree_.feature    threshold = tree.tree_.threshold    print("决策树{0}的决策路径".format(j))    node_indicator = tree.decision_path(X_train)    leave_id = tree.apply(X_train)    node_index = node_indicator.indices[node_indicator.indptr[sample_id]:                                        node_indicator.indptr[sample_id + 1]]    print('用于预测样本%s的规则: ' % sample_id)    for node_id in node_index:        if leave_id[sample_id] != node_id:            continue        if (X_train[sample_id, feature[node_id]] <= threshold[node_id]):            threshold_sign = "<="        else:            threshold_sign = ">"        print("决策节点ID %s : (X_train[%s, %s] (= %s) %s %s)"              % (node_id,                 sample_id,                 feature[node_id],                 X_train[sample_id, feature[node_id]],                 threshold_sign,                 threshold[node_id]))

请注意,在您的案例中,您有50个估计器,阅读起来可能会有些乏味。

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中创建了一个多类分类项目。该项目可以对…

发表回复

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