| Conditions | 12 |
| Paths | 51 |
| Total Lines | 57 |
| Code Lines | 39 |
| 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, $categorical = []) |
||
|
|
|||
| 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 = []; |
||
| 37 | for ($i=0;$i<$nclusters;$i++){ |
||
| 38 | $centroids[] = $data[$i]; |
||
| 39 | } |
||
| 40 | //$centroids = self::initCentroids($nclusters, $ndimensions, function(){return rand(0,100)/100;}); |
||
| 41 | while (!$finished && $niter < $maxiter) { |
||
| 42 | // Assign each one of the points to one centroid |
||
| 43 | $niter++; |
||
| 44 | $nresp = []; |
||
| 45 | for ($j = 0; $j < $npoints; $j++) { |
||
| 46 | $best = -1; |
||
| 47 | $bdist = INF; |
||
| 48 | for ($i = 0; $i < $nclusters; $i++) { |
||
| 49 | $ndist = self::eclideanDistance($data[$j], $centroids[$i]); |
||
| 50 | if($bdist > $ndist) { |
||
| 51 | $bdist = $ndist; |
||
| 52 | $best = $i; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | $nresp[] = $best; |
||
| 56 | |||
| 57 | } |
||
| 58 | |||
| 59 | // Check change |
||
| 60 | |||
| 61 | $finished = true; |
||
| 62 | if (count($resp) > 0) { |
||
| 63 | for ($j=0; $j < $npoints; $j++) { |
||
| 64 | if ($resp[$j]!==$nresp[$j]) { |
||
| 65 | $finished = false; |
||
| 66 | break; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } else { |
||
| 70 | $finished = false; |
||
| 71 | } |
||
| 72 | $resp = $nresp; |
||
| 73 | var_dump($resp); |
||
| 74 | // Recalculate the centroids |
||
| 75 | $centroids = self::initCentroids($nclusters, $ndimensions, function(){return 0;}); |
||
| 76 | $counts = array_fill(0, $nclusters, 0); |
||
| 77 | for ($j = 0; $j < $npoints; $j++) { |
||
| 78 | $centroids[$resp[$j]] = Matrix::sumArray($centroids[$resp[$j]], $data[$j]); |
||
| 79 | $counts[$resp[$j]]++; |
||
| 80 | } |
||
| 81 | $centroids = self::normalizeCentroids($centroids, $counts); |
||
| 82 | } |
||
| 83 | return [$resp]; |
||
| 84 | } |
||
| 134 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.