Registry / ai-ml / sklearn-crfsuite

sklearn-crfsuite

JSON →
library0.5.0pypypi✓ verified 86d ago

sklearn-crfsuite is a thin wrapper around the `python-crfsuite` library, providing an interface similar to scikit-learn. It enables the use of scikit-learn's model selection utilities (like cross-validation and hyperparameter optimization) with Conditional Random Field (CRF) models, and allows saving/loading models using joblib. The library is actively maintained, with its latest major release (0.5.0) in June 2024.

pip install sklearn-crfsuite
INSTALL
IMPORT
SIG · SKLEARN-CRFSUITE
S
sklearn-crfsuite
ai-mlpythonv0.5.0
Install
9.8s avg
Import
3657ms
Disk
286MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
build_error
glibc
py 3.103.920 runs
installs and imports cleanly · install 9.8s · import 3.657s · 275MB
286MB installed
● package 286MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CRF
from sklearn_crfsuite import CRF
metrics
from sklearn_crfsuite import metrics
scorers
from sklearn_crfsuite import scorers

This quickstart demonstrates how to prepare data, extract basic features, train a Conditional Random Field (CRF) model using `sklearn_crfsuite.CRF`, and make predictions. It also shows how to leverage `sklearn_crfsuite.metrics` for evaluating model performance. The example uses a small, self-contained dummy dataset to illustrate a part-of-speech (POS) tagging task.

