【发布时间】:2016-02-23 13:22:25
【问题描述】:
我是机器学习的初学者,我无法解释我从第一个程序中获得的一些结果。这是设置:
我有一个书评数据集。这些书籍可以用大约 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"
我成功地生成了一个情节,但老实说,我不确定情节的含义。据我了解,这向我展示了任何给定功能对分数变量的影响程度。但是,我意识到这一定是个愚蠢的问题,我怎么知道影响是正面的还是负面的?
【问题讨论】:
标签: python numpy machine-learning statistics scikit-learn