如何获得scikit学习分类器的最有用的功能? [英] How to get most informative features for scikit-learn classifiers?

查看:58
本文介绍了如何获得scikit学习分类器的最有用的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

liblinear和nltk等机器学习包中的分类器提供了方法show_most_informative_features(),这对于调试功能确实很有帮助:

The classifiers in machine learning packages like liblinear and nltk offer a method show_most_informative_features(), which is really helpful for debugging features:

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

我的问题是,是否对scikit-learn中的分类器实施了类似的操作.我搜索了文档,但找不到类似的东西.

My question is if something similar is implemented for the classifiers in scikit-learn. I searched the documentation, but couldn't find anything the like.

如果还没有这样的功能,有人知道如何获得这些值的解决方法吗?

If there is no such function yet, does somebody know a workaround how to get to those values?

非常感谢!

推荐答案

在larsmans代码的帮助下,我想到了以下二进制情况的代码:

With the help of larsmans code I came up with this code for the binary case:

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)

这篇关于如何获得scikit学习分类器的最有用的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