1 | <?php |
||
11 | class Perceptron implements Classifier |
||
12 | { |
||
13 | use Predictable; |
||
14 | |||
15 | /** |
||
16 | * The function whose result will be used to calculate the network error |
||
17 | * for each instance |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected static $errorFunction = 'outputClass'; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $samples = []; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $targets = []; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $labels = []; |
||
37 | |||
38 | /** |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $featureCount = 0; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $weights; |
||
47 | |||
48 | /** |
||
49 | * @var float |
||
50 | */ |
||
51 | protected $learningRate; |
||
52 | |||
53 | /** |
||
54 | * @var int |
||
55 | */ |
||
56 | protected $maxIterations; |
||
57 | |||
58 | /** |
||
59 | * Initalize a perceptron classifier with given learning rate and maximum |
||
60 | * number of iterations used while training the perceptron <br> |
||
61 | * |
||
62 | * Learning rate should be a float value between 0.0(exclusive) and 1.0(inclusive) <br> |
||
63 | * Maximum number of iterations can be an integer value greater than 0 |
||
64 | * @param int $learningRate |
||
65 | * @param int $maxIterations |
||
66 | */ |
||
67 | public function __construct(float $learningRate = 0.001, int $maxIterations = 1000) |
||
80 | |||
81 | /** |
||
82 | * @param array $samples |
||
83 | * @param array $targets |
||
84 | */ |
||
85 | public function train(array $samples, array $targets) |
||
110 | |||
111 | /** |
||
112 | * Adapts the weights with respect to given samples and targets |
||
113 | * by use of perceptron learning rule |
||
114 | */ |
||
115 | protected function runTraining() |
||
132 | |||
133 | /** |
||
134 | * Calculates net output of the network as a float value for the given input |
||
135 | * |
||
136 | * @param array $sample |
||
137 | * @return int |
||
138 | */ |
||
139 | protected function output(array $sample) |
||
152 | |||
153 | /** |
||
154 | * Returns the class value (either -1 or 1) for the given input |
||
155 | * |
||
156 | * @param array $sample |
||
157 | * @return int |
||
158 | */ |
||
159 | protected function outputClass(array $sample) |
||
163 | |||
164 | /** |
||
165 | * @param array $sample |
||
166 | * @return mixed |
||
167 | */ |
||
168 | protected function predictSample(array $sample) |
||
174 | } |
||
175 |