1 | <?php |
||
9 | trait OneVsRest |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $classifiers = []; |
||
15 | |||
16 | /** |
||
17 | * All provided training targets' labels. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $allLabels = []; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $costValues = []; |
||
27 | |||
28 | /** |
||
29 | * Train a binary classifier in the OvR style |
||
30 | */ |
||
31 | public function train(array $samples, array $targets): void |
||
38 | |||
39 | /** |
||
40 | * Resets the classifier and the vars internally used by OneVsRest to create multiple classifiers. |
||
41 | */ |
||
42 | public function reset(): void |
||
50 | |||
51 | protected function trainByLabel(array $samples, array $targets, array $allLabels = []): void |
||
92 | |||
93 | /** |
||
94 | * Returns an instance of the current class after cleaning up OneVsRest stuff. |
||
95 | * |
||
96 | * @return Classifier|OneVsRest |
||
97 | */ |
||
98 | protected function getClassifierCopy() |
||
108 | |||
109 | /** |
||
110 | * @return mixed |
||
111 | */ |
||
112 | protected function predictSample(array $sample) |
||
128 | |||
129 | /** |
||
130 | * Each classifier should implement this method instead of train(samples, targets) |
||
131 | */ |
||
132 | abstract protected function trainBinary(array $samples, array $targets, array $labels); |
||
133 | |||
134 | /** |
||
135 | * To be overwritten by OneVsRest classifiers. |
||
136 | */ |
||
137 | abstract protected function resetBinary(): void; |
||
138 | |||
139 | /** |
||
140 | * Each classifier that make use of OvR approach should be able to |
||
141 | * return a probability for a sample to belong to the given label. |
||
142 | * |
||
143 | * @return mixed |
||
144 | */ |
||
145 | abstract protected function predictProbability(array $sample, string $label); |
||
146 | |||
147 | /** |
||
148 | * Each classifier should implement this method instead of predictSample() |
||
149 | * |
||
150 | * @return mixed |
||
151 | */ |
||
152 | abstract protected function predictSampleBinary(array $sample); |
||
153 | |||
154 | /** |
||
155 | * Groups all targets into two groups: Targets equal to |
||
156 | * the given label and the others |
||
157 | * |
||
158 | * $targets is not passed by reference nor contains objects so this method |
||
159 | * changes will not affect the caller $targets array. |
||
160 | * |
||
161 | * @param mixed $label |
||
162 | * |
||
163 | * @return array Binarized targets and target's labels |
||
164 | */ |
||
165 | private function binarizeTargets(array $targets, $label): array |
||
176 | } |
||
177 |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.