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 |
||
| 12 | class DecisionStump extends WeightedClassifier |
||
| 13 | { |
||
| 14 | use Predictable, OneVsRest; |
||
| 15 | |||
| 16 | const AUTO_SELECT = -1; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | protected $givenColumnIndex; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $binaryLabels; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Lowest error rate obtained while training/optimizing the model |
||
| 30 | * |
||
| 31 | * @var float |
||
| 32 | */ |
||
| 33 | protected $trainingErrorRate; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | protected $column; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var mixed |
||
| 42 | */ |
||
| 43 | protected $value; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $operator; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $columnTypes; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | protected $featureCount; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var float |
||
| 62 | */ |
||
| 63 | protected $numSplitCount = 100.0; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Distribution of samples in the leaves |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $prob; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * A DecisionStump classifier is a one-level deep DecisionTree. It is generally |
||
| 74 | * used with ensemble algorithms as in the weak classifier role. <br> |
||
| 75 | * |
||
| 76 | * If columnIndex is given, then the stump tries to produce a decision node |
||
| 77 | * on this column, otherwise in cases given the value of -1, the stump itself |
||
| 78 | * decides which column to take for the decision (Default DecisionTree behaviour) |
||
| 79 | * |
||
| 80 | * @param int $columnIndex |
||
| 81 | */ |
||
| 82 | public function __construct(int $columnIndex = self::AUTO_SELECT) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param array $samples |
||
| 89 | * @param array $targets |
||
| 90 | * @throws \Exception |
||
| 91 | */ |
||
| 92 | protected function trainBinary(array $samples, array $targets, array $labels) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * While finding best split point for a numerical valued column, |
||
| 147 | * DecisionStump looks for equally distanced values between minimum and maximum |
||
| 148 | * values in the column. Given <i>$count</i> value determines how many split |
||
| 149 | * points to be probed. The more split counts, the better performance but |
||
| 150 | * worse processing time (Default value is 10.0) |
||
| 151 | * |
||
| 152 | * @param float $count |
||
| 153 | */ |
||
| 154 | public function setNumericalSplitCount(float $count) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Determines best split point for the given column |
||
| 161 | * |
||
| 162 | * @param array $samples |
||
| 163 | * @param array $targets |
||
| 164 | * @param int $col |
||
| 165 | * |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | protected function getBestNumericalSplit(array $samples, array $targets, int $col) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param array $samples |
||
| 209 | * @param array $targets |
||
| 210 | * @param int $col |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | protected function getBestNominalSplit(array $samples, array $targets, int $col) : array |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * |
||
| 240 | * @param type $leftValue |
||
| 241 | * @param type $operator |
||
| 242 | * @param type $rightValue |
||
| 243 | * |
||
| 244 | * @return boolean |
||
| 245 | */ |
||
| 246 | protected function evaluate($leftValue, $operator, $rightValue) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Calculates the ratio of wrong predictions based on the new threshold |
||
| 263 | * value given as the parameter |
||
| 264 | * |
||
| 265 | * @param array $targets |
||
| 266 | * @param float $threshold |
||
| 267 | * @param string $operator |
||
| 268 | * @param array $values |
||
| 269 | * |
||
| 270 | * @return array |
||
| 271 | */ |
||
| 272 | protected function calculateErrorRate(array $targets, float $threshold, string $operator, array $values) : array |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Returns the probability of the sample of belonging to the given label |
||
| 313 | * |
||
| 314 | * Probability of a sample is calculated as the proportion of the label |
||
| 315 | * within the labels of the training samples in the decision node |
||
| 316 | * |
||
| 317 | * @param array $sample |
||
| 318 | * @param mixed $label |
||
| 319 | * |
||
| 320 | * @return float |
||
| 321 | */ |
||
| 322 | protected function predictProbability(array $sample, $label) : float |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param array $sample |
||
| 334 | * |
||
| 335 | * @return mixed |
||
| 336 | */ |
||
| 337 | protected function predictSampleBinary(array $sample) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function __toString() |
||
| 355 | } |
||
| 356 |
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.