1 | <?php |
||
7 | trait OneVsRest |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | protected $classifiers = []; |
||
14 | |||
15 | /** |
||
16 | * All provided training targets' labels. |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | protected $allLabels = []; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $costValues = []; |
||
26 | |||
27 | /** |
||
28 | * Train a binary classifier in the OvR style |
||
29 | * |
||
30 | * @param array $samples |
||
31 | * @param array $targets |
||
32 | */ |
||
33 | public function train(array $samples, array $targets) |
||
40 | |||
41 | /** |
||
42 | * @param array $samples |
||
43 | * @param array $targets |
||
44 | * @param array $allLabels All training set labels |
||
45 | * @return void |
||
46 | */ |
||
47 | protected function trainByLabel(array $samples, array $targets, array $allLabels = array()) |
||
90 | |||
91 | /** |
||
92 | * Resets the classifier and the vars internally used by OneVsRest to create multiple classifiers. |
||
93 | */ |
||
94 | public function reset() |
||
102 | |||
103 | /** |
||
104 | * Returns an instance of the current class after cleaning up OneVsRest stuff. |
||
105 | * |
||
106 | * @return \Phpml\Estimator |
||
107 | */ |
||
108 | protected function getClassifierCopy() |
||
118 | |||
119 | /** |
||
120 | * Groups all targets into two groups: Targets equal to |
||
121 | * the given label and the others |
||
122 | * |
||
123 | * $targets is not passed by reference nor contains objects so this method |
||
124 | * changes will not affect the caller $targets array. |
||
125 | * |
||
126 | * @param array $targets |
||
127 | * @param mixed $label |
||
128 | * @return array Binarized targets and target's labels |
||
129 | */ |
||
130 | private function binarizeTargets($targets, $label) |
||
141 | |||
142 | |||
143 | /** |
||
144 | * @param array $sample |
||
145 | * |
||
146 | * @return mixed |
||
147 | */ |
||
148 | protected function predictSample(array $sample) |
||
163 | |||
164 | /** |
||
165 | * Each classifier should implement this method instead of train(samples, targets) |
||
166 | * |
||
167 | * @param array $samples |
||
168 | * @param array $targets |
||
169 | * @param array $labels |
||
170 | */ |
||
171 | abstract protected function trainBinary(array $samples, array $targets, array $labels); |
||
172 | |||
173 | /** |
||
174 | * To be overwritten by OneVsRest classifiers. |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | abstract protected function resetBinary(); |
||
179 | |||
180 | /** |
||
181 | * Each classifier that make use of OvR approach should be able to |
||
182 | * return a probability for a sample to belong to the given label. |
||
183 | * |
||
184 | * @param array $sample |
||
185 | * |
||
186 | * @return mixed |
||
187 | */ |
||
188 | abstract protected function predictProbability(array $sample, string $label); |
||
189 | |||
190 | /** |
||
191 | * Each classifier should implement this method instead of predictSample() |
||
192 | * |
||
193 | * @param array $sample |
||
194 | * |
||
195 | * @return mixed |
||
196 | */ |
||
197 | abstract protected function predictSampleBinary(array $sample); |
||
198 | } |
||
199 |