sou*_*eux 6 python statistics numpy machine-learning scikit-learn
我是机器学习的初学者,我无法解释我从第一个程序中获得的一些结果。这是设置:
我有一个书评数据集。这些书可以在大约 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"
Run Code Online (Sandbox Code Playgroud)
我成功地生成了一个情节,但老实说,我不确定情节的含义。据我了解,这向我展示了任何给定特征对分数变量的影响有多大。但是,我意识到这一定是一个愚蠢的问题,我怎么知道影响是积极的还是消极的?
总之你没有。决策树(随机森林的构建块)不能以这种方式工作。如果您使用线性模型,那么特征是“正”还是“负”的区别非常简单,因为它对最终结果的唯一影响是添加(带权重)。而已。然而,决策树的集合对于每个特征可以有任意复杂的规则,例如“如果书有红色封面并且有超过 100 页,那么如果它包含龙,它会得到高分”但是“如果书有蓝色封面并且超过 100页然后如果它包含龙,它会得到低分”等等。
特征重要性只让你知道哪些特征有助于决策,而不是“哪种方式”,因为有时它会起作用,有时会起作用。
你可以做什么?您可以添加一些极端的简化 - 假设您只对完全没有其他功能的功能感兴趣,现在 - 一旦您知道哪些是重要的,您就可以计算此功能在每个类中的次数(您的情况下的分数)。这样你就会得到分布
P(gets score X|has feature Y)
Run Code Online (Sandbox Code Playgroud)
如果它具有(在边缘化后)正面或负面影响,它将或多或少地显示给您。