Skip to main content

Prediction probabilities with a linear programming constraint during label assignment.

Project description

PredLP

pypi_versions py_versions pytest

Prediction probabilities with a linear programming constraint during label assignment.

Usage

Example label contraint imposed using the package predlp via this code:

from predlp.solver import pred_prob_lp

# ML outputs feed into predlp constraints
class_names = ['label_a', 'label_b', 'label_c']
label_counts = {'label_a': 2, 'label_b': 1, 'label_c': 1}
pred_probs = [
    [0.6*, 0.3,  0.1],
    [0.2,  0.5*, 0.3],
    [0.8*, 0.1,  0.1],
    [0.5,  0.1,  0.4*]
]

# Run through solver and optimize for probability scores
pred_after_lp = pred_prob_lp(class_names=class_names, label_counts= label_counts, pred_probs=pred_probs)

# Output of predlp, highlighted in array (*)
pred_after_lp == ['label_a', 'label_b', 'label_a', 'label_c']

Parameters

  • class_names (list): The list should be in the order that matches the pred_prob array. So if class 0 is ‘linear’ and class 1 is ‘programming’, then class_names = ['linear', 'programming'].
  • label_counts (dict): A dictionary specifying the expected count for each label. The keys represent the class (N), and the values (K) constraints.
  • pred_probs (np.array): A 2D array of shape (N, K) containing the model predicted probabilities for each label. Each row corresponds to a class, corresponding to the class_name parameter, and the columns represent probabilities for classes {0, 1, ..., K-1}.

Returns

  • pred_lp (list): A list of predicted labels, with a total length of N. The list satisfies the constraints defined by label_counts and is optimized to maximize the total probability score across all predictions.

Citation and related publications

Here are relevant papers to cite if you use this package:

Life in the FastText Lane: Harnessing Linear Programming Constrained Machine Learning for Classifications Revision (click to show bibtex)
@inproceedings{
    title={Life in the FastText Lane: Harnessing Linear Programming Constrained Machine Learning for Classifications Revision},
    author={Justin Evans, Laura Wile},
    conference={Statistics Canada's International Methodology Symposium: The Future of Official Statistics},
    year={2024}
}

Contribute

Contributions of any kind welcome. See the development guide to get started.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

predlp-0.1.1.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

predlp-0.1.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file predlp-0.1.1.tar.gz.

File metadata

  • Download URL: predlp-0.1.1.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for predlp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 84a713c6dd2eb3249257411dddb09e440c3f8bc13eb9e26f58322e8e87cb822c
MD5 c04eb05e0c9f1030dcedff7c68dc445d
BLAKE2b-256 1c7716f92a1281c28237526e5e166d41ecf115b994c6d6f70258289eace5d463

See more details on using hashes here.

Provenance

The following attestation bundles were made for predlp-0.1.1.tar.gz:

Publisher: release.yaml on justinj-evans/predlp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file predlp-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: predlp-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for predlp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2a8d9d4cdd67bd831622476e0077ea76de0a607dd834c63136ae0d3a53dec158
MD5 4441045ba0f17c3c8098842ffe899d1e
BLAKE2b-256 364b0d969ce3fe552ebdcb3c5200358dd0882f6b41e303ab765b99a6f3666b0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for predlp-0.1.1-py3-none-any.whl:

Publisher: release.yaml on justinj-evans/predlp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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