决策树的实现和排序重要特征

from sklearn.tree import DecisionTreeClassifier
from adspy_shared_utilities import plot_feature_importances

tree=DecisionTreeClassifier(random_state=0) 
tree.fit(X_train2,y_train2) list(X_mush.columns[tree.feature_importances_.argsort()[::
-1][0:5]])
  • 训练的决策树模型为tree,返回决策树训练的前五个重要特征的list
  • 返回排序的索引用argsort函数,函数用法见https://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html#numpy.argsort
  • 取倒数5个字符用了[::-1][0:5]这样的索引

 

posted @ 2017-09-07 19:47  kekukele  阅读(859)  评论(0编辑  收藏  举报