| Conditions | 11 |
| Paths | 26 |
| Total Lines | 52 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 27 | public static function kmeans($data, $nclusters) |
||
| 28 | { |
||
| 29 | $resp = []; |
||
| 30 | $finished = false; |
||
| 31 | $niter = 0; |
||
| 32 | $maxiter = 100; |
||
| 33 | $npoints = count($data); |
||
| 34 | if ($npoints <= 0) throw new \Exception("Not enough data. "); |
||
| 35 | $ndimensions = count($data[0]); |
||
| 36 | $centroids = self::select_disjoint($data, $nclusters); |
||
| 37 | |||
| 38 | while (!$finished && $niter < $maxiter) { |
||
| 39 | // Assign each one of the points to one centroid |
||
| 40 | $niter++; |
||
| 41 | $nresp = []; |
||
| 42 | for ($j = 0; $j < $npoints; $j++) { |
||
| 43 | $best = -1; |
||
| 44 | $bdist = INF; |
||
| 45 | for ($i = 0; $i < $nclusters; $i++) { |
||
| 46 | $ndist = self::eclideanDistance($data[$j], $centroids[$i]); |
||
| 47 | if($bdist > $ndist) { |
||
| 48 | $bdist = $ndist; |
||
| 49 | $best = $i; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | $nresp[] = $best; |
||
| 53 | |||
| 54 | } |
||
| 55 | |||
| 56 | // Check change |
||
| 57 | $finished = true; |
||
| 58 | if (count($resp) > 0) { |
||
| 59 | for ($j=0; $j < $npoints; $j++) { |
||
| 60 | if ($resp[$j]!==$nresp[$j]) { |
||
| 61 | $finished = false; |
||
| 62 | break; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } else { |
||
| 66 | $finished = false; |
||
| 67 | } |
||
| 68 | $resp = $nresp; |
||
| 69 | // Recalculate the centroids |
||
| 70 | $centroids = self::initCentroids($nclusters, $ndimensions, function(){return 0;}); |
||
| 71 | $counts = array_fill(0, $nclusters, 0); |
||
| 72 | for ($j = 0; $j < $npoints; $j++) { |
||
| 73 | $centroids[$resp[$j]] = Matrix::sumArray($centroids[$resp[$j]], $data[$j]); |
||
| 74 | $counts[$resp[$j]]++; |
||
| 75 | } |
||
| 76 | $centroids = self::normalizeCentroids($centroids, $counts); |
||
| 77 | } |
||
| 78 | return $resp; |
||
| 79 | } |
||
| 170 |