/** * @param Dataset $dataset * * @return Dataset[]|array */ private function splitByTarget(Dataset $dataset) : array { $targets = $dataset->getTargets(); $samples = $dataset->getSamples(); $uniqueTargets = array_unique($targets); $split = array_combine($uniqueTargets, array_fill(0, count($uniqueTargets), [])); foreach ($samples as $key => $sample) { $split[$targets[$key]][] = $sample; } $datasets = $this->createDatasets($uniqueTargets, $split); return $datasets; }
/** * @param Dataset $dataset * @param float $testSize */ protected function splitDataset(Dataset $dataset, float $testSize) { $samples = $dataset->getSamples(); $labels = $dataset->getTargets(); $datasetSize = count($samples); $testCount = count($this->testSamples); for ($i = $datasetSize; $i > 0; --$i) { $key = mt_rand(0, $datasetSize - 1); $setName = (count($this->testSamples) - $testCount) / $datasetSize >= $testSize ? 'train' : 'test'; $this->{$setName . 'Samples'}[] = $samples[$key]; $this->{$setName . 'Labels'}[] = $labels[$key]; } }