A Python framework for Quantification
Go to file
Alejandro Moreo Fernandez 24719ed0af Add HistNetQ, a differentiable-histogram neural quantifier
Ports the "hard" histogram variant of HistNetQ (from
https://github.com/pglez84/histnetq) into quapy/method/_histnet.py, dropping
that repo's quantificationlib-backed bag generators in favor of QuaPy's own
sampling protocols (UPP by default). Implemented as a BaseQuantifier,
alongside QuaNet, since it trains end-to-end on samples of known prevalence
rather than following the classify-then-aggregate pattern.

- HistNetQ.fit(X, y): resamples training/validation bags from a
  LabelledCollection via a configurable protocol (UPP by default; fresh
  random bags each training epoch, a fixed reproducible sequence for
  validation).
- HistNetQ.fit_from_samples(protocol, val_protocol=None, mix_bags=False):
  trains directly from a protocol that already yields bags (e.g. LeQua's
  SamplesFromDir), with an optional mixer to synthesize extra
  intermediate-prevalence bags from the given ones.
- Aliased in meta.py (torch-optional, mirroring the existing QuaNet guard)
  and registered in META_METHODS.
- Adds test_histnetq covering both entry points on binary and multiclass
  synthetic data.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-19 18:44:57 +02:00
.github/workflows using real PYPI 2025-10-06 15:37:08 +02:00
docs update docs 2026-07-17 16:45:12 +02:00
examples adding EDy and improved manuals 2026-07-06 17:30:42 +02:00
experimental_non_aggregative adding kdex 2025-10-23 14:12:39 +02:00
logo devel merge for release 2025-10-06 14:35:35 +02:00
quapy Add HistNetQ, a differentiable-histogram neural quantifier 2026-08-19 18:44:57 +02:00
.gitignore devel merge for release 2025-10-06 14:35:35 +02:00
CHANGE_LOG.txt added EDx and improve doc 2026-07-07 15:46:45 +02:00
LICENSE license updated 2020-12-15 13:36:24 +01:00
README.md adding EDy and improved manuals 2026-07-06 17:30:42 +02:00
TODO.txt adding EDy and improved manuals 2026-07-06 17:30:42 +02:00
prepare_svmperf.sh all examples but 15 (qunfold) properly working 2025-10-01 17:41:36 +02:00
setup.py add missing sphinx_design and pydata-sphinx-theme to docs extra 2026-07-20 11:06:34 +02:00
svm-perf-quantification-ext.patch many aggregative methods added 2020-12-03 18:12:28 +01:00

README.md

QuaPy

version 0.2.1

QuaPy is an open source framework for quantification (a.k.a. supervised prevalence estimation, or learning to quantify) written in Python.

QuaPy is based on the concept of “data sample”, and provides implementations of the most important aspects of the quantification workflow, such as (baseline and advanced) quantification methods, quantification-oriented model selection mechanisms, evaluation measures, and evaluations protocols used for evaluating quantification methods. QuaPy also makes available commonly used datasets, and offers visualization tools for facilitating the analysis and interpretation of the experimental results.

Last updates:

  • Version 0.2.1 is released! major changes can be consulted here.
  • The developer API documentation is available here
  • Manuals are available here

Installation

pip install quapy

Cite QuaPy

If you find QuaPy useful (and we hope you will), please consider citing the original paper in your research:

@inproceedings{moreo2021quapy,
  title={QuaPy: a python-based framework for quantification},
  author={Moreo, Alejandro and Esuli, Andrea and Sebastiani, Fabrizio},
  booktitle={Proceedings of the 30th ACM International Conference on Information \& Knowledge Management},
  pages={4534--4543},
  year={2021}
}

A quick example:

The following script fetches a dataset of tweets, trains, applies, and evaluates a quantifier based on the Adjusted Classify & Count quantification method, using, as the evaluation measure, the Mean Absolute Error (MAE) between the predicted and the true class prevalence values of the test set.

import quapy as qp

training, test = qp.datasets.fetch_UCIBinaryDataset("yeast").train_test

# create an "Adjusted Classify & Count" quantifier
model = qp.method.aggregative.ACC()
Xtr, ytr = training.Xy
model.fit(Xtr, ytr)

estim_prevalence = model.predict(test.X)
true_prevalence = test.prevalence()

error = qp.error.mae(true_prevalence, estim_prevalence)
print(f'Mean Absolute Error (MAE)={error:.3f}')

Quantification is useful in scenarios characterized by prior probability shift. In other words, we would be little interested in estimating the class prevalence values of the test set if we could assume the IID assumption to hold, as this prevalence would be roughly equivalent to the class prevalence of the training set. For this reason, any quantification model should be tested across many samples, even ones characterized by class prevalence values different or very different from those found in the training set. QuaPy implements sampling procedures and evaluation protocols that automate this workflow. See the documentation for detailed examples.

Features

  • Implementation of many popular quantification methods (Classify-&-Count and its variants, Expectation Maximization, quantification methods based on structured output learning, HDy, QuaNet, quantification ensembles, among others).
  • Support for uncertainty quantification via bootstrap-based and Bayesian methods, including confidence intervals and simplex-aware confidence regions.
  • Versatile functionality for performing evaluation based on sampling generation protocols (e.g., APP, NPP, etc.).
  • Implementation of most commonly used evaluation metrics (e.g., AE, RAE, NAE, NRAE, SE, KLD, NKLD, etc.).
  • Datasets frequently used in quantification (textual and numeric), including:
    • 32 UCI Machine Learning datasets.
    • 11 Twitter quantification-by-sentiment datasets.
    • 3 product reviews quantification-by-sentiment datasets.
    • 4 tasks from LeQua 2022 competition and 4 tasks from LeQua 2024 competition
    • IFCB for Plancton quantification
  • Native support for binary and single-label multiclass quantification scenarios.
  • Model selection functionality that minimizes quantification-oriented loss functions.
  • Visualization tools for analysing the experimental results.

Requirements

  • scikit-learn, numpy, scipy
  • pytorch (for QuaNet)
  • svmperf patched for quantification (see below)
  • joblib
  • tqdm
  • pandas, xlrd
  • matplotlib

Contributing

In case you want to contribute improvements to quapy, please generate pull request to the “devel” branch.

Documentation

Check out the developer API documentation here.

Check out the Manuals, in which many code examples are provided:

Acknowledgments:

SoBigData++

This work has been supported by the QuaDaSh project “Finanziato dallUnione europea—Next Generation EU, Missione 4 Componente 2 CUP B53D23026250001”.