Package nltk :: Package classify :: Module maxent :: Class MaxentClassifier
[hide private]
[frames] | no frames]

Class MaxentClassifier

source code

     object --+    
              |    
api.ClassifierI --+
                  |
                 MaxentClassifier

A maximum entropy classifier (also known as a conditional exponential classifier). This classifier is parameterized by a set of weights, which are used to combine the joint-features that are generated from a featureset by an encoding. In particular, the encoding maps each (featureset, label) pair to a vector. The probability of each label is then computed using the following equation:

                           dotprod(weights, encode(fs,label))
 prob(fs|label) = ---------------------------------------------------
                  sum(dotprod(weights, encode(fs,l)) for l in labels)

Where dotprod is the dot product:

 dotprod(a,b) = sum(x*y for (x,y) in zip(a,b))
Instance Methods [hide private]
 
__init__(self, encoding, weights, logarithmic=True)
Construct a new maxent classifier model.
source code
list of (immutable)
labels(self)
Returns: the list of category labels used by this classifier.
source code
 
set_weights(self, new_weights)
Set the feature weight vector for this classifier.
source code
list of float
weights(self)
Returns: The feature weight vector for this classifier.
source code
label
classify(self, featureset)
Returns: the most appropriate label for the given featureset.
source code
ProbDistI
prob_classify(self, featureset)
Returns: a probability distribution over labels for the given featureset.
source code
 
explain(self, featureset, columns=4)
Print a table showing the effect of each of the features in the given feature set, and how they combine to determine the probabilities of each label for that featureset.
source code
 
show_most_informative_features(self, n=10, show='all') source code
 
__repr__(self)
repr(x)
source code

Inherited from api.ClassifierI: batch_classify, batch_prob_classify

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __str__

    Deprecated

Inherited from api.ClassifierI: batch_probdist, probdist

Class Methods [hide private]
MaxentClassifier
train(cls, train_toks, algorithm=None, trace=3, encoding=None, labels=None, sparse=True, gaussian_prior_sigma=0, **cutoffs)
Train a new maxent classifier based on the given corpus of training samples.
source code
Class Variables [hide private]
  ALGORITHMS = ['GIS', 'IIS', 'CG', 'BFGS', 'Powell', 'LBFGSB', ...
A list of the algorithm names that are accepted for the train() method's algorithm parameter.
  _SCIPY_ALGS = {'bfgs': 'BFGS', 'cg': 'CG', 'lbfgsb': 'LBFGSB',...
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, encoding, weights, logarithmic=True)
(Constructor)

source code 

Construct a new maxent classifier model. Typically, new classifier models are created using the train() method.

Parameters:
  • encoding (MaxentFeatureEncodingI) - An encoding that is used to convert the featuresets that are given to the classify method into joint-feature vectors, which are used by the maxent classifier model.
  • weights (list of float) - The feature weight vector for this classifier.
  • logarithmic (bool) - If false, then use non-logarithmic weights.
Overrides: object.__init__

labels(self)

source code 
Returns: list of (immutable)
the list of category labels used by this classifier.
Overrides: api.ClassifierI.labels
(inherited documentation)

set_weights(self, new_weights)

source code 

Set the feature weight vector for this classifier.

Parameters:
  • new_weights (list of float) - The new feature weight vector.

weights(self)

source code 
Returns: list of float
The feature weight vector for this classifier.

classify(self, featureset)

source code 
Returns: label
the most appropriate label for the given featureset.
Overrides: api.ClassifierI.classify
(inherited documentation)

prob_classify(self, featureset)

source code 
Returns: ProbDistI
a probability distribution over labels for the given featureset.
Overrides: api.ClassifierI.prob_classify
(inherited documentation)

show_most_informative_features(self, n=10, show='all')

source code 
Parameters:
  • show - all, neg, or pos (for negative-only or positive-only)

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)

train(cls, train_toks, algorithm=None, trace=3, encoding=None, labels=None, sparse=True, gaussian_prior_sigma=0, **cutoffs)
Class Method

source code 

Train a new maxent classifier based on the given corpus of training samples. This classifier will have its weights chosen to maximize entropy while remaining empirically consistent with the training corpus.

Parameters:
  • train_toks (list) - Training data, represented as a list of pairs, the first member of which is a featureset, and the second of which is a classification label.
  • algorithm (str) - A case-insensitive string, specifying which algorithm should be used to train the classifier. The following algorithms are currently available.
    • Iterative Scaling Methods
      • 'GIS': Generalized Iterative Scaling
      • 'IIS': Improved Iterative Scaling
    • Optimization Methods (require scipy)
      • 'CG': Conjugate gradient
      • 'BFGS': Broyden-Fletcher-Goldfarb-Shanno algorithm
      • 'Powell': Powell agorithm
      • 'LBFGSB': A limited-memory variant of the BFGS algorithm
      • 'Nelder-Mead': The Nelder-Mead algorithm
    • External Libraries
      • 'megam': LM-BFGS algorithm, with training performed by an megam. (requires that megam be installed.)

    The default algorithm is 'CG' if 'scipy' is installed; and 'iis' otherwise.

  • trace (int) - The level of diagnostic tracing output to produce. Higher values produce more verbose output.
  • encoding (MaxentFeatureEncodingI) - A feature encoding, used to convert featuresets into feature vectors. If none is specified, then a BinaryMaxentFeatureEncoding will be built based on the features that are attested in the training corpus.
  • labels (list of str) - The set of possible labels. If none is given, then the set of all labels attested in the training data will be used instead.
  • sparse - If true, then use sparse matrices instead of dense matrices. Currently, this is only supported by the scipy (optimization method) algorithms. For other algorithms, its value is ignored.
  • gaussian_prior_sigma - The sigma value for a gaussian prior on model weights. Currently, this is supported by the scipy (optimization method) algorithms and megam. For other algorithms, its value is ignored.
  • cutoffs - Arguments specifying various conditions under which the training should be halted. (Some of the cutoff conditions are not supported by some algorithms.)
    • max_iter=v: Terminate after v iterations.
    • min_ll=v: Terminate after the negative average log-likelihood drops under v.
    • min_lldelta=v: Terminate if a single iteration improves log likelihood by less than v.
    • tolerance=v: Terminate a scipy optimization method when improvement drops below a tolerance level v. The exact meaning of this tolerance depends on the scipy algorithm used. See scipy documentation for more info. Default values: 1e-3 for CG, 1e-5 for LBFGSB, and 1e-4 for other algorithms. (scipy only)
Returns: MaxentClassifier
The new maxent classifier

Class Variable Details [hide private]

ALGORITHMS

A list of the algorithm names that are accepted for the train() method's algorithm parameter.

Value:
['GIS',
 'IIS',
 'CG',
 'BFGS',
 'Powell',
 'LBFGSB',
 'Nelder-Mead',
 'MEGAM']

_SCIPY_ALGS

Value:
{'bfgs': 'BFGS',
 'cg': 'CG',
 'lbfgsb': 'LBFGSB',
 'nelder-mead': 'Nelder-Mead',
 'powell': 'Powell'}