如何获取scikit-learn分类器中最具信息量的特征?

像liblinear和nltk这样的机器学习包中的分类器提供了一个方法show_most_informative_features(),这对于调试特征非常有帮助:

viagra = None          ok : spam     =      4.5 : 1.0hello = True           ok : spam     =      4.5 : 1.0hello = None           spam : ok     =      3.3 : 1.0viagra = True          spam : ok     =      3.3 : 1.0casino = True          spam : ok     =      2.0 : 1.0casino = None          ok : spam     =      1.5 : 1.0

我的问题是,scikit-learn中的分类器是否实现了类似的功能。我查看了文档,但没有找到类似的内容。

如果还没有这样的功能,有人知道如何通过变通方法获取这些值吗?


回答:

在larsmans的代码帮助下,我为二元情况编写了以下代码:

def show_most_informative_features(vectorizer, clf, n=20):    feature_names = vectorizer.get_feature_names()    coefs_with_fns = sorted(zip(clf.coef_[0], feature_names))    top = zip(coefs_with_fns[:n], coefs_with_fns[:-(n + 1):-1])    for (coef_1, fn_1), (coef_2, fn_2) in top:        print "\t%.4f\t%-15s\t\t%.4f\t%-15s" % (coef_1, fn_1, coef_2, fn_2)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注