|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Module\ML\Application\Model; |
| 4 | + |
| 5 | +use App\Core\Application\Path\AppPathResolver; |
| 6 | +use Rubix\ML\CrossValidation\Reports\AggregateReport; |
| 7 | +use Rubix\ML\CrossValidation\Reports\ConfusionMatrix; |
| 8 | +use Rubix\ML\CrossValidation\Reports\MulticlassBreakdown; |
| 9 | +use Rubix\ML\Datasets\Labeled; |
| 10 | +use Rubix\ML\Extractors\CSV; |
| 11 | +use Rubix\ML\PersistentModel; |
| 12 | +use Rubix\ML\Persisters\Filesystem; |
| 13 | +use Rubix\ML\Report; |
| 14 | + |
| 15 | +/** |
| 16 | + * @see https://docs.rubixml.com/latest/cross-validation.html |
| 17 | + * @see https://docs.rubixml.com/latest/cross-validation/reports/multiclass-breakdown.html |
| 18 | + * @see https://docs.rubixml.com/latest/cross-validation/reports/confusion-matrix.html |
| 19 | + */ |
| 20 | +readonly class SpamModelReport |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + private AppPathResolver $appPathResolver, |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + public function generateReport( |
| 28 | + string $testingDatasetFilename, |
| 29 | + string $modelFilename, |
| 30 | + ): Report { |
| 31 | + $dataset = Labeled::fromIterator(new CSV( |
| 32 | + $this->appPathResolver->getDatasetPath($testingDatasetFilename), |
| 33 | + header: true, |
| 34 | + )); |
| 35 | + |
| 36 | + $estimator = PersistentModel::load(new Filesystem( |
| 37 | + $this->appPathResolver->getModelPath($modelFilename) |
| 38 | + )); |
| 39 | + |
| 40 | + $predictions = $estimator->predict($dataset); |
| 41 | + |
| 42 | + $report = new AggregateReport([ |
| 43 | + new MulticlassBreakdown(), |
| 44 | + new ConfusionMatrix(), |
| 45 | + ]); |
| 46 | + |
| 47 | + return $report->generate($predictions, $dataset->labels()); |
| 48 | + } |
| 49 | +} |
0 commit comments