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:
Complex classes like DecisionStump often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DecisionStump, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class DecisionStump extends WeightedClassifier |
||
| 14 | { |
||
| 15 | use Trainable, 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 | * Sample weights : If used the optimization on the decision value |
||
| 31 | * will take these weights into account. If not given, all samples |
||
| 32 | * will be weighed with the same value of 1 |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $weights = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Lowest error rate obtained while training/optimizing the model |
||
| 40 | * |
||
| 41 | * @var float |
||
| 42 | */ |
||
| 43 | protected $trainingErrorRate; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | protected $column; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var mixed |
||
| 52 | */ |
||
| 53 | protected $value; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $operator; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $columnTypes; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | protected $featureCount; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var float |
||
| 72 | */ |
||
| 73 | protected $numSplitCount = 100.0; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Distribution of samples in the leaves |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $prob; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * A DecisionStump classifier is a one-level deep DecisionTree. It is generally |
||
| 84 | * used with ensemble algorithms as in the weak classifier role. <br> |
||
| 85 | * |
||
| 86 | * If columnIndex is given, then the stump tries to produce a decision node |
||
| 87 | * on this column, otherwise in cases given the value of -1, the stump itself |
||
| 88 | * decides which column to take for the decision (Default DecisionTree behaviour) |
||
| 89 | * |
||
| 90 | * @param int $columnIndex |
||
| 91 | */ |
||
| 92 | public function __construct(int $columnIndex = self::AUTO_SELECT) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param array $samples |
||
| 99 | * @param array $targets |
||
| 100 | */ |
||
| 101 | protected function trainBinary(array $samples, array $targets) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * While finding best split point for a numerical valued column, |
||
| 158 | * DecisionStump looks for equally distanced values between minimum and maximum |
||
| 159 | * values in the column. Given <i>$count</i> value determines how many split |
||
| 160 | * points to be probed. The more split counts, the better performance but |
||
| 161 | * worse processing time (Default value is 10.0) |
||
| 162 | * |
||
| 163 | * @param float $count |
||
| 164 | */ |
||
| 165 | public function setNumericalSplitCount(float $count) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Determines best split point for the given column |
||
| 172 | * |
||
| 173 | * @param int $col |
||
| 174 | * |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | protected function getBestNumericalSplit(int $col) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * |
||
| 218 | * @param int $col |
||
| 219 | * |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | protected function getBestNominalSplit(int $col) |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * |
||
| 248 | * @param type $leftValue |
||
| 249 | * @param type $operator |
||
| 250 | * @param type $rightValue |
||
| 251 | * |
||
| 252 | * @return boolean |
||
| 253 | */ |
||
| 254 | protected function evaluate($leftValue, $operator, $rightValue) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Calculates the ratio of wrong predictions based on the new threshold |
||
| 271 | * value given as the parameter |
||
| 272 | * |
||
| 273 | * @param float $threshold |
||
| 274 | * @param string $operator |
||
| 275 | * @param array $values |
||
| 276 | * |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | protected function calculateErrorRate(float $threshold, string $operator, array $values) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns the probability of the sample of belonging to the given label |
||
| 320 | * |
||
| 321 | * Probability of a sample is calculated as the proportion of the label |
||
| 322 | * within the labels of the training samples in the decision node |
||
| 323 | * |
||
| 324 | * @param array $sample |
||
| 325 | * @param mixed $label |
||
| 326 | * |
||
| 327 | * @return float |
||
| 328 | */ |
||
| 329 | protected function predictProbability(array $sample, $label) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param array $sample |
||
| 341 | * |
||
| 342 | * @return mixed |
||
| 343 | */ |
||
| 344 | protected function predictSampleBinary(array $sample) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public function __toString() |
||
| 362 | } |
||
| 363 |
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.