1 | <?php |
||
7 | trait OneVsRest |
||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | protected $samples = []; |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $targets = []; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $classifiers; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $labels; |
||
28 | |||
29 | /** |
||
30 | * Train a binary classifier in the OvR style |
||
31 | * |
||
32 | * @param array $samples |
||
33 | * @param array $targets |
||
34 | */ |
||
35 | public function train(array $samples, array $targets) |
||
60 | |||
61 | /** |
||
62 | * Groups all targets into two groups: Targets equal to |
||
63 | * the given label and the others |
||
64 | * |
||
65 | * @param mixed $label |
||
66 | */ |
||
67 | private function binarizeTargets($label) |
||
77 | |||
78 | |||
79 | /** |
||
80 | * @param array $sample |
||
81 | * |
||
82 | * @return mixed |
||
83 | */ |
||
84 | protected function predictSample(array $sample) |
||
99 | |||
100 | /** |
||
101 | * Each classifier should implement this method instead of train(samples, targets) |
||
102 | * |
||
103 | * @param array $samples |
||
104 | * @param array $targets |
||
105 | */ |
||
106 | abstract protected function trainBinary(array $samples, array $targets); |
||
107 | |||
108 | /** |
||
109 | * Each classifier that make use of OvR approach should be able to |
||
110 | * return a probability for a sample to belong to the given label. |
||
111 | * |
||
112 | * @param array $sample |
||
113 | * |
||
114 | * @return mixed |
||
115 | */ |
||
116 | abstract protected function predictProbability(array $sample, string $label); |
||
117 | |||
118 | /** |
||
119 | * Each classifier should implement this method instead of predictSample() |
||
120 | * |
||
121 | * @param array $sample |
||
122 | * |
||
123 | * @return mixed |
||
124 | */ |
||
125 | abstract protected function predictSampleBinary(array $sample); |
||
126 | } |
||
127 |