Passed
Push — master ( f45968...5c8cf9 )
by Ruben
02:03
created
src/ML.php 1 patch
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@  discard block
 block discarded – undo
34 34
         if ($npoints <= 0) throw new \Exception("Not enough data. ");    
35 35
         $ndimensions = count($data[0]);
36 36
         $centroids = [];
37
-        for ($i=0;$i<$nclusters;$i++){
37
+        for ($i = 0; $i < $nclusters; $i++) {
38 38
             $centroids[] = $data[$i];
39 39
         }
40 40
         //$centroids = self::initCentroids($nclusters, $ndimensions, function(){return rand(0,100)/100;});   
@@ -47,7 +47,7 @@  discard block
 block discarded – undo
47 47
                 $bdist = INF;
48 48
                 for ($i = 0; $i < $nclusters; $i++) {
49 49
                     $ndist = self::eclideanDistance($data[$j], $centroids[$i]);
50
-                    if($bdist > $ndist) {
50
+                    if ($bdist > $ndist) {
51 51
                         $bdist = $ndist;
52 52
                         $best = $i;
53 53
                     }            
@@ -59,7 +59,7 @@  discard block
 block discarded – undo
59 59
             // Check change 
60 60
             $finished = true;
61 61
             if (count($resp) > 0) {
62
-                for ($j=0; $j < $npoints; $j++) {        
62
+                for ($j = 0; $j < $npoints; $j++) {        
63 63
                     if ($resp[$j]!==$nresp[$j]) {
64 64
                         $finished = false;
65 65
                         break;
@@ -71,7 +71,7 @@  discard block
 block discarded – undo
71 71
             $resp = $nresp;
72 72
             
73 73
             // Recalculate the centroids
74
-            $centroids = self::initCentroids($nclusters, $ndimensions, function(){return 0;});
74
+            $centroids = self::initCentroids($nclusters, $ndimensions, function() {return 0; });
75 75
             $counts = array_fill(0, $nclusters, 0);
76 76
             for ($j = 0; $j < $npoints; $j++) {    
77 77
                 $centroids[$resp[$j]] = Matrix::sumArray($centroids[$resp[$j]], $data[$j]);
@@ -91,10 +91,10 @@  discard block
 block discarded – undo
91 91
         $resp = [];
92 92
         $n = count($centroids);
93 93
         $d = count($centroids[0]);
94
-        for ($i=0;$i<$n;$i++) {
94
+        for ($i = 0; $i < $n; $i++) {
95 95
             $tmp = [];
96
-            for ($j=0;$j<$d;$j++){
97
-                $tmp[] = $centroids[$i][$j]/$counts[$i];
96
+            for ($j = 0; $j < $d; $j++) {
97
+                $tmp[] = $centroids[$i][$j] / $counts[$i];
98 98
             }
99 99
             $resp[] = $tmp;
100 100
         }
@@ -123,7 +123,7 @@  discard block
 block discarded – undo
123 123
     public static function eclideanDistance($p1, $p2) {
124 124
        $len = count($p1);
125 125
        $acum = 0;
126
-       for($i=0; $i<$len; $i++) {
126
+       for ($i = 0; $i < $len; $i++) {
127 127
            $acum += ($p1[$i] - $p2[$i])**2;
128 128
        }
129 129
        return sqrt($acum);
Please login to merge, or discard this patch.