解释来自 RandomForestClassifier 的特征重要性值

Interpreting feature importance values from a RandomForestClassifier

我是机器学习的初学者,我无法解释我从第一个程序中获得的一些结果。这是设置:

我有一个书评数据集。这些书可以用大约 1600 本书中的任意数量的限定词来标记。审阅这些书的人也可以用这些限定词来标记自己,以表明他们喜欢阅读带有该标签的东西。

数据集的每个限定符都有一列。对于每个评论,如果给定的限定符用于标记书籍和评论者,则记录值为 1。如果给定评论的给定限定词没有 "match",则记录值 0。

还有一个 "Score" 列,其中包含每个评论的整数 1-5(该评论的 "star rating")。我的目标是确定哪些特征对获得高分最重要。

这是我现在的代码 (https://gist.github.com/souldeux/99f71087c712c48e50b7):

def determine_feature_importance(df):
    #Determines the importance of individual features within a dataframe
    #Grab header for all feature values excluding score & ids
    features_list = df.columns.values[4::]
    print "Features List: \n", features_list

    #set X equal to all feature values, excluding Score & ID fields
    X = df.values[:,4::]

    #set y equal to all Score values
    y = df.values[:,0]

    #fit a random forest with near-default paramaters to determine feature importance
    print '\nCreating Random Forest Classifier...\n'
    forest = RandomForestClassifier(oob_score=True, n_estimators=10000)
    print '\nFitting Random Forest Classifier...\n'
    forest.fit(X,y)
    feature_importance = forest.feature_importances_
    print feature_importance

    #Make importances relative to maximum importance
    print "\nMaximum feature importance is currently: ", feature_importance.max()
    feature_importance = 100.0 * (feature_importance / feature_importance.max())
    print "\nNormalized feature importance: \n", feature_importance
    print "\nNormalized maximum feature importance: \n", feature_importance.max()
    print "\nTo do: set fi_threshold == max?"
    print "\nTesting: setting fi_threshhold == 1"
    fi_threshold=1

    #get indicies of all features over fi_threshold
    important_idx = np.where(feature_importance > fi_threshold)[0]
    print "\nRetrieved important_idx: ", important_idx

    #create a list of all feature names above fi_threshold
    important_features = features_list[important_idx]
    print "\n", important_features.shape[0], "Important features(>", fi_threshold, "% of max importance:\n", important_features

    #get sorted indices of important features
    sorted_idx = np.argsort(feature_importance[important_idx])[::-1]
    print "\nFeatures sorted by importance (DESC):\n", important_features[sorted_idx]

    #generate plot
    pos = np.arange(sorted_idx.shape[0]) + .5
    plt.subplot(1,2,2)
    plt.barh(pos,feature_importance[important_idx][sorted_idx[::-1]],align='center')
    plt.yticks(pos, important_features[sorted_idx[::-1]])
    plt.xlabel('Relative importance')
    plt.ylabel('Variable importance')
    plt.draw()
    plt.show()

    X = X[:, important_idx][:, sorted_idx]


    return "Feature importance determined"

我成功地生成了一个情节,但老实说我不确定情节的含义。据我了解,这向我展示了任何给定特征对分数变量的影响有多强烈。但是,我意识到这一定是一个愚蠢的问题,我怎么知道影响是积极的还是消极的?

简而言之你不会。决策树(随机森林的组成部分)不是这样工作的。如果你使用线性模型,那么特征是 "positive" 还是 "negative" 就很简单了,因为它对最终结果的唯一影响是 被添加(有权重) 。而已。然而,决策树的集成可以为每个特征制定任意复杂的规则,例如 "if book has red cover and have more than 100 pages then if it contains dragons it gets high score" 但 "if book has blue cover and more than 100 pages then if it contains dragons it gets low score" 等等。

特征重要性仅让您了解哪些特征有助于决策,而不是"which way",因为有时它会起作用,有时则相反。

你能做什么?您可以添加一些极端的简化 - 假设您只对完全没有其他功能的功能感兴趣,现在 - 一旦您知道哪些功能很重要,您就可以计算出该功能在每个 class 中出现的次数(分数在你的情况)。这样你就会得到分布

P(gets score X|has feature Y)

这将或多或少地向您展示它是否具有(边缘化后)正面或负面影响。

随机森林可以衡量class化任务中任何特征的相对重要性。

通常,我们会衡量如果我们失去该特征的真实值将会造成的损失。一次对一个特征进行打乱,并测量预测准确性的损失。

因为每次我们构建新的决策树时都会这样做,并且随机森林由几棵树组成,所以值是可靠的。

看看这个page.

从 forest.feature_importances_ 返回的数字越高,意味着它们在 这个 class 化任务 .

中越重要

但是你的情况不适合。我建议尝试 Multinomial Naive Bayes Classifier 并在训练后检查 feature_log_prob_。这样你就可以看到给定 class, P(x_i|y).

特征的概率