# Scikit-P4: P4 Metric Calculation for Python
[](LICENSE)
Scikit-P4 is a Python library for calculating the P4 metric [1][2] across binary, multiclass, and multilabel classification tasks. Its API closely follows that of Scikit-learn’s metrics (e.g., `f1_score`).
# Installation
```
pip install scikit-p4
```
# Usage
## Binary Case
Example for a binary classification problem.
```python
from skp4.metrics import p4_score
y_true = [0, 0, 0, 0, 1, 1, 1, 1, 0, 0]
y_pred = [0, 1, 0, 0, 1, 1, 1, 0, 1, 0]
p4_score(y_true, y_pred)
```
**Output**
```
np.float64(0.6956521739130435)
```
## Multiclass Case
Example with multiple classes.
```python
from skp4.metrics import p4_score
y_true = ['versicolor', 'versicolor', 'setosa', 'setosa', 'virginica', 'virginica', 'setosa']
y_pred = ['versicolor', 'setosa', 'setosa', 'virginica', 'setosa', 'virginica', 'setosa']
p4_score(y_true, y_pred)
```
**Output**
Returns a `MultiResult` object:
```
micro avg: 0.6617
macro avg: 0.6520
weighted avg: 0.6405
samples avg: 0.5714
```
## Multilabel Case
Example with multilabel classification.
```python
from skp4.metrics import p4_score
y_true = [['A', 'B'], ['A', 'C'], ['B'], ['A', 'B', 'C']]
y_pred = [['A'], ['A', 'B'], [], ['A', 'B', 'C']]
p4_score(y_true, y_pred)
```
**Output**
Returns a `MultiResult` object:
```
micro avg: 0.6522
macro avg: 0.5758
weighted avg: 0.5568
samples avg: 0.1667
```
# Function signature
```python
def p4_score(y_true, y_pred, *, zero_division='warn') -> np.number | MultiResult:
...
```
Calculates the P4 metric for binary, multiclass, and multilabel classification tasks.
The classifier type is detected automatically.
Parameters:
* `y_true` – Ground-truth labels.
* `y_pred` – Predicted labels.
* `zero_division` – Defines the behavior when division by zero occurs (compatible with Scikit-learn):
* `0.0` – return 0 in case of division by zero
* `1.0` – return 1 in case of division by zero
* `np.nan` – return NaN in case of division by zero
* `warn` (default) – return 0 and issue a warning
# Remarks
1. In the **multiclass** case, the *samples average* for the P4 metric equals *accuracy*, just like for the F1 metric.
2. In the **multilabel** case, it is possible to obtain a P4 result below 1.0 even when `y_true` is perfectly matched.
This occurs due to division-by-zero issues, which are common to many true-negative–dependent metrics.
To avoid this:
* Use the *micro average*, which does not exhibit this issue.
* Set the parameter `zero_division=1.0` during calculation.
# References
[1] Wikipedia – [P4 metric](https://en.wikipedia.org/wiki/P4-metric)
[2] Sitarz, Mikołaj (2023). *Extending F1 Metric: A Probabilistic Approach*.
Advances in Artificial Intelligence and Machine Learning, 03(2), 1025–1038.
[arXiv:2210.11997](https://arxiv.org/abs/2210.11997)
# Contributing / License
Contributions are welcome! If you would like to report a bug, suggest an improvement,
or contribute code, please open an issue or submit a pull request on GitHub.
This project is licensed under the **BSD 3-Clause License**.
See the [LICENSE](LICENSE) file for details.
Raw data
{
"_id": null,
"home_page": "https://github.com/mikolaj1024/scikit-p4",
"name": "scikit-p4",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.11",
"maintainer_email": null,
"keywords": "metrics, classification, p4, scikit-learn",
"author": "Mikolaj Sitarz",
"author_email": "sitarz@refaba.com",
"download_url": "https://files.pythonhosted.org/packages/12/7f/3a80716daaac12dd2a62d21187ce92a7bcb567ae5cffa1de3ff17498a569/scikit_p4-0.1.0.tar.gz",
"platform": null,
"description": "# Scikit-P4: P4 Metric Calculation for Python\n[](LICENSE)\n\nScikit-P4 is a Python library for calculating the P4 metric [1][2] across binary, multiclass, and multilabel classification tasks. Its API closely follows that of Scikit-learn\u2019s metrics (e.g., `f1_score`).\n\n# Installation\n```\npip install scikit-p4\n```\n\n# Usage\n## Binary Case\nExample for a binary classification problem.\n```python\nfrom skp4.metrics import p4_score\ny_true = [0, 0, 0, 0, 1, 1, 1, 1, 0, 0]\ny_pred = [0, 1, 0, 0, 1, 1, 1, 0, 1, 0]\np4_score(y_true, y_pred)\n```\n**Output**\n```\nnp.float64(0.6956521739130435)\n```\n\n## Multiclass Case\nExample with multiple classes.\n```python\nfrom skp4.metrics import p4_score\ny_true = ['versicolor', 'versicolor', 'setosa', 'setosa', 'virginica', 'virginica', 'setosa']\ny_pred = ['versicolor', 'setosa', 'setosa', 'virginica', 'setosa', 'virginica', 'setosa']\np4_score(y_true, y_pred)\n```\n**Output**\n\nReturns a `MultiResult` object:\n```\n micro avg: 0.6617\n macro avg: 0.6520\nweighted avg: 0.6405\n samples avg: 0.5714\n```\n\n## Multilabel Case\nExample with multilabel classification.\n```python\nfrom skp4.metrics import p4_score\ny_true = [['A', 'B'], ['A', 'C'], ['B'], ['A', 'B', 'C']]\ny_pred = [['A'], ['A', 'B'], [], ['A', 'B', 'C']]\np4_score(y_true, y_pred)\n```\n**Output**\n\nReturns a `MultiResult` object:\n```\n micro avg: 0.6522\n macro avg: 0.5758\nweighted avg: 0.5568\n samples avg: 0.1667\n```\n\n# Function signature\n```python\ndef p4_score(y_true, y_pred, *, zero_division='warn') -> np.number | MultiResult:\n ...\n```\nCalculates the P4 metric for binary, multiclass, and multilabel classification tasks.\nThe classifier type is detected automatically.\n\nParameters:\n* `y_true` \u2013 Ground-truth labels.\n* `y_pred` \u2013 Predicted labels.\n* `zero_division` \u2013 Defines the behavior when division by zero occurs (compatible with Scikit-learn):\n * `0.0` \u2013 return 0 in case of division by zero\n * `1.0` \u2013 return 1 in case of division by zero\n * `np.nan` \u2013 return NaN in case of division by zero\n * `warn` (default) \u2013 return 0 and issue a warning\n\n# Remarks\n1. In the **multiclass** case, the *samples average* for the P4 metric equals *accuracy*, just like for the F1 metric.\n\n2. In the **multilabel** case, it is possible to obtain a P4 result below 1.0 even when `y_true` is perfectly matched. \n This occurs due to division-by-zero issues, which are common to many true-negative\u2013dependent metrics.\n To avoid this:\n * Use the *micro average*, which does not exhibit this issue.\n * Set the parameter `zero_division=1.0` during calculation.\n\n\n# References\n[1] Wikipedia \u2013 [P4 metric](https://en.wikipedia.org/wiki/P4-metric)\n\n[2] Sitarz, Miko\u0142aj (2023). *Extending F1 Metric: A Probabilistic Approach*. \n Advances in Artificial Intelligence and Machine Learning, 03(2), 1025\u20131038. \n [arXiv:2210.11997](https://arxiv.org/abs/2210.11997)\n\n# Contributing / License\nContributions are welcome! If you would like to report a bug, suggest an improvement, \nor contribute code, please open an issue or submit a pull request on GitHub.\n\nThis project is licensed under the **BSD 3-Clause License**. \nSee the [LICENSE](LICENSE) file for details.\n",
"bugtrack_url": null,
"license": "BSD-3-Clause",
"summary": "P4 metric calculation library compatible with scikit-learn",
"version": "0.1.0",
"project_urls": {
"Homepage": "https://github.com/mikolaj1024/scikit-p4",
"Repository": "https://github.com/mikolaj1024/scikit-p4"
},
"split_keywords": [
"metrics",
" classification",
" p4",
" scikit-learn"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "788c4826581a2a129ef32f4d1293bb9a8ea468845cae162b2a84832dd7eb4e1c",
"md5": "ac76a52727c4e2e61ee32a72197e4c1c",
"sha256": "8b7b0534042e46abf93b2de460e820d35d50b2283a49fdea327e4be95397910c"
},
"downloads": -1,
"filename": "scikit_p4-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ac76a52727c4e2e61ee32a72197e4c1c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11",
"size": 13443,
"upload_time": "2025-08-31T18:08:14",
"upload_time_iso_8601": "2025-08-31T18:08:14.637681Z",
"url": "https://files.pythonhosted.org/packages/78/8c/4826581a2a129ef32f4d1293bb9a8ea468845cae162b2a84832dd7eb4e1c/scikit_p4-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "127f3a80716daaac12dd2a62d21187ce92a7bcb567ae5cffa1de3ff17498a569",
"md5": "d976b36ebfe1af7000f32b71b893c7e0",
"sha256": "aadd8925799ad1938b4891abdcb04e7e6b6c4601155c1c226b4d16c4bcf810d8"
},
"downloads": -1,
"filename": "scikit_p4-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "d976b36ebfe1af7000f32b71b893c7e0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11",
"size": 9272,
"upload_time": "2025-08-31T18:08:16",
"upload_time_iso_8601": "2025-08-31T18:08:16.365518Z",
"url": "https://files.pythonhosted.org/packages/12/7f/3a80716daaac12dd2a62d21187ce92a7bcb567ae5cffa1de3ff17498a569/scikit_p4-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-31 18:08:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mikolaj1024",
"github_project": "scikit-p4",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "scikit-p4"
}