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 |
||
| 13 | class DecisionStump extends WeightedClassifier |
||
| 14 | { |
||
| 15 | use Predictable, OneVsRest; |
||
| 16 | |||
| 17 | const AUTO_SELECT = -1; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | protected $givenColumnIndex; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $binaryLabels; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Lowest error rate obtained while training/optimizing the model |
||
| 31 | * |
||
| 32 | * @var float |
||
| 33 | */ |
||
| 34 | protected $trainingErrorRate; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | protected $column; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var mixed |
||
| 43 | */ |
||
| 44 | protected $value; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $operator; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $columnTypes; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | protected $featureCount; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var float |
||
| 63 | */ |
||
| 64 | protected $numSplitCount = 100.0; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Distribution of samples in the leaves |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $prob; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * A DecisionStump classifier is a one-level deep DecisionTree. It is generally |
||
| 75 | * used with ensemble algorithms as in the weak classifier role. <br> |
||
| 76 | * |
||
| 77 | * If columnIndex is given, then the stump tries to produce a decision node |
||
| 78 | * on this column, otherwise in cases given the value of -1, the stump itself |
||
| 79 | * decides which column to take for the decision (Default DecisionTree behaviour) |
||
| 80 | * |
||
| 81 | * @param int $columnIndex |
||
| 82 | */ |
||
| 83 | public function __construct(int $columnIndex = self::AUTO_SELECT) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param array $samples |
||
| 90 | * @param array $targets |
||
| 91 | * @param array $labels |
||
| 92 | * |
||
| 93 | * @throws \Exception |
||
| 94 | */ |
||
| 95 | protected function trainBinary(array $samples, array $targets, array $labels) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * While finding best split point for a numerical valued column, |
||
| 150 | * DecisionStump looks for equally distanced values between minimum and maximum |
||
| 151 | * values in the column. Given <i>$count</i> value determines how many split |
||
| 152 | * points to be probed. The more split counts, the better performance but |
||
| 153 | * worse processing time (Default value is 10.0) |
||
| 154 | * |
||
| 155 | * @param float $count |
||
| 156 | */ |
||
| 157 | public function setNumericalSplitCount(float $count) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Determines best split point for the given column |
||
| 164 | * |
||
| 165 | * @param array $samples |
||
| 166 | * @param array $targets |
||
| 167 | * @param int $col |
||
| 168 | * |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | protected function getBestNumericalSplit(array $samples, array $targets, int $col) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param array $samples |
||
| 212 | * @param array $targets |
||
| 213 | * @param int $col |
||
| 214 | * |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | protected function getBestNominalSplit(array $samples, array $targets, int $col) : array |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Calculates the ratio of wrong predictions based on the new threshold |
||
| 242 | * value given as the parameter |
||
| 243 | * |
||
| 244 | * @param array $targets |
||
| 245 | * @param float $threshold |
||
| 246 | * @param string $operator |
||
| 247 | * @param array $values |
||
| 248 | * |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | protected function calculateErrorRate(array $targets, float $threshold, string $operator, array $values) : array |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns the probability of the sample of belonging to the given label |
||
| 292 | * |
||
| 293 | * Probability of a sample is calculated as the proportion of the label |
||
| 294 | * within the labels of the training samples in the decision node |
||
| 295 | * |
||
| 296 | * @param array $sample |
||
| 297 | * @param mixed $label |
||
| 298 | * |
||
| 299 | * @return float |
||
| 300 | */ |
||
| 301 | protected function predictProbability(array $sample, $label) : float |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param array $sample |
||
| 313 | * |
||
| 314 | * @return mixed |
||
| 315 | */ |
||
| 316 | protected function predictSampleBinary(array $sample) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | protected function resetBinary() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public function __toString() |
||
| 341 | } |
||
| 342 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.