php-ml/src/Dataset/ArrayDataset.php

63 lines
1.2 KiB
PHP
Raw Normal View History

2016-04-07 20:12:36 +00:00
<?php
2016-04-07 20:35:49 +00:00
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
2016-04-07 20:12:36 +00:00
namespace Phpml\Dataset;
2016-04-07 20:13:31 +00:00
use Phpml\Exception\InvalidArgumentException;
2016-04-07 20:12:36 +00:00
class ArrayDataset implements Dataset
{
/**
* @var array
*/
2016-04-07 20:19:04 +00:00
protected $samples = [];
2016-04-07 20:12:36 +00:00
/**
* @var array
*/
2016-06-16 21:56:15 +00:00
protected $targets = [];
2016-04-07 20:12:36 +00:00
/**
2016-04-07 20:13:31 +00:00
* @throws InvalidArgumentException
2016-04-07 20:12:36 +00:00
*/
2016-06-16 21:56:15 +00:00
public function __construct(array $samples, array $targets)
2016-04-07 20:12:36 +00:00
{
if (count($samples) !== count($targets)) {
throw new InvalidArgumentException('Size of given arrays does not match');
2016-04-07 20:13:31 +00:00
}
2016-04-07 20:12:36 +00:00
$this->samples = $samples;
2016-06-16 21:56:15 +00:00
$this->targets = $targets;
2016-04-07 20:12:36 +00:00
}
public function getSamples(): array
2016-04-07 20:12:36 +00:00
{
return $this->samples;
}
public function getTargets(): array
2016-04-07 20:12:36 +00:00
{
2016-06-16 21:56:15 +00:00
return $this->targets;
2016-04-07 20:12:36 +00:00
}
/**
* @param int[] $columns
*/
public function removeColumns(array $columns): void
{
foreach ($this->samples as &$sample) {
$this->removeColumnsFromSample($sample, $columns);
}
}
private function removeColumnsFromSample(array &$sample, array $columns): void
{
foreach ($columns as $index) {
unset($sample[$index]);
}
$sample = array_values($sample);
}
2016-04-07 20:12:36 +00:00
}