Complex classes like DecisionTree 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 DecisionTree, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class DecisionTree implements Classifier |
||
| 13 | { |
||
| 14 | use Trainable, Predictable; |
||
| 15 | |||
| 16 | const CONTINUOS = 1; |
||
| 17 | const NOMINAL = 2; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private $samples = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private $columnTypes; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $labels = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | private $featureCount = 0; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var DecisionTreeLeaf |
||
| 41 | */ |
||
| 42 | private $tree = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | private $maxDepth; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | public $actualDepth = 0; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | private $numUsableFeatures = 0; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | private $featureImportances = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $columnNames = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param int $maxDepth |
||
| 72 | */ |
||
| 73 | public function __construct($maxDepth = 10) |
||
| 77 | /** |
||
| 78 | * @param array $samples |
||
| 79 | * @param array $targets |
||
| 80 | */ |
||
| 81 | public function train(array $samples, array $targets) |
||
| 106 | |||
| 107 | protected function getColumnTypes(array $samples) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param null|array $records |
||
| 120 | * @return DecisionTreeLeaf |
||
| 121 | */ |
||
| 122 | protected function getSplitLeaf($records, $depth = 0) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param array $records |
||
| 169 | * @return DecisionTreeLeaf[] |
||
| 170 | */ |
||
| 171 | protected function getBestSplit($records) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | protected function getSelectedFeatures() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string $baseValue |
||
| 225 | * @param array $colValues |
||
| 226 | * @param array $targets |
||
| 227 | */ |
||
| 228 | public function getGiniIndex($baseValue, $colValues, $targets) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param array $samples |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | protected function preprocess(array $samples) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param array $columnValues |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | protected function isCategoricalColumn(array $columnValues) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * This method is used to set number of columns to be used |
||
| 306 | * when deciding a split at an internal node of the tree. <br> |
||
| 307 | * If the value is given 0, then all features are used (default behaviour), |
||
| 308 | * otherwise the given value will be used as a maximum for number of columns |
||
| 309 | * randomly selected for each split operation. |
||
| 310 | * |
||
| 311 | * @param int $numFeatures |
||
| 312 | * @return $this |
||
| 313 | * @throws Exception |
||
| 314 | */ |
||
| 315 | public function setNumFeatures(int $numFeatures) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * A string array to represent columns. Useful when HTML output or |
||
| 328 | * column importances are desired to be inspected. |
||
| 329 | * |
||
| 330 | * @param array $names |
||
| 331 | * @return $this |
||
| 332 | */ |
||
| 333 | public function setColumnNames(array $names) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function getHtml() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * This will return an array including an importance value for |
||
| 354 | * each column in the given dataset. The importance values are |
||
| 355 | * normalized and their total makes 1.<br/> |
||
| 356 | * |
||
| 357 | * @param array $labels |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | public function getFeatureImportances() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Collects and returns an array of internal nodes that use the given |
||
| 393 | * column as a split criteron |
||
| 394 | * |
||
| 395 | * @param int $column |
||
| 396 | * @param DecisionTreeLeaf |
||
| 397 | * @param array $collected |
||
| 398 | * |
||
| 399 | * @return array |
||
| 400 | */ |
||
| 401 | protected function getSplitNodesByColumn($column, DecisionTreeLeaf $node) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @param array $sample |
||
| 427 | * @return mixed |
||
| 428 | */ |
||
| 429 | protected function predictSample(array $sample) |
||
| 445 | } |
||
| 446 |
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..