scikitlearn.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Natural Language Toolkit: Interface to scikit-learn classifiers
  2. #
  3. # Author: Lars Buitinck <L.J.Buitinck@uva.nl>
  4. # URL: <http://nltk.org/>
  5. # For license information, see LICENSE.TXT
  6. """
  7. scikit-learn (http://scikit-learn.org) is a machine learning library for
  8. Python. It supports many classification algorithms, including SVMs,
  9. Naive Bayes, logistic regression (MaxEnt) and decision trees.
  10. This package implements a wrapper around scikit-learn classifiers. To use this
  11. wrapper, construct a scikit-learn estimator object, then use that to construct
  12. a SklearnClassifier. E.g., to wrap a linear SVM with default settings:
  13. >>> from sklearn.svm import LinearSVC
  14. >>> from nltk.classify.scikitlearn import SklearnClassifier
  15. >>> classif = SklearnClassifier(LinearSVC())
  16. A scikit-learn classifier may include preprocessing steps when it's wrapped
  17. in a Pipeline object. The following constructs and wraps a Naive Bayes text
  18. classifier with tf-idf weighting and chi-square feature selection to get the
  19. best 1000 features:
  20. >>> from sklearn.feature_extraction.text import TfidfTransformer
  21. >>> from sklearn.feature_selection import SelectKBest, chi2
  22. >>> from sklearn.naive_bayes import MultinomialNB
  23. >>> from sklearn.pipeline import Pipeline
  24. >>> pipeline = Pipeline([('tfidf', TfidfTransformer()),
  25. ... ('chi2', SelectKBest(chi2, k=1000)),
  26. ... ('nb', MultinomialNB())])
  27. >>> classif = SklearnClassifier(pipeline)
  28. """
  29. from __future__ import print_function, unicode_literals
  30. from six.moves import zip
  31. from nltk.classify.api import ClassifierI
  32. from nltk.probability import DictionaryProbDist
  33. from nltk import compat
  34. try:
  35. from sklearn.feature_extraction import DictVectorizer
  36. from sklearn.preprocessing import LabelEncoder
  37. except ImportError:
  38. pass
  39. __all__ = ['SklearnClassifier']
  40. @compat.python_2_unicode_compatible
  41. class SklearnClassifier(ClassifierI):
  42. """Wrapper for scikit-learn classifiers."""
  43. def __init__(self, estimator, dtype=float, sparse=True):
  44. """
  45. :param estimator: scikit-learn classifier object.
  46. :param dtype: data type used when building feature array.
  47. scikit-learn estimators work exclusively on numeric data. The
  48. default value should be fine for almost all situations.
  49. :param sparse: Whether to use sparse matrices internally.
  50. The estimator must support these; not all scikit-learn classifiers
  51. do (see their respective documentation and look for "sparse
  52. matrix"). The default value is True, since most NLP problems
  53. involve sparse feature sets. Setting this to False may take a
  54. great amount of memory.
  55. :type sparse: boolean.
  56. """
  57. self._clf = estimator
  58. self._encoder = LabelEncoder()
  59. self._vectorizer = DictVectorizer(dtype=dtype, sparse=sparse)
  60. def __repr__(self):
  61. return "<SklearnClassifier(%r)>" % self._clf
  62. def classify_many(self, featuresets):
  63. """Classify a batch of samples.
  64. :param featuresets: An iterable over featuresets, each a dict mapping
  65. strings to either numbers, booleans or strings.
  66. :return: The predicted class label for each input sample.
  67. :rtype: list
  68. """
  69. X = self._vectorizer.transform(featuresets)
  70. classes = self._encoder.classes_
  71. return [classes[i] for i in self._clf.predict(X)]
  72. def prob_classify_many(self, featuresets):
  73. """Compute per-class probabilities for a batch of samples.
  74. :param featuresets: An iterable over featuresets, each a dict mapping
  75. strings to either numbers, booleans or strings.
  76. :rtype: list of ``ProbDistI``
  77. """
  78. X = self._vectorizer.transform(featuresets)
  79. y_proba_list = self._clf.predict_proba(X)
  80. return [self._make_probdist(y_proba) for y_proba in y_proba_list]
  81. def labels(self):
  82. """The class labels used by this classifier.
  83. :rtype: list
  84. """
  85. return list(self._encoder.classes_)
  86. def train(self, labeled_featuresets):
  87. """
  88. Train (fit) the scikit-learn estimator.
  89. :param labeled_featuresets: A list of ``(featureset, label)``
  90. where each ``featureset`` is a dict mapping strings to either
  91. numbers, booleans or strings.
  92. """
  93. X, y = list(zip(*labeled_featuresets))
  94. X = self._vectorizer.fit_transform(X)
  95. y = self._encoder.fit_transform(y)
  96. self._clf.fit(X, y)
  97. return self
  98. def _make_probdist(self, y_proba):
  99. classes = self._encoder.classes_
  100. return DictionaryProbDist(dict((classes[i], p) for i, p in enumerate(y_proba)))
  101. # skip doctests if scikit-learn is not installed
  102. def setup_module(module):
  103. from nose import SkipTest
  104. try:
  105. import sklearn
  106. except ImportError:
  107. raise SkipTest("scikit-learn is not installed")
  108. if __name__ == "__main__":
  109. from nltk.classify.util import names_demo, names_demo_features
  110. from sklearn.linear_model import LogisticRegression
  111. from sklearn.naive_bayes import BernoulliNB
  112. # Bernoulli Naive Bayes is designed for binary classification. We set the
  113. # binarize option to False since we know we're passing boolean features.
  114. print("scikit-learn Naive Bayes:")
  115. names_demo(
  116. SklearnClassifier(BernoulliNB(binarize=False)).train,
  117. features=names_demo_features,
  118. )
  119. # The C parameter on logistic regression (MaxEnt) controls regularization.
  120. # The higher it's set, the less regularized the classifier is.
  121. print("\n\nscikit-learn logistic regression:")
  122. names_demo(
  123. SklearnClassifier(LogisticRegression(C=1000)).train,
  124. features=names_demo_features,
  125. )