这是我在您的环境中运行它的代码,我正在使用RandomForestClassifier
我正在尝试找出decision_path
对于选定的样本RandomForestClassifier
.
import numpy as np
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
X, y = make_classification(n_samples=1000,
n_features=6,
n_informative=3,
n_classes=2,
random_state=0,
shuffle=False)
# Creating a dataFrame
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)
我得到的最远的是:
#Extracting the decision path for instance i = 12
i_data = X_train.iloc[12].values.reshape(1,-1)
d_path = rf.decision_path(i_data)
print(d_path)
但输出没有多大意义:
(' 的稀疏矩阵
以压缩稀疏行格式存储 486 个元素>, 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))
我试图找出数据框中粒子样本的决策路径。谁能告诉我该怎么做?
这个想法是有类似的东西this http://scikit-learn.org/stable/auto_examples/tree/plot_unveil_tree_structure.html.