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 array |
||
25 | */ |
||
26 | private $targets = []; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | protected $featureCount = 0; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | protected $numClassifier; |
||
37 | |||
38 | /** |
||
39 | * @var Classifier |
||
40 | */ |
||
41 | protected $classifier = DecisionTree::class; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $classifierOptions = ['depth' => 20]; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $classifiers; |
||
52 | |||
53 | /** |
||
54 | * @var float |
||
55 | */ |
||
56 | protected $subsetRatio = 0.5; |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | private $samples = []; |
||
62 | |||
63 | /** |
||
64 | * Creates an ensemble classifier with given number of base classifiers<br> |
||
65 | * Default number of base classifiers is 100. |
||
66 | * The more number of base classifiers, the better performance but at the cost of procesing time |
||
67 | * |
||
68 | * @param int $numClassifier |
||
69 | */ |
||
70 | public function __construct($numClassifier = 50) |
||
74 | |||
75 | /** |
||
76 | * This method determines the ratio of samples used to create the 'bootstrap' subset, |
||
77 | * e.g., random samples drawn from the original dataset with replacement (allow repeats), |
||
78 | * to train each base classifier. |
||
79 | * |
||
80 | * @param float $ratio |
||
81 | * @return $this |
||
82 | * @throws Exception |
||
83 | */ |
||
84 | 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 | * @param string $classifier |
||
102 | * @param array $classifierOptions |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function setClassifer(string $classifier, array $classifierOptions = []) |
||
111 | |||
112 | /** |
||
113 | * @param array $samples |
||
114 | * @param array $targets |
||
115 | */ |
||
116 | public function train(array $samples, array $targets) |
||
132 | |||
133 | /** |
||
134 | * @param int $index |
||
135 | * @return array |
||
136 | */ |
||
137 | protected function getRandomSubset($index) |
||
152 | |||
153 | /** |
||
154 | * @return array |
||
155 | */ |
||
156 | protected function initClassifiers() |
||
170 | |||
171 | /** |
||
172 | * @param Classifier $classifier |
||
173 | * @param int $index |
||
174 | * @return Classifier |
||
175 | */ |
||
176 | protected function initSingleClassifier($classifier, $index) |
||
180 | |||
181 | /** |
||
182 | * @param array $sample |
||
183 | * @return mixed |
||
184 | */ |
||
185 | protected function predictSample(array $sample) |
||
198 | } |
||
199 |
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..