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

Split::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 5
nc 2
nop 3
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\CrossValidation;
6
7
use Phpml\Dataset\Dataset;
8
use Phpml\Exception\InvalidArgumentException;
9
10
abstract class Split
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $trainSamples = [];
16
17
    /**
18
     * @var array
19
     */
20
    protected $testSamples = [];
21
22
    /**
23
     * @var array
24
     */
25
    protected $trainLabels = [];
26
27
    /**
28
     * @var array
29
     */
30
    protected $testLabels = [];
31
32
    /**
33
     * @param Dataset $dataset
34
     * @param float   $testSize
35
     * @param int     $seed
36
     *
37
     * @throws InvalidArgumentException
38
     */
39
    public function __construct(Dataset $dataset, float $testSize = 0.3, int $seed = null)
40
    {
41
        if (0 >= $testSize || 1 <= $testSize) {
42
            throw InvalidArgumentException::percentNotInRange('testSize');
43
        }
44
        $this->seedGenerator($seed);
45
46
        $this->splitDataset($dataset, $testSize);
47
    }
48
49
    abstract protected function splitDataset(Dataset $dataset, float $testSize);
50
51
    /**
52
     * @return array
53
     */
54
    public function getTrainSamples()
55
    {
56
        return $this->trainSamples;
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function getTestSamples()
63
    {
64
        return $this->testSamples;
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function getTrainLabels()
71
    {
72
        return $this->trainLabels;
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function getTestLabels()
79
    {
80
        return $this->testLabels;
81
    }
82
83
    /**
84
     * @param int|null $seed
85
     */
86
    protected function seedGenerator(int $seed = null)
87
    {
88
        if (null === $seed) {
89
            mt_srand();
90
        } else {
91
            mt_srand($seed);
92
        }
93
    }
94
}
95