Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
16 | class Perceptron implements Classifier, IncrementalEstimator |
||
17 | { |
||
18 | use Predictable, OneVsRest; |
||
19 | |||
20 | /** |
||
21 | * @var \Phpml\Helper\Optimizer\Optimizer |
||
22 | */ |
||
23 | protected $optimizer; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $labels = []; |
||
29 | |||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | protected $featureCount = 0; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $weights; |
||
39 | |||
40 | /** |
||
41 | * @var float |
||
42 | */ |
||
43 | protected $learningRate; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $maxIterations; |
||
49 | |||
50 | /** |
||
51 | * @var Normalizer |
||
52 | */ |
||
53 | protected $normalizer; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | protected $enableEarlyStop = true; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $costValues = []; |
||
64 | |||
65 | /** |
||
66 | * Initalize a perceptron classifier with given learning rate and maximum |
||
67 | * number of iterations used while training the perceptron <br> |
||
68 | * |
||
69 | * Learning rate should be a float value between 0.0(exclusive) and 1.0(inclusive) <br> |
||
70 | * Maximum number of iterations can be an integer value greater than 0 |
||
71 | * @param int $learningRate |
||
72 | * @param int $maxIterations |
||
73 | */ |
||
74 | public function __construct(float $learningRate = 0.001, int $maxIterations = 1000, |
||
92 | |||
93 | /** |
||
94 | * @param array $samples |
||
95 | * @param array $targets |
||
96 | * @param array $labels |
||
97 | */ |
||
98 | public function partialTrain(array $samples, array $targets, array $labels = array()) |
||
102 | |||
103 | /** |
||
104 | * @param array $samples |
||
105 | * @param array $targets |
||
106 | * @param array $labels |
||
107 | */ |
||
108 | public function trainBinary(array $samples, array $targets, array $labels) |
||
126 | |||
127 | protected function resetBinary() |
||
135 | |||
136 | /** |
||
137 | * Normally enabling early stopping for the optimization procedure may |
||
138 | * help saving processing time while in some cases it may result in |
||
139 | * premature convergence.<br> |
||
140 | * |
||
141 | * If "false" is given, the optimization procedure will always be executed |
||
142 | * for $maxIterations times |
||
143 | * |
||
144 | * @param bool $enable |
||
145 | */ |
||
146 | public function setEarlyStop(bool $enable = true) |
||
152 | |||
153 | /** |
||
154 | * Returns the cost values obtained during the training. |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | public function getCostValues() |
||
162 | |||
163 | /** |
||
164 | * Trains the perceptron model with Stochastic Gradient Descent optimization |
||
165 | * to get the correct set of weights |
||
166 | * |
||
167 | * @param array $samples |
||
168 | * @param array $targets |
||
169 | */ |
||
170 | protected function runTraining(array $samples, array $targets) |
||
185 | |||
186 | /** |
||
187 | * Executes a Gradient Descent algorithm for |
||
188 | * the given cost function |
||
189 | * |
||
190 | * @param array $samples |
||
191 | * @param array $targets |
||
192 | */ |
||
193 | protected function runGradientDescent(array $samples, array $targets, \Closure $gradientFunc, bool $isBatch = false) |
||
208 | |||
209 | /** |
||
210 | * Checks if the sample should be normalized and if so, returns the |
||
211 | * normalized sample |
||
212 | * |
||
213 | * @param array $sample |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | protected function checkNormalizedSample(array $sample) |
||
227 | |||
228 | /** |
||
229 | * Calculates net output of the network as a float value for the given input |
||
230 | * |
||
231 | * @param array $sample |
||
232 | * @return int |
||
233 | */ |
||
234 | protected function output(array $sample) |
||
247 | |||
248 | /** |
||
249 | * Returns the class value (either -1 or 1) for the given input |
||
250 | * |
||
251 | * @param array $sample |
||
252 | * @return int |
||
253 | */ |
||
254 | protected function outputClass(array $sample) |
||
258 | |||
259 | /** |
||
260 | * Returns the probability of the sample of belonging to the given label. |
||
261 | * |
||
262 | * The probability is simply taken as the distance of the sample |
||
263 | * to the decision plane. |
||
264 | * |
||
265 | * @param array $sample |
||
266 | * @param mixed $label |
||
267 | */ |
||
268 | View Code Duplication | protected function predictProbability(array $sample, $label) |
|
279 | |||
280 | /** |
||
281 | * @param array $sample |
||
282 | * @return mixed |
||
283 | */ |
||
284 | protected function predictSampleBinary(array $sample) |
||
292 | } |
||
293 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..