gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 16:29:08 25 4
gpt4 key购买 nike

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

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

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

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

这是我现在拥有的代码 ( 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"

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

最佳答案

简而言之,您没有。决策树(随机森林的组成部分)不是这样工作的。如果您使用线性模型,那么特征是“正面”还是“负面”就很简单了,因为它对最终结果的唯一影响是被添加(带有权重)。而已。然而,决策树的集合可以对每个特征有任意复杂的规则,例如“如果书有红色封面并且有超过 100 页,那么如果它包含龙,它会得到高分”但是“如果书有蓝色封面并且超过 100 页页面,然后如果它包含龙,它会得到低分”等等。

特征重要性仅让您了解哪些特征对决策有贡献,而不是“以哪种方式”,因为有时它会以这种方式起作用,有时会以另一种方式起作用。

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

P(gets score X|has feature Y)

这将或多或少地告诉您它是否具有(在边缘化之后)积极或消极的影响。

关于python - 解释来自 RandomForestClassifier 的特征重要性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33837125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com