PHPackages                             sphamster/classification-metrics - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sphamster/classification-metrics

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sphamster/classification-metrics
================================

PHP package to compute confusion matrices and classification metrics

v1.0.0(10mo ago)02MITPHPPHP ^8.1CI passing

Since Jul 19Pushed 10mo agoCompare

[ Source](https://github.com/sphamster/classification-metrics)[ Packagist](https://packagist.org/packages/sphamster/classification-metrics)[ RSS](/packages/sphamster-classification-metrics/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (7)Versions (2)Used By (0)

Classification Metrics for PHP
==============================

[](#classification-metrics-for-php)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1c7157fe46bdf922a09e03296b61db07f418da2812b18e9ea30f85e34e24c61d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f737068616d737465722f636c617373696669636174696f6e2d6d6574726963732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sphamster/classification-metrics)[![GitHub Tests Action Status](https://camo.githubusercontent.com/7bc894790ec073d4764a0a2829e95652db7aa5afb3812b9c1a4ef17ed386a8d7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f737068616d737465722f636c617373696669636174696f6e2d6d6574726963732f70756c6c2d726571756573742e796d6c3f6272616e63683d6d6173746572266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/sphamster/classification-metrics/actions?query=workflow%3Arun-tests+branch%3Amain)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![PHP Version](https://camo.githubusercontent.com/4c37146ddaf0c22a728307816e2491aecd7a7a07926a9c2ee8385ae8f9990951/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f737068616d737465722f636c617373696669636174696f6e2d6d6574726963732f7068702e7376673f7374796c653d666c61742d737175617265)](composer.json)[![PHPStan Level](https://camo.githubusercontent.com/fa7d257d0c5c1cf237ac3490ef3a5561626b17fcb0a8547c01b0bb8746554e60/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://phpstan.org/)[![Code Coverage](https://camo.githubusercontent.com/8b94d24cafb5a4f63f51139f87b8a264ab2702ef64b2a25abb42272951457d86/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f737068616d737465722f636c617373696669636174696f6e2d6d6574726963733f7374796c653d666c61742d737175617265)](https://codecov.io/gh/sphamster/classification-metrics)

A PHP package for computing confusion matrices and classification metrics for machine learning models.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require sphamster/classification-metrics
```

Requirements
------------

[](#requirements)

- PHP 8.1 or higher

Usage
-----

[](#usage)

### Creating a Confusion Matrix

[](#creating-a-confusion-matrix)

You can create a confusion matrix directly from predictions:

```
use Sphamster\ClassificationMetrics\ConfusionMatrix;

// Your ground truth labels
$true_labels = ['cat', 'dog', 'cat', 'bird', 'dog', 'bird'];

// Your model's predictions
$predicted_labels = ['cat', 'dog', 'dog', 'bird', 'cat', 'bird'];

// Optional: specify the order of labels (if omitted, will use unique values from true_labels)
$labels = ['cat', 'dog', 'bird'];

// Create the confusion matrix
$confusion_matrix = ConfusionMatrix::fromPredictions($true_labels, $predicted_labels, $labels);
```

Or you can create it directly from a matrix:

```
$labels = ['cat', 'dog', 'bird'];
$matrix = [
    [5, 1, 0],  // cat:  5 correct, 1 as dog, 0 as bird
    [2, 8, 1],  // dog:  2 as cat, 8 correct, 1 as bird
    [0, 0, 6]   // bird: 0 as cat, 0 as dog, 6 correct
];

$confusion_matrix = new ConfusionMatrix($labels, $matrix);
```

### Extracting Basic Metrics

[](#extracting-basic-metrics)

The confusion matrix provides methods to extract basic metrics:

```
// Get true positives for all classes
$tp = $confusion_matrix->truePositives();
// Or for a specific class
$tp_cat = $confusion_matrix->truePositives('cat');

// Similarly for false positives, false negatives, and true negatives
$fp = $confusion_matrix->falsePositives();
$fn = $confusion_matrix->falseNegatives();
$tn = $confusion_matrix->trueNegatives();
```

### Computing Classification Metrics

[](#computing-classification-metrics)

The package provides implementations for common classification metrics:

#### Precision

[](#precision)

```
use Sphamster\ClassificationMetrics\Metrics\Precision;
use Sphamster\ClassificationMetrics\Enums\AverageStrategy;

// Get precision for each class
$precision = new Precision();
$class_precision = $precision->measure($confusion_matrix);

// Get macro-averaged precision
$macro_precision = (new Precision(AverageStrategy::MACRO))->measure($confusion_matrix);

// Get micro-averaged precision
$micro_precision = (new Precision(AverageStrategy::MICRO))->measure($confusion_matrix);

// Get weighted-averaged precision
$weighted_precision = (new Precision(AverageStrategy::WEIGHTED))->measure($confusion_matrix);
```

#### Recall

[](#recall)

```
use Sphamster\ClassificationMetrics\Metrics\Recall;

// Get recall for each class
$recall = new Recall();
$class_recall = $recall->measure($confusion_matrix);

// Similarly, you can use AverageStrategy for macro, micro, and weighted averaging
```

#### F1 Score

[](#f1-score)

```
use Sphamster\ClassificationMetrics\Metrics\F1Score;

// Get F1 score for each class
$f1 = new F1Score();
$class_f1 = $f1->measure($confusion_matrix);

// Similarly, you can use AverageStrategy for macro, micro, and weighted averaging
```

Averaging Strategies
--------------------

[](#averaging-strategies)

The package supports three averaging strategies for multi-class metrics:

- **Macro**: Calculate metrics for each label and find their unweighted mean. This does not take label imbalance into account.
- **Micro**: Calculate metrics globally by counting the total true positives, false negatives, and false positives.
- **Weighted**: Calculate metrics for each label and find their average weighted by support (the number of true instances for each label).

Testing
-------

[](#testing)

```
composer test
```

Code Quality
------------

[](#code-quality)

The package includes tools for maintaining code quality:

```
# Run code style fixer
composer lint

# Run static analysis
composer test:types

# Run refactoring tool
composer refactor

# Run all checks
composer test
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

Author
------

[](#author)

- [Andrea Civita](https://github.com/andreacivita)

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance54

Moderate activity, may be stable

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

304d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3ca8ae45c81288c22836f584e70c2e3dd2ac2ef2a22bea404d5eb91b4f669649?d=identicon)[andreacivita](/maintainers/andreacivita)

---

Top Contributors

[![andreacivita](https://avatars.githubusercontent.com/u/4959092?v=4)](https://github.com/andreacivita "andreacivita (5 commits)")

###  Code Quality

TestsPest

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sphamster-classification-metrics/health.svg)

```
[![Health](https://phpackages.com/badges/sphamster-classification-metrics/health.svg)](https://phpackages.com/packages/sphamster-classification-metrics)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
