natural language toolkit interface to scikitlearn classifiers lars buitinck l j buitinckuva nl url https www nltk org for license information see license txt scikitlearn https scikitlearn org is a machine learning library for python it supports many classification algorithms including svms naive bayes logistic regression maxent and decision trees this package implements a wrapper around scikitlearn classifiers to use this wrapper construct a scikitlearn estimator object then use that to construct a sklearnclassifier e g to wrap a linear svm with default settings from sklearn svm import linearsvc from nltk classify scikitlearn import sklearnclassifier classif sklearnclassifierlinearsvc a scikitlearn classifier may include preprocessing steps when it s wrapped in a pipeline object the following constructs and wraps a naive bayes text classifier with tfidf weighting and chisquare feature selection to get the best 1000 features from sklearn featureextraction text import tfidftransformer from sklearn featureselection import selectkbest chi2 from sklearn naivebayes import multinomialnb from sklearn pipeline import pipeline pipeline pipeline tfidf tfidftransformer chi2 selectkbestchi2 k1000 nb multinomialnb classif sklearnclassifierpipeline wrapper for scikitlearn classifiers def initself estimator dtypefloat sparsetrue self clf estimator self encoder labelencoder self vectorizer dictvectorizerdtypedtype sparsesparse def reprself return sklearnclassifierr self clf def classifymanyself featuresets x self vectorizer transformfeaturesets classes self encoder classes return classesi for i in self clf predictx def probclassifymanyself featuresets x self vectorizer transformfeaturesets yprobalist self clf predictprobax return self makeprobdistyproba for yproba in yprobalist def labelsself return listself encoder classes def trainself labeledfeaturesets x y listziplabeledfeaturesets x self vectorizer fittransformx y self encoder fittransformy self clf fitx y return self def makeprobdistself yproba classes self encoder classes return dictionaryprobdistclassesi p for i p in enumerateyproba if name main from sklearn linearmodel import logisticregression from sklearn naivebayes import bernoullinb from nltk classify util import namesdemo namesdemofeatures bernoulli naive bayes is designed for binary classification we set the binarize option to false since we know we re passing boolean features printscikitlearn naive bayes namesdemo sklearnclassifierbernoullinbbinarizefalse train featuresnamesdemofeatures the c parameter on logistic regression maxent controls regularization the higher it s set the less regularized the classifier is printnnscikitlearn logistic regression namesdemo sklearnclassifierlogisticregressionc1000 train featuresnamesdemofeatures natural language toolkit interface to scikit learn classifiers lars buitinck l j buitinck uva nl url https www nltk org for license information see license txt scikit learn https scikit learn org is a machine learning library for python it supports many classification algorithms including svms naive bayes logistic regression maxent and decision trees this package implements a wrapper around scikit learn classifiers to use this wrapper construct a scikit learn estimator object then use that to construct a sklearnclassifier e g to wrap a linear svm with default settings from sklearn svm import linearsvc from nltk classify scikitlearn import sklearnclassifier classif sklearnclassifier linearsvc a scikit learn classifier may include preprocessing steps when it s wrapped in a pipeline object the following constructs and wraps a naive bayes text classifier with tf idf weighting and chi square feature selection to get the best 1000 features from sklearn feature_extraction text import tfidftransformer from sklearn feature_selection import selectkbest chi2 from sklearn naive_bayes import multinomialnb from sklearn pipeline import pipeline pipeline pipeline tfidf tfidftransformer chi2 selectkbest chi2 k 1000 nb multinomialnb classif sklearnclassifier pipeline wrapper for scikit learn classifiers param estimator scikit learn classifier object param dtype data type used when building feature array scikit learn estimators work exclusively on numeric data the default value should be fine for almost all situations param sparse whether to use sparse matrices internally the estimator must support these not all scikit learn classifiers do see their respective documentation and look for sparse matrix the default value is true since most nlp problems involve sparse feature sets setting this to false may take a great amount of memory type sparse boolean classify a batch of samples param featuresets an iterable over featuresets each a dict mapping strings to either numbers booleans or strings return the predicted class label for each input sample rtype list compute per class probabilities for a batch of samples param featuresets an iterable over featuresets each a dict mapping strings to either numbers booleans or strings rtype list of probdisti the class labels used by this classifier rtype list train fit the scikit learn estimator param labeled_featuresets a list of featureset label where each featureset is a dict mapping strings to either numbers booleans or strings bernoulli naive bayes is designed for binary classification we set the binarize option to false since we know we re passing boolean features the c parameter on logistic regression maxent controls regularization the higher it s set the less regularized the classifier is
from nltk.classify.api import ClassifierI
from nltk.probability import DictionaryProbDist

try:
    from sklearn.feature_extraction import DictVectorizer
    from sklearn.preprocessing import LabelEncoder
except ImportError:
    pass

__all__ = ["SklearnClassifier"]


class SklearnClassifier(ClassifierI):
    

    def __init__(self, estimator, dtype=float, sparse=True):
        
        self._clf = estimator
        self._encoder = LabelEncoder()
        self._vectorizer = DictVectorizer(dtype=dtype, sparse=sparse)

    def __repr__(self):
        return "<SklearnClassifier(%r)>" % self._clf

    def classify_many(self, featuresets):
        
        X = self._vectorizer.transform(featuresets)
        classes = self._encoder.classes_
        return [classes[i] for i in self._clf.predict(X)]

    def prob_classify_many(self, featuresets):
        
        X = self._vectorizer.transform(featuresets)
        y_proba_list = self._clf.predict_proba(X)
        return [self._make_probdist(y_proba) for y_proba in y_proba_list]

    def labels(self):
        
        return list(self._encoder.classes_)

    def train(self, labeled_featuresets):
        

        X, y = list(zip(*labeled_featuresets))
        X = self._vectorizer.fit_transform(X)
        y = self._encoder.fit_transform(y)
        self._clf.fit(X, y)

        return self

    def _make_probdist(self, y_proba):
        classes = self._encoder.classes_
        return DictionaryProbDist({classes[i]: p for i, p in enumerate(y_proba)})


if __name__ == "__main__":
    from sklearn.linear_model import LogisticRegression
    from sklearn.naive_bayes import BernoulliNB

    from nltk.classify.util import names_demo, names_demo_features

    
    
    print("scikit-learn Naive Bayes:")
    names_demo(
        SklearnClassifier(BernoulliNB(binarize=False)).train,
        features=names_demo_features,
    )

    
    
    print("\n\nscikit-learn logistic regression:")
    names_demo(
        SklearnClassifier(LogisticRegression(C=1000)).train,
        features=names_demo_features,
    )