Completed
Push — develop ( 021320...f04cc0 )
by Arkadiusz
03:15
created

RandomSplit::__construct()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 22
rs 8.6737
cc 5
eloc 14
nc 4
nop 3
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\CrossValidation;
6
7
use Phpml\Dataset\Dataset;
8
9
class RandomSplit extends Split
10
{
11
    /**
12
     * @param Dataset $dataset
13
     * @param float   $testSize
14
     */
15
    protected function splitDataset(Dataset $dataset, float $testSize)
16
    {
17
        $samples = $dataset->getSamples();
18
        $labels = $dataset->getTargets();
19
        $datasetSize = count($samples);
20
        $testCount = count($this->testSamples);
21
22
        for ($i = $datasetSize; $i > 0; --$i) {
23
            $key = mt_rand(0, $datasetSize - 1);
24
            $setName = (count($this->testSamples) - $testCount) / $datasetSize >= $testSize ? 'train' : 'test';
25
26
            $this->{$setName.'Samples'}[] = $samples[$key];
27
            $this->{$setName.'Labels'}[] = $labels[$key];
28
        }
29
    }
30
}
31