1 | <?php |
||
14 | class Bagging implements Classifier |
||
15 | { |
||
16 | use Trainable, Predictable; |
||
17 | |||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | protected $numSamples; |
||
22 | |||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | protected $featureCount = 0; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | protected $numClassifier; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $classifier = DecisionTree::class; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $classifierOptions = ['depth' => 20]; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $classifiers = []; |
||
47 | |||
48 | /** |
||
49 | * @var float |
||
50 | */ |
||
51 | protected $subsetRatio = 0.7; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $targets = []; |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | private $samples = []; |
||
62 | |||
63 | /** |
||
64 | * Creates an ensemble classifier with given number of base classifiers |
||
65 | * Default number of base classifiers is 50. |
||
66 | * The more number of base classifiers, the better performance but at the cost of procesing time |
||
67 | */ |
||
68 | public function __construct(int $numClassifier = 50) |
||
72 | |||
73 | /** |
||
74 | * This method determines the ratio of samples used to create the 'bootstrap' subset, |
||
75 | * e.g., random samples drawn from the original dataset with replacement (allow repeats), |
||
76 | * to train each base classifier. |
||
77 | * |
||
78 | * @return $this |
||
79 | * |
||
80 | * @throws \Exception |
||
81 | */ |
||
82 | public function setSubsetRatio(float $ratio) |
||
92 | |||
93 | /** |
||
94 | * This method is used to set the base classifier. Default value is |
||
95 | * DecisionTree::class, but any class that implements the <i>Classifier</i> |
||
96 | * can be used. <br> |
||
97 | * While giving the parameters of the classifier, the values should be |
||
98 | * given in the order they are in the constructor of the classifier and parameter |
||
99 | * names are neglected. |
||
100 | * |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function setClassifer(string $classifier, array $classifierOptions = []) |
||
110 | |||
111 | public function train(array $samples, array $targets): void |
||
127 | |||
128 | protected function getRandomSubset(int $index): array |
||
142 | |||
143 | protected function initClassifiers(): array |
||
159 | |||
160 | protected function initSingleClassifier(Classifier $classifier): Classifier |
||
164 | |||
165 | /** |
||
166 | * @return mixed |
||
167 | */ |
||
168 | protected function predictSample(array $sample) |
||
182 | } |
||
183 |
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.