scikitlearn.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 nltk.classify.api import ClassifierI
  30. from nltk.probability import DictionaryProbDist
  31. try:
  32. from sklearn.feature_extraction import DictVectorizer
  33. from sklearn.preprocessing import LabelEncoder
  34. except ImportError:
  35. pass
  36. __all__ = ["SklearnClassifier"]
  37. class SklearnClassifier(ClassifierI):
  38. """Wrapper for scikit-learn classifiers."""
  39. def __init__(self, estimator, dtype=float, sparse=True):
  40. """
  41. :param estimator: scikit-learn classifier object.
  42. :param dtype: data type used when building feature array.
  43. scikit-learn estimators work exclusively on numeric data. The
  44. default value should be fine for almost all situations.
  45. :param sparse: Whether to use sparse matrices internally.
  46. The estimator must support these; not all scikit-learn classifiers
  47. do (see their respective documentation and look for "sparse
  48. matrix"). The default value is True, since most NLP problems
  49. involve sparse feature sets. Setting this to False may take a
  50. great amount of memory.
  51. :type sparse: boolean.
  52. """
  53. self._clf = estimator
  54. self._encoder = LabelEncoder()
  55. self._vectorizer = DictVectorizer(dtype=dtype, sparse=sparse)
  56. def __repr__(self):
  57. return "<SklearnClassifier(%r)>" % self._clf
  58. def classify_many(self, featuresets):
  59. """Classify a batch of samples.
  60. :param featuresets: An iterable over featuresets, each a dict mapping
  61. strings to either numbers, booleans or strings.
  62. :return: The predicted class label for each input sample.
  63. :rtype: list
  64. """
  65. X = self._vectorizer.transform(featuresets)
  66. classes = self._encoder.classes_
  67. return [classes[i] for i in self._clf.predict(X)]
  68. def prob_classify_many(self, featuresets):
  69. """Compute per-class probabilities for a batch of samples.
  70. :param featuresets: An iterable over featuresets, each a dict mapping
  71. strings to either numbers, booleans or strings.
  72. :rtype: list of ``ProbDistI``
  73. """
  74. X = self._vectorizer.transform(featuresets)
  75. y_proba_list = self._clf.predict_proba(X)
  76. return [self._make_probdist(y_proba) for y_proba in y_proba_list]
  77. def labels(self):
  78. """The class labels used by this classifier.
  79. :rtype: list
  80. """
  81. return list(self._encoder.classes_)
  82. def train(self, labeled_featuresets):
  83. """
  84. Train (fit) the scikit-learn estimator.
  85. :param labeled_featuresets: A list of ``(featureset, label)``
  86. where each ``featureset`` is a dict mapping strings to either
  87. numbers, booleans or strings.
  88. """
  89. X, y = list(zip(*labeled_featuresets))
  90. X = self._vectorizer.fit_transform(X)
  91. y = self._encoder.fit_transform(y)
  92. self._clf.fit(X, y)
  93. return self
  94. def _make_probdist(self, y_proba):
  95. classes = self._encoder.classes_
  96. return DictionaryProbDist(dict((classes[i], p) for i, p in enumerate(y_proba)))
  97. # skip doctests if scikit-learn is not installed
  98. def setup_module(module):
  99. from nose import SkipTest
  100. try:
  101. import sklearn
  102. except ImportError:
  103. raise SkipTest("scikit-learn is not installed")
  104. if __name__ == "__main__":
  105. from nltk.classify.util import names_demo, names_demo_features
  106. from sklearn.linear_model import LogisticRegression
  107. from sklearn.naive_bayes import BernoulliNB
  108. # Bernoulli Naive Bayes is designed for binary classification. We set the
  109. # binarize option to False since we know we're passing boolean features.
  110. print("scikit-learn Naive Bayes:")
  111. names_demo(
  112. SklearnClassifier(BernoulliNB(binarize=False)).train,
  113. features=names_demo_features,
  114. )
  115. # The C parameter on logistic regression (MaxEnt) controls regularization.
  116. # The higher it's set, the less regularized the classifier is.
  117. print("\n\nscikit-learn logistic regression:")
  118. names_demo(
  119. SklearnClassifier(LogisticRegression(C=1000)).train,
  120. features=names_demo_features,
  121. )