@@ -194,12 +194,12 @@ public function train(Dataset $dataset) : void
194
194
]));
195
195
}
196
196
197
- $ this -> centroids = $ previous = $ dataset ->samples ();
197
+ $ centroids = $ previous = $ dataset ->samples ();
198
198
199
199
$ this ->steps = [];
200
200
201
201
for ($ epoch = 1 ; $ epoch <= $ this ->epochs ; $ epoch ++) {
202
- foreach ($ this -> centroids as $ i => &$ centroid ) {
202
+ foreach ($ centroids as $ i => &$ centroid ) {
203
203
foreach ($ dataset as $ sample ) {
204
204
$ distance = $ this ->kernel ->compute ($ sample , $ centroid );
205
205
@@ -214,15 +214,15 @@ public function train(Dataset $dataset) : void
214
214
}
215
215
}
216
216
217
- foreach ($ this -> centroids as $ j => $ neighbor ) {
217
+ foreach ($ centroids as $ j => $ neighbor ) {
218
218
if ($ i === $ j ) {
219
219
continue 1 ;
220
220
}
221
221
222
222
$ distance = $ this ->kernel ->compute ($ centroid , $ neighbor );
223
223
224
224
if ($ distance < $ this ->radius ) {
225
- unset($ this -> centroids [$ j ]);
225
+ unset($ centroids [$ j ]);
226
226
}
227
227
}
228
228
}
@@ -243,10 +243,10 @@ public function train(Dataset $dataset) : void
243
243
break 1 ;
244
244
}
245
245
246
- $ previous = $ this -> centroids ;
246
+ $ previous = $ centroids ;
247
247
}
248
248
249
- $ this ->centroids = array_values ($ this -> centroids );
249
+ $ this ->centroids = array_values ($ centroids );
250
250
251
251
if ($ this ->logger ) {
252
252
$ this ->logger ->info ('Training complete ' );
@@ -296,7 +296,7 @@ protected function assign(array $sample) : int
296
296
}
297
297
298
298
/**
299
- * Calculate the magnitude (l1) of a centroid shift from the previous epoch.
299
+ * Calculate the magnitude (l1) of centroid shift from the previous epoch.
300
300
*
301
301
* @param array $previous
302
302
* @return float
@@ -306,10 +306,10 @@ protected function centroidShift(array $previous) : float
306
306
$ shift = 0. ;
307
307
308
308
foreach ($ this ->centroids as $ cluster => $ centroid ) {
309
- $ prevCluster = $ previous [$ cluster ];
309
+ $ prevCentroid = $ previous [$ cluster ];
310
310
311
311
foreach ($ centroid as $ column => $ mean ) {
312
- $ shift += abs ($ prevCluster [$ column ] - $ mean );
312
+ $ shift += abs ($ prevCentroid [$ column ] - $ mean );
313
313
}
314
314
}
315
315
0 commit comments