Ensure DataTransformer::testSet samples array is not empty (#204)
This commit is contained in:
parent
4562f1dfc9
commit
9e375ca544
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Phpml\SupportVectorMachine;
|
namespace Phpml\SupportVectorMachine;
|
||||||
|
|
||||||
|
use Phpml\Exception\InvalidArgumentException;
|
||||||
|
|
||||||
class DataTransformer
|
class DataTransformer
|
||||||
{
|
{
|
||||||
public static function trainingSet(array $samples, array $labels, bool $targets = false): string
|
public static function trainingSet(array $samples, array $labels, bool $targets = false): string
|
||||||
|
@ -24,6 +26,10 @@ class DataTransformer
|
||||||
|
|
||||||
public static function testSet(array $samples): string
|
public static function testSet(array $samples): string
|
||||||
{
|
{
|
||||||
|
if (empty($samples)) {
|
||||||
|
throw InvalidArgumentException::arrayCantBeEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
if (!is_array($samples[0])) {
|
if (!is_array($samples[0])) {
|
||||||
$samples = [$samples];
|
$samples = [$samples];
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Phpml\Tests\SupportVectorMachine;
|
namespace Phpml\Tests\SupportVectorMachine;
|
||||||
|
|
||||||
|
use Phpml\Exception\InvalidArgumentException;
|
||||||
use Phpml\SupportVectorMachine\DataTransformer;
|
use Phpml\SupportVectorMachine\DataTransformer;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
@ -78,4 +79,12 @@ class DataTransformerTest extends TestCase
|
||||||
|
|
||||||
$this->assertEquals($probabilities, DataTransformer::probabilities($rawPredictions, $labels));
|
$this->assertEquals($probabilities, DataTransformer::probabilities($rawPredictions, $labels));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testThrowExceptionWhenTestSetIsEmpty(): void
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
$this->expectExceptionMessage('The array has zero elements');
|
||||||
|
|
||||||
|
DataTransformer::testSet([]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue