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 |
||
10 | class LogisticRegression extends Adaline |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * Batch training: Gradient descent algorithm (default) |
||
15 | */ |
||
16 | const BATCH_TRAINING = 1; |
||
17 | |||
18 | /** |
||
19 | * Online training: Stochastic gradient descent learning |
||
20 | */ |
||
21 | const ONLINE_TRAINING = 2; |
||
22 | |||
23 | /** |
||
24 | * Conjugate Batch: Conjugate Gradient algorithm |
||
25 | */ |
||
26 | const CONJUGATE_GRAD_TRAINING = 3; |
||
27 | |||
28 | /** |
||
29 | * Cost function to optimize: 'log' and 'sse' are supported <br> |
||
30 | * - 'log' : log likelihood <br> |
||
31 | * - 'sse' : sum of squared errors <br> |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $costFunction = 'sse'; |
||
36 | |||
37 | /** |
||
38 | * Regularization term: only 'L2' is supported |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $penalty = 'L2'; |
||
43 | |||
44 | /** |
||
45 | * Lambda (λ) parameter of regularization term. If λ is set to 0, then |
||
46 | * regularization term is cancelled. |
||
47 | * |
||
48 | * @var float |
||
49 | */ |
||
50 | protected $lambda = 0.5; |
||
51 | |||
52 | /** |
||
53 | * Initalize a Logistic Regression classifier with maximum number of iterations |
||
54 | * and learning rule to be applied <br> |
||
55 | * |
||
56 | * Maximum number of iterations can be an integer value greater than 0 <br> |
||
57 | * If normalizeInputs is set to true, then every input given to the algorithm will be standardized |
||
58 | * by use of standard deviation and mean calculation <br> |
||
59 | * |
||
60 | * Cost function can be 'log' for log-likelihood and 'sse' for sum of squared errors <br> |
||
61 | * |
||
62 | * Penalty (Regularization term) can be 'L2' or empty string to cancel penalty term |
||
63 | * |
||
64 | * @param int $maxIterations |
||
65 | * @param bool $normalizeInputs |
||
66 | * @param int $trainingType |
||
67 | * @param string $cost |
||
68 | * @param string $penalty |
||
69 | * |
||
70 | * @throws \Exception |
||
71 | */ |
||
72 | public function __construct(int $maxIterations = 500, bool $normalizeInputs = true, |
||
100 | |||
101 | /** |
||
102 | * Sets the learning rate if gradient descent algorithm is |
||
103 | * selected for training |
||
104 | * |
||
105 | * @param float $learningRate |
||
106 | */ |
||
107 | public function setLearningRate(float $learningRate) |
||
111 | |||
112 | /** |
||
113 | * Lambda (λ) parameter of regularization term. If 0 is given, |
||
114 | * then the regularization term is cancelled |
||
115 | * |
||
116 | * @param float $lambda |
||
117 | */ |
||
118 | public function setLambda(float $lambda) |
||
122 | |||
123 | /** |
||
124 | * Adapts the weights with respect to given samples and targets |
||
125 | * by use of selected solver |
||
126 | */ |
||
127 | protected function runTraining() |
||
142 | |||
143 | /** |
||
144 | * Executes Conjugate Gradient method to optimize the |
||
145 | * weights of the LogReg model |
||
146 | */ |
||
147 | protected function runConjugateGradient(\Closure $gradientFunc) |
||
155 | |||
156 | /** |
||
157 | * Returns the appropriate callback function for the selected cost function |
||
158 | * |
||
159 | * @return \Closure |
||
160 | */ |
||
161 | protected function getCostFunction() |
||
224 | |||
225 | /** |
||
226 | * Returns the output of the network, a float value between 0.0 and 1.0 |
||
227 | * |
||
228 | * @param array $sample |
||
229 | * |
||
230 | * @return float |
||
231 | */ |
||
232 | protected function output(array $sample) |
||
238 | |||
239 | /** |
||
240 | * Returns the class value (either -1 or 1) for the given input |
||
241 | * |
||
242 | * @param array $sample |
||
243 | * @return int |
||
244 | */ |
||
245 | protected function outputClass(array $sample) |
||
255 | |||
256 | /** |
||
257 | * Returns the probability of the sample of belonging to the given label. |
||
258 | * |
||
259 | * The probability is simply taken as the distance of the sample |
||
260 | * to the decision plane. |
||
261 | * |
||
262 | * @param array $sample |
||
263 | * @param mixed $label |
||
264 | */ |
||
265 | View Code Duplication | protected function predictProbability(array $sample, $label) |
|
276 | } |
||
277 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.