{"library":"sklearn-crfsuite","title":"sklearn-crfsuite","description":"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.","language":"python","status":"active","last_verified":"Sat May 16","install":{"commands":["pip install sklearn-crfsuite"],"cli":null},"imports":["from sklearn_crfsuite import CRF","from sklearn_crfsuite import metrics","from sklearn_crfsuite import scorers"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import sklearn_crfsuite\nfrom sklearn_crfsuite import metrics\n\n# Dummy data for a simple sequence labeling task (e.g., POS tagging)\n# Each sentence is a list of (word, pos_tag)\n# Features are extracted for each word, labels are the expected tags\n\ndef word2features(sent, i):\n    word = sent[i][0]\n    postag = sent[i][1]\n\n    features = {\n        'bias': 1.0,\n        'word.lower()': word.lower(),\n        'word.isupper()': word.isupper(),\n        'word.istitle()': word.istitle(),\n        'word.isdigit()': word.isdigit(),\n        'postag': postag,\n        'postag[:2]': postag[:2],\n    }\n    if i > 0:\n        word1 = sent[i-1][0]\n        postag1 = sent[i-1][1]\n        features[' -1:word.lower()'] = word1.lower()\n        features[' -1:word.istitle()'] = word1.istitle()\n        features[' -1:word.isupper()'] = word1.isupper()\n        features[' -1:postag'] = postag1\n        features[' -1:postag[:2]'] = postag1[:2]\n    else:\n        features['BOS'] = True # Beginning of Sentence\n\n    if i < len(sent)-1:\n        word1 = sent[i+1][0]\n        postag1 = sent[i+1][1]\n        features['+1:word.lower()'] = word1.lower()\n        features['+1:word.istitle()'] = word1.istitle()\n        features['+1:word.isupper()'] = word1.isupper()\n        features['+1:postag'] = postag1\n        features['+1:postag[:2]'] = postag1[:2]\n    else:\n        features['EOS'] = True # End of Sentence\n\n    return features\n\ndef sent2features(sent):\n    return [word2features(sent, i) for i in range(len(sent))]\n\ndef sent2labels(sent):\n    return [label for word, label in sent]\n\ntrain_sents = [\n    [('The', 'DT'), ('quick', 'JJ'), ('brown', 'JJ'), ('fox', 'NN'), ('jumps', 'VBZ'), ('over', 'IN'), ('the', 'DT'), ('lazy', 'JJ'), ('dog', 'NN')],\n    [('I', 'PRP'), ('love', 'VBP'), ('Python', 'NNP')],\n    [('Natural', 'JJ'), ('Language', 'NNP'), ('Processing', 'NNP'), ('is', 'VBZ'), ('fun', 'JJ')],\n]\n\nX_train = [sent2features(s) for s in train_sents]\ny_train = [sent2labels(s) for s in train_sents]\n\n# Initialize and train the CRF model\ncrf = sklearn_crfsuite.CRF(\n    algorithm='lbfgs',\n    c1=0.1,  # L1 regularization\n    c2=0.1,  # L2 regularization\n    max_iterations=100,\n    all_possible_transitions=True\n)\n\ncrf.fit(X_train, y_train)\n\n# Make predictions on new data\ntest_sents = [\n    [('A', 'DT'), ('fast', 'JJ'), ('red', 'JJ'), ('car', 'NN'), ('drives', 'VBZ'), ('by', 'IN')],\n]\nX_test = [sent2features(s) for s in test_sents]\ny_pred = crf.predict(X_test)\n\nprint(\"Predicted labels for test sentence:\")\nfor sent_idx, labels in enumerate(y_pred):\n    print(f\"Sentence {sent_idx+1}: {labels}\")\n\n# Example of using metrics (requires scikit-learn)\ny_true = [sent2labels(s) for s in test_sents] # In a real scenario, this would be actual ground truth\nif y_true:\n    print(\"\\nClassification Report:\")\n    print(metrics.flat_classification_report(y_true, y_pred))\n","lang":"python","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-16","installed_version":"0.5.0","pypi_latest":"0.5.0","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":50,"avg_install_s":9.8,"avg_import_s":3.64,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"sklearn-crfsuite","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"sklearn-crfsuite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":9.7,"import_time_s":2.38,"mem_mb":43.8,"disk_size":"275M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"sklearn-crfsuite","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"sklearn-crfsuite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":9.3,"import_time_s":4.14,"mem_mb":54.3,"disk_size":"293M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"sklearn-crfsuite","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"sklearn-crfsuite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":9.6,"import_time_s":4.8,"mem_mb":53.3,"disk_size":"276M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"sklearn-crfsuite","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"sklearn-crfsuite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":9.6,"import_time_s":4.24,"mem_mb":54.1,"disk_size":"275M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"sklearn-crfsuite","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"sklearn-crfsuite","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":10.8,"import_time_s":2.66,"mem_mb":40.1,"disk_size":"290M"}]}}