cii-text-analysis/vendor/rubix/ml/tests/Transformers/OneHotEncoderTest.php

90 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2026-02-10 16:19:59 +01:00
<?php
namespace Rubix\ML\Tests\Transformers;
use Rubix\ML\Datasets\Unlabeled;
use Rubix\ML\Transformers\Stateful;
use Rubix\ML\Transformers\Transformer;
use Rubix\ML\Transformers\OneHotEncoder;
use PHPUnit\Framework\TestCase;
use Rubix\ML\Exceptions\RuntimeException;
/**
* @group Transformers
* @covers \Rubix\ML\Transformers\OneHotEncoder
*/
class OneHotEncoderTest extends TestCase
{
/**
* @var \Rubix\ML\Datasets\Unlabeled
*/
protected $dataset;
/**
* @var \Rubix\ML\Transformers\OneHotEncoder
*/
protected $transformer;
/**
* @before
*/
protected function setUp() : void
{
$this->dataset = new Unlabeled([
['nice', 'furry', 'friendly'],
['mean', 'furry', 'loner'],
['nice', 'rough', 'friendly'],
['mean', 'rough', 'friendly'],
]);
$this->transformer = new OneHotEncoder();
}
/**
* @test
*/
public function build() : void
{
$this->assertInstanceOf(OneHotEncoder::class, $this->transformer);
$this->assertInstanceOf(Transformer::class, $this->transformer);
$this->assertInstanceOf(Stateful::class, $this->transformer);
}
/**
* @test
*/
public function fitTransform() : void
{
$this->transformer->fit($this->dataset);
$this->assertTrue($this->transformer->fitted());
$categories = $this->transformer->categories();
$this->assertIsArray($categories);
$this->assertCount(3, $categories);
$this->assertContainsOnly('array', $categories);
$this->dataset->apply($this->transformer);
$this->assertEquals([
[1, 0, 1, 0, 1, 0],
[0, 1, 1, 0, 0, 1],
[1, 0, 0, 1, 1, 0],
[0, 1, 0, 1, 1, 0],
], $this->dataset->samples());
}
/**
* @test
*/
public function transformUnfitted() : void
{
$this->expectException(RuntimeException::class);
$samples = $this->dataset->samples();
$this->transformer->transform($samples);
}
}