# Implementation of CRF in python

Is there a popular implementation of Conditional Random Fields in Python?

I can't seem to find any that is widely used and popular!

CRF++ is a popular choice in general, and has Python bindings. CRFSuite also has bindings documented here, but doesn't seem to have seen as much widespread use as CRF++. As of this writing, higher level machine learning frameworks such as scikit-learn lack CRF support (see this pull request).

CRF++ has more incoming links because it is an older library.
CRFSuite is superior in my opinion.

• CRFSuite's author's claim that it is 20x faster than CRF++ at training a model.
• Less rigid requirements for the input data.

If you are looking for Python bindings CRFSuite is also better because you can train a model in Python, while in CRF++ you can only test existing models in Python. (That was the deal breaker for me.) CRFSuite also comes with a bunch of example code in Python, such as NER, Chunking, and POS tagging.

• Can you elaborate on how to use the NER and Chunking provided by CRFSuite? It looks like it expects training data of a different format. Where can I find this? – Legend Jul 17 '13 at 21:44

Here are some other wrappers/implementations:

• https://github.com/adsva/python-wapiti - Python wrapper for http://wapiti.limsi.fr/. Wapiti is fast; crfsuite benchmarks are not fair to wapiti because wapiti can parallelize L-BFGS training to multiple CPU cores, and this feature was not used in benchmarks. The problem with Wapiti is that it is not written as a library. The wrapper tries hard to overcome that, but you can still get an uncatchable exit(), and I've seen memory leaks during the training. Also, wapiti is limited in a type of features it can represent, but CRFsuite is also limited (in a different way). Wapiti is bundled in a wrapper, no need to install it separately.
• https://github.com/jakevdp/pyCRFsuite - a wrapper for crfsuite. The wrapper is quite advanced and allows using scipy sparse matrices as an input, but it seems there are some unresolved issues, it is possible to get a segfault in some cases.
• https://github.com/tpeng/python-crfsuite - another crfsuite wrapper. This one is rather simple; it bundles crfsuite for easier installation and can be installed just with 'pip install python-crfsuite'.
• https://github.com/larsmans/seqlearn provides Structured Perceptron which can be a replacement for CRF in many cases. Structured Perceptron implementation is very fast in seqlearn. There is a PR (not merged at the time of writing) which adds CRF support to seqlearn; it looks solid.
• https://github.com/timvieira/crf - it is quite basic and doesn't have some essential features, but it requires only numpy.

I'd recommend to use seqlearn if you can, python-crfsuite if you need CRFsuite training algorithms and training speed, pyCRFsuite if you need more advanced CRFsuite integration and ready to face some inconveniences, python-wapiti if you need Wapiti training algorithms or features not available in CRFsuite (like conditioning individual observations on transitions) and timvieira's crf if there is no way to get a C/C++ compiler working, but a prebuilt numpy is available.

I think what you are looking for is PyStruct.

PyStruct aims at being an easy-to-use structured learning and prediction library. Currently it implements only max-margin methods and a perceptron, but other algorithms might follow.

The goal of PyStruct is to provide a well-documented tool for researchers as well as non-experts to make use of structured prediction algorithms. The design tries to stay as close as possible to the interface and conventions of scikit-learn.

PyStruct comes with good documentation, and it is actively developed on github.

Below is a table comparing PyStruct with CRFsuite and other packages, extracted from PyStruct - Structured prediction in Python: