php-ml/tests/Phpml/FeatureExtraction/TfIdfTransformerTest.php

32 lines
808 B
PHP
Raw Normal View History

2016-06-15 14:04:09 +00:00
<?php
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
2016-06-15 14:04:09 +00:00
namespace tests\Phpml\FeatureExtraction;
use Phpml\FeatureExtraction\TfIdfTransformer;
2017-02-03 11:58:25 +00:00
use PHPUnit\Framework\TestCase;
2016-06-15 14:04:09 +00:00
2017-02-03 11:58:25 +00:00
class TfIdfTransformerTest extends TestCase
2016-06-15 14:04:09 +00:00
{
public function testTfIdfTransformation()
{
2016-07-11 22:21:34 +00:00
// https://en.wikipedia.org/wiki/Tf-idf
2016-06-15 14:04:09 +00:00
$samples = [
[0 => 1, 1 => 1, 2 => 2, 3 => 1, 4 => 0, 5 => 0],
[0 => 1, 1 => 1, 2 => 0, 3 => 0, 4 => 2, 5 => 3],
];
$tfIdfSamples = [
[0 => 0, 1 => 0, 2 => 0.602, 3 => 0.301, 4 => 0, 5 => 0],
[0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0.602, 5 => 0.903],
];
$transformer = new TfIdfTransformer($samples);
$transformer->transform($samples);
2016-06-15 14:04:09 +00:00
$this->assertEquals($tfIdfSamples, $samples, '', 0.001);
2016-06-15 14:04:09 +00:00
}
}