import sklearn_crfsuite from sklearn_crfsuite import metrics # Dummy data for a simple sequence labeling task (e.g., POS tagging) # Each sentence is a list of (word, pos_tag) # Features are extracted for each word, labels are the expected tags def word2features(sent, i): word = sent[i][0] postag = sent[i][1] features = { 'bias': 1.0, 'word.lower()': word.lower(), 'word.isupper()': word.isupper(), 'word.istitle()': word.istitle(), 'word.isdigit()': word.isdigit(), 'postag': postag, 'postag[:2]': postag[:2], } if i > 0: word1 = sent[i-1][0] postag1 = sent[i-1][1] features[' -1:word.lower()'] = word1.lower() features[' -1:word.istitle()'] = word1.istitle() features[' -1:word.isupper()'] = word1.isupper() features[' -1:postag'] = postag1 features[' -1:postag[:2]'] = postag1[:2] else: features['BOS'] = True # Beginning of Sentence if i < len(sent)-1: word1 = sent[i+1][0] postag1 = sent[i+1][1] features['+1:word.lower()'] = word1.lower() features['+1:word.istitle()'] = word1.istitle() features['+1:word.isupper()'] = word1.isupper() features['+1:postag'] = postag1 features['+1:postag[:2]'] = postag1[:2] else: features['EOS'] = True # End of Sentence return features def sent2features(sent): return [word2features(sent, i) for i in range(len(sent))] def sent2labels(sent): return [label for word, label in sent] train_sents = [ [('The', 'DT'), ('quick', 'JJ'), ('brown', 'JJ'), ('fox', 'NN'), ('jumps', 'VBZ'), ('over', 'IN'), ('the', 'DT'), ('lazy', 'JJ'), ('dog', 'NN')], [('I', 'PRP'), ('love', 'VBP'), ('Python', 'NNP')], [('Natural', 'JJ'), ('Language', 'NNP'), ('Processing', 'NNP'), ('is', 'VBZ'), ('fun', 'JJ')], ] X_train = [sent2features(s) for s in train_sents] y_train = [sent2labels(s) for s in train_sents] # Initialize and train the CRF model crf = sklearn_crfsuite.CRF( algorithm='lbfgs', c1=0.1, # L1 regularization c2=0.1, # L2 regularization max_iterations=100, all_possible_transitions=True ) crf.fit(X_train, y_train) # Make predictions on new data test_sents = [ [('A', 'DT'), ('fast', 'JJ'), ('red', 'JJ'), ('car', 'NN'), ('drives', 'VBZ'), ('by', 'IN')], ] X_test = [sent2features(s) for s in test_sents] y_pred = crf.predict(X_test) print("Predicted labels for test sentence:") for sent_idx, labels in enumerate(y_pred): print(f"Sentence {sent_idx+1}: {labels}") # Example of using metrics (requires scikit-learn) y_true = [sent2labels(s) for s in test_sents] # In a real scenario, this would be actual ground truth if y_true: print("\nClassification Report:") print(metrics.flat_classification_report(y_true, y_pred))
Debug
Known issues
breakingIn version 0.5.0, the `CRF.predict()` and `CRF.predict_marginals()` methods now return a NumPy array instead of a list of lists, aligning with expectations from newer scikit-learn versions.
fix
Update code to expect and handle NumPy array outputs from `predict()` and `predict_marginals()`.
affects: >=0.5.0
breakingVersion 0.4.0 dropped official support for Python 3.7 and lower, and explicitly added support for Python 3.8 and higher. It also increased minimum versions for dependencies like `python-crfsuite` (0.9.7) and `scikit-learn` (0.24.0).
fix
Ensure your Python environment is 3.8+ and update `python-crfsuite` and `scikit-learn` to their specified minimum versions or newer. Consider pinning dependencies in your `requirements.txt`.
affects: >=0.4.0
breakingIn version 0.2, the `crf.tagger` attribute was renamed to `crf.tagger_`. Additionally, accessing `crf.tagger_` before training no longer raises an exception but returns `None`.
fix
Update any code referencing `crf.tagger` to `crf.tagger_`. If relying on exceptions for untraining state, adapt logic to check for `None`.
affects: >=0.2
gotcha`python-crfsuite` and `sklearn-crfsuite` do not natively support array-like features (e.g., word embeddings) directly. Attempting to pass a NumPy array as a single feature will result in errors.
fix
Each component of an array feature (like a word embedding vector) must be flattened and passed as a separate dictionary feature (e.g., `{'v0': value_0, 'v1': value_1, ...}`). This can significantly increase the number of features and training time.
affects: All
gotchaAs with general scikit-learn practices, inconsistent preprocessing between training and test data (e.g., feature extraction functions) can lead to unexpected model performance.
fix
Always apply the exact same feature extraction logic and transformations to both your training and inference data. Consider encapsulating feature extraction in a consistent pipeline or utility function.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'sklearn_crfsuite'
The `sklearn-crfsuite` package is not installed in the current Python environment or the environment is not active.
fix
pip install sklearn-crfsuite
AttributeError: 'CRF' object has no attribute 'predict_proba'
The `sklearn_crfsuite.CRF` model provides `predict` for sequence prediction and `predict_marginals` for token-level probabilities, but not a `predict_proba` method common in other scikit-learn classifiers.
fix
Use `model.predict(X_test)` for the predicted label sequence or `model.predict_marginals(X_test)` for marginal probabilities.
TypeError: 'str' object is not subscriptable
The input data `X` (features) or `y` (labels) is not in the expected nested list format for CRF training/prediction, typically expecting lists of lists of dictionaries for features and lists of lists of strings for labels.
fix
Ensure features are extracted into `[[{'feature_name': value, ...}, ...], ...]` and labels are formatted as `[['label1', 'label2'], ...]`, as `TypeError: 'str' object is not subscriptable` often indicates iterating over a string where a list of dictionaries/strings was expected.
ValueError: Found input variables with inconsistent numbers of samples:
The number of sequences (samples) in your feature list (`X`) does not match the number of sequences in your label list (`y`) when passed to methods like `fit` or scikit-learn's cross-validation utilities.
fix
Verify that `len(X)` equals `len(y)` and that each inner list (representing a sequence) in `X` correctly corresponds to an inner list in `y` for each sample.
Upgrade
Version history
0.5.0latest on PyPI · released Jun 18, 2024
Audit
Dependencies
python-crfsuiterequiredCore CRF engine. Version 0.9.7 or higher required for sklearn-crfsuite >= 0.4.0.
scikit-learnoptionalRequired for using scikit-learn compatible metrics and scorers, or for integration with scikit-learn pipelines. Optional for basic CRF fitting and prediction. Version 0.24.0 or higher required for sklearn-crfsuite >= 0.4.0.
tabulaterequiredRequired by sklearn-crfsuite >= 0.4.0, often used for displaying metrics.
nltkrequiredCommonly used in tutorials and examples for tokenization, POS tagging, and accessing corpora like CoNLL-2002/2003 for sequence labeling tasks.
Agent activity
11 hits · last 30 days
node
8
OpenAI (training)
1
Resources
sklearn-crfsuite — pip install sklearn-crfsuite · libregistry