【发布时间】:2018-01-01 16:54:27
【问题描述】:
我想获得决策树上的重要特征(BaggingClasifier(estimator=DecisionTreeClassifier)。如果我使用数据集(n=8900)的所有特征进行计算,即 ist max_features=1.0(float),我可以将它们正确编入索引。但是,如果我将 max_features 更改为任何值(例如:181),则索引是根据 max_features 中使用的集合的新编号给出的。所以,我不知道哪些是实际特征/重要特征在我的原始数据集上。
Hier 是我的代码:
dt= BaggingClassifier(base_estimator=DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
max_features=None, max_leaf_nodes=20, min_impurity_split=0.2,
min_samples_leaf=6, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort=False, random_state=7,
splitter='best'),
bootstrap=False, bootstrap_features=True, max_features=181,
max_samples=1.0, n_estimators=3, n_jobs=2, oob_score=False,
random_state=7, verbose=0, warm_start=False) #min_samples_leaf=10
# Fit the model
fit_dt= dt.fit(X_train, Y_train)
print(dir(fit_dt))
trees = dt.estimators_
print(trees)
#--------------------
# Print the important features (way 1)
feature_importances = np.mean([
tree.feature_importances_ for tree in dt.estimators_], axis=0)
print(feature_importances)
indices = np.argsort(feature_importances)[::-1]
print("Feature ranking:")
for f in range(X_train.shape[1]):
print("%d. feature %d (%f)" % (f + 1, indices[f], feature_importances[indices[f]]))
# Plotting the trees
from sklearn.externals.six import StringIO
from sklearn.tree import export_graphviz
for i in range(0,len(dt.estimators_)): # since in dt.estimators we have a list with all trees
t = dt.estimators_[i]
export_graphviz(t, out_file='tree.dot') # necessary to plot the graph
dot_data = StringIO() # need to understand but it probably relates to read of strings
export_graphviz(t, out_file=dot_data, filled=True, class_names= target_names, rounded=True, special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
img = Image(graph.create_png())
print(dir(img)) # check what are the possibilities in graph.create_png
with open("my_tree_" + str(i) + ".png", "wb") as png:
png.write(img.data)
返回特征编号,但不返回原始数据的特征编号并报错:
Error Message: IndexError: index 181 is out of bounds for axis 0 with size 181
在使用所有功能的情况下效果很好
Feature ranking: (my code)
1. feature 976 (0.077076)
2. feature 2119 (0.071093)
3. feature 7481 (0.065344)
4. feature 9092 (0.042598)
5. feature 7986 (0.040946)
6. feature 9642 (0.039385)
7. feature 3032 (0.039291)
8. feature 4299 (0.038662)
9. feature 8334 (0.037809)
10. feature 363 (0.037768)
Feature raking: (@akaran code, returns different results)
8157 0.213513
5406 0.081889
1461 0.078714
7085 0.059718
3213 0.048554
1901 0.039385
1486 0.038662
1470 0.037289
8328 0.036474
8349 0.027375
我将不胜感激。
【问题讨论】:
-
print dt.estimators_features_的输出是什么? (在你使用max_features=181的情况下) -
[array([1593, 8114, 752, 3098, 2938, 3598, 5139, 4529, ...]), array([5947, 8040, 5958, 5451, 383, 8115, 8066 , 4560, 2674, 5471, 5380, ...]), 数组([5489, 6586, 3132, 6926, 313, 3231, 724, 6106, 274, 581, ... ]) 。到目前为止,它为我提供了 181 个特征,其索引(数字)与原始数据中的完全相同。
-
@mkaran 如果我只是尝试通过 np.argsort(dt.estimators_features_)[::-1] 对它们进行排序,我得到与上述相同的数组,但使用索引(数字)取而代之的是 0-181。
标签: indexing scikit-learn decision-tree