nltk.tag.HiddenMarkovModelTrainer

class nltk.tag.HiddenMarkovModelTrainer[source]

Bases: object

Algorithms for learning HMM parameters from training data. These include both supervised learning (MLE) and unsupervised learning (Baum-Welch).

Creates an HMM trainer to induce an HMM with the given states and output symbol alphabet. A supervised and unsupervised training method may be used. If either of the states or symbols are not given, these may be derived from supervised training.

Parameters
  • states (sequence of any) – the set of state labels

  • symbols (sequence of any) – the set of observation symbols

__init__(states=None, symbols=None)[source]
train(labeled_sequences=None, unlabeled_sequences=None, **kwargs)[source]

Trains the HMM using both (or either of) supervised and unsupervised techniques.

Returns

the trained model

Return type

HiddenMarkovModelTagger

Parameters
  • labelled_sequences (list) – the supervised training data, a set of labelled sequences of observations ex: [ (word_1, tag_1),…,(word_n,tag_n) ]

  • unlabeled_sequences (list) – the unsupervised training data, a set of sequences of observations ex: [ word_1, …, word_n ]

  • kwargs – additional arguments to pass to the training methods

train_unsupervised(unlabeled_sequences, update_outputs=True, **kwargs)[source]

Trains the HMM using the Baum-Welch algorithm to maximise the probability of the data sequence. This is a variant of the EM algorithm, and is unsupervised in that it doesn’t need the state sequences for the symbols. The code is based on ‘A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition’, Lawrence Rabiner, IEEE, 1989.

Returns

the trained model

Return type

HiddenMarkovModelTagger

Parameters

unlabeled_sequences (list) – the training data, a set of sequences of observations

kwargs may include following parameters:

Parameters
  • model – a HiddenMarkovModelTagger instance used to begin the Baum-Welch algorithm

  • max_iterations – the maximum number of EM iterations

  • convergence_logprob – the maximum change in log probability to allow convergence

train_supervised(labelled_sequences, estimator=None)[source]

Supervised training maximising the joint probability of the symbol and state sequences. This is done via collecting frequencies of transitions between states, symbol observations while within each state and which states start a sentence. These frequency distributions are then normalised into probability estimates, which can be smoothed if desired.

Returns

the trained model

Return type

HiddenMarkovModelTagger

Parameters
  • labelled_sequences (list) – the training data, a set of labelled sequences of observations

  • estimator – a function taking a FreqDist and a number of bins and returning a CProbDistI; otherwise a MLE estimate is used