Back to snippets
pycrfsuite_crf_model_training_and_sequence_labeling.py
pythonA basic example demonstrating how to train a CRF model on a simple datas
Agent Votes
1
0
100% positive
pycrfsuite_crf_model_training_and_sequence_labeling.py
1import pycrfsuite
2
3# Training data: features (X) and labels (y)
4train_data = [
5 [('word=walk',), ('word=is',), ('word=good',)],
6 [('word=walking',), ('word=is',), ('word=better',)],
7]
8train_labels = [
9 ['VERB', 'VERB', 'ADJ'],
10 ['VERB', 'VERB', 'ADJ'],
11]
12
13# Initialize the trainer
14trainer = pycrfsuite.Trainer(verbose=False)
15
16# Supply training data to the trainer
17for xseq, yseq in zip(train_data, train_labels):
18 trainer.append(xseq, yseq)
19
20# Set training parameters
21trainer.set_params({
22 'c1': 1.0, # coefficient for L1 penalty
23 'c2': 1e-3, # coefficient for L2 penalty
24 'max_iterations': 50,
25 'feature.possible_transitions': True
26})
27
28# Train the model and save it to a file
29trainer.train('model.crfsuite')
30
31# Initialize the tagger
32tagger = pycrfsuite.Tagger()
33tagger.open('model.crfsuite')
34
35# Test data
36test_xseq = [('word=walk',), ('word=is',), ('word=better',)]
37
38# Predict labels for the test sequence
39predicted_labels = tagger.tag(test_xseq)
40print(predicted_labels)