Skip to main content

A hidden (state) conditional random field (HCRF) implementation written in Python and Cython.

Project description

pyhcrf

A hidden (state) conditional random field (HCRF) written in Python and Cython.

TravisCI AppVeyor Coverage License PyPI Wheel Python Versions Python Implementations Source GitHub issues Changelog

This package is a fork of the original pyhcrf, written by Dirko Coetsee, featuring Python 3 and Windows support with a cleaner code base maintained by Martin Larralde.

Overview

pyhcrf implements a HCRF model with a sklearn-inspired interface. The model classifies sequences according to a latent state sequence. This package provides methods to learn parameters from example sequences and to classify new sequences. See the paper by Wang et al. and the report by Dirko Coetsee.

States

Each state is numbered 0, 1, ..., num_states - 1. The state machine starts in state 0 and ends in num_states - 1. Currently the state transitions are constrained so that, on each element in the input sequence, the state machine either stays in the current state or advances to a state represented by the next number. This default can be changed by setting the transitions and corresponding transition_parameters properties.

Dependencies

pyhcrf depends on numpy, scipy (for the LM-BFGS optimiser and scipy.sparse), and also needs cython for building from source.

Example

X = [array([[ 1. , -0.82683403,  2.48881337],
            [ 1. , -1.07491808,  1.55848197],
            [ 1. ,  6.7814359 ,  4.01074595]]),
     array([[ 1. , -3.01165932, -2.15972362],
            [ 1. , -3.41449473, -2.2668825 ]]),
     array([[ 1. , -2.64921323, -1.20159641],
            [ 1. ,  0.31139394,  1.58841159]]),
     array([[ 1. ,  5.85226017,  2.43317499],
            [ 1. , -1.57598266, -2.07585778]]),
     array([[ 1. , -0.32999744, -2.70695361],
            [ 1. ,  0.44311988,  0.36400733]]),
     array([[ 1. , -0.05301562,  3.95424435],
            [ 1. ,  3.04540498, -3.25040276]]),
     array([[ 1. , -4.29117715,  0.9167861 ],
            [ 1. , -3.22775884,  1.83277224]]),
     array([[ 1. , -2.80856491,  1.95630489],
            [ 1. ,  1.62290542, -0.7457237 ]]),
     array([[ 1. , -2.32682366,  2.60844469],
            [ 1. ,  2.12320609,  1.04483217]]),
     array([[ 1. , -4.17616178,  4.09969658],
            [ 1. ,  0.67287935, -5.67652159]])]

y = [0, 1, 0, 1, 1, 0, 1, 0, 0, 0]

Training examples

>>> from pyhcrf import HCRF
>>> from sklearn.metrics import confusion_matrix

>>> model = HCRF(num_states=3, random_seed=3, optimizer_kwargs={'maxfun':200})
>>> model.fit(X, y)
>>> pred = model.predict(X)
>>> confusion_matrix(y, pred)
array([[12,  0],
      [ 0,  8]])

Installation

Download/clone and run

python setup.py build_ext --inplace
python setup.py install

License

The original code, and all contributions made subsequently in this fork, are licensed with BSD-2-Clause.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyhcrf-0.1.0.tar.gz (15.2 kB view hashes)

Uploaded Source

Built Distributions

pyhcrf-0.1.0-pp36-pypy36_pp73-manylinux2010_x86_64.whl (113.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ x86-64

pyhcrf-0.1.0-cp38-cp38-win_amd64.whl (265.2 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

pyhcrf-0.1.0-cp38-cp38-manylinux2010_x86_64.whl (573.4 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

pyhcrf-0.1.0-cp38-cp38-manylinux1_x86_64.whl (573.4 kB view hashes)

Uploaded CPython 3.8

pyhcrf-0.1.0-cp38-cp38-macosx_10_13_x86_64.whl (118.4 kB view hashes)

Uploaded CPython 3.8 macOS 10.13+ x86-64

pyhcrf-0.1.0-cp37-cp37m-win_amd64.whl (263.0 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

pyhcrf-0.1.0-cp37-cp37m-manylinux2010_x86_64.whl (509.4 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

pyhcrf-0.1.0-cp37-cp37m-manylinux1_x86_64.whl (509.4 kB view hashes)

Uploaded CPython 3.7m

pyhcrf-0.1.0-cp37-cp37m-macosx_10_13_x86_64.whl (116.8 kB view hashes)

Uploaded CPython 3.7m macOS 10.13+ x86-64

pyhcrf-0.1.0-cp36-cp36m-win_amd64.whl (263.0 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

pyhcrf-0.1.0-cp36-cp36m-manylinux2010_x86_64.whl (509.7 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

pyhcrf-0.1.0-cp36-cp36m-manylinux1_x86_64.whl (509.7 kB view hashes)

Uploaded CPython 3.6m

pyhcrf-0.1.0-cp36-cp36m-macosx_10_13_x86_64.whl (118.5 kB view hashes)

Uploaded CPython 3.6m macOS 10.13+ x86-64

pyhcrf-0.1.0-cp35-cp35m-win_amd64.whl (261.8 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

pyhcrf-0.1.0-cp35-cp35m-manylinux2010_x86_64.whl (504.9 kB view hashes)

Uploaded CPython 3.5m manylinux: glibc 2.12+ x86-64

pyhcrf-0.1.0-cp35-cp35m-manylinux1_x86_64.whl (504.9 kB view hashes)

Uploaded CPython 3.5m

pyhcrf-0.1.0-cp35-cp35m-macosx_10_13_x86_64.whl (116.3 kB view hashes)

Uploaded CPython 3.5m macOS 10.13+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page