(vectorizer, classifier, n=100)
See: https://stackoverflow.com/a/26980472

    Identify most important features if given a vectorizer and binary classifier. Set n to the number
    of weighted features you would like to show. (Note: current implementation merely prints and does not
    return top classes.)
See: https://stackoverflow.com/a/26980472
See : https : // stackoverflow . com / a / 26980472
def most_informative_feature_for_binary_classification(vectorizer, classifier, n=100):
    """
    See: https://stackoverflow.com/a/26980472

    Identify most important features if given a vectorizer and binary classifier. Set n to the number
    of weighted features you would like to show. (Note: current implementation merely prints and does not
    return top classes.)
    """

    class_labels = classifier.classes_
    feature_names = vectorizer.get_feature_names()
    topn_class1 = sorted(zip(classifier.coef_[0], feature_names))[:n]
    topn_class2 = sorted(zip(classifier.coef_[0], feature_names))[-n:]

    for coef, feat in topn_class1:
        print(class_labels[0], coef, feat)

    print()

    for coef, feat in reversed(topn_class2):
        print(class_labels[1], coef, feat)
def most_informative_feature_for_binary_classification ( vectorizer , classifier , n = 100 ) : class_labels = classifier . classes_ feature_names = vectorizer . get_feature_names ( ) topn_class1 = sorted ( zip ( classifier . coef_ [ 0 ] , feature_names ) ) [ : n ] topn_class2 = sorted ( zip ( classifier . coef_ [ 0 ] , feature_names ) ) [ - n : ] for coef , feat in topn_class1 : print ( class_labels [ 0 ] , coef , feat ) print ( ) for coef , feat in reversed ( topn_class2 ) : print ( class_labels [ 1 ] , coef , feat )