@@ -34,10 +34,10 @@ discard block |
||
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 | - print( SiteAnalyzer::transform($centroids, "html") ); |
|
40 | + print(SiteAnalyzer::transform($centroids, "html")); |
|
41 | 41 | //$centroids = self::initCentroids($nclusters, $ndimensions, function(){return rand(0,100)/100;}); |
42 | 42 | while (!$finished && $niter < $maxiter) { |
43 | 43 | // Assign each one of the points to one centroid |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | $bdist = INF; |
49 | 49 | for ($i = 0; $i < $nclusters; $i++) { |
50 | 50 | $ndist = self::eclideanDistance($data[$j], $centroids[$i]); |
51 | - if($bdist > $ndist) { |
|
51 | + if ($bdist > $ndist) { |
|
52 | 52 | $bdist = $ndist; |
53 | 53 | $best = $i; |
54 | 54 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | // Check change |
61 | 61 | $finished = true; |
62 | 62 | if (count($resp) > 0) { |
63 | - for ($j=0; $j < $npoints; $j++) { |
|
63 | + for ($j = 0; $j < $npoints; $j++) { |
|
64 | 64 | if ($resp[$j]!==$nresp[$j]) { |
65 | 65 | $finished = false; |
66 | 66 | break; |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | } |
72 | 72 | $resp = $nresp; |
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]); |
@@ -92,10 +92,10 @@ discard block |
||
92 | 92 | $resp = []; |
93 | 93 | $n = count($centroids); |
94 | 94 | $d = count($centroids[0]); |
95 | - for ($i=0;$i<$n;$i++) { |
|
95 | + for ($i = 0; $i < $n; $i++) { |
|
96 | 96 | $tmp = []; |
97 | - for ($j=0;$j<$d;$j++){ |
|
98 | - $tmp[] = $centroids[$i][$j]/$counts[$i]; |
|
97 | + for ($j = 0; $j < $d; $j++) { |
|
98 | + $tmp[] = $centroids[$i][$j] / $counts[$i]; |
|
99 | 99 | } |
100 | 100 | $resp[] = $tmp; |
101 | 101 | } |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | public static function eclideanDistance($p1, $p2) { |
125 | 125 | $len = count($p1); |
126 | 126 | $acum = 0; |
127 | - for($i=0; $i<$len; $i++) { |
|
127 | + for ($i = 0; $i < $len; $i++) { |
|
128 | 128 | $acum += ($p1[$i] - $p2[$i])**2; |
129 | 129 | } |
130 | 130 | return sqrt($acum); |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | */ |
83 | 83 | public static function getPDO($config, $options) |
84 | 84 | { |
85 | - if (array_key_exists("pdo",$options)) { |
|
85 | + if (array_key_exists("pdo", $options)) { |
|
86 | 86 | return $options["pdo"]; |
87 | 87 | } |
88 | 88 | return Persistence::getPDO($config); |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | } |
186 | 186 | |
187 | 187 | $result = $data; |
188 | - for ($i=1;$i<$level;$i++) { |
|
188 | + for ($i = 1; $i < $level; $i++) { |
|
189 | 189 | $tmp = Matrix::multiply($result, $data); |
190 | 190 | $result = Matrix::sum($result, $tmp); |
191 | 191 | } |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | } |
219 | 219 | |
220 | 220 | $result = $data; |
221 | - for ($i=1;$i<$level;$i++) { |
|
221 | + for ($i = 1; $i < $level; $i++) { |
|
222 | 222 | $tmp = Matrix::multiply($result, $data); |
223 | 223 | $result = Matrix::sum($result, $tmp); |
224 | 224 | } |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | $cdata = new CategoricalDataset($data); |
280 | 280 | $cdata->setEncodedFeatures([0, 1]); |
281 | 281 | $tdata = $cdata->encode(); |
282 | - print( SiteAnalyzer::transform($tdata, "html") ); |
|
282 | + print(SiteAnalyzer::transform($tdata, "html")); |
|
283 | 283 | $clusters = ML::kmeans($tdata, $nprofiles); |
284 | 284 | |
285 | 285 | return [$clusters]; |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | sort($array); |
60 | 60 | $this->encodedValues = []; |
61 | 61 | $this->sortedEncodedFeatures = $array; |
62 | - foreach($this->sortedEncodedFeatures as $col){ |
|
62 | + foreach ($this->sortedEncodedFeatures as $col) { |
|
63 | 63 | $vals = $this->getUniqueValues($col); |
64 | 64 | $this->encodedValues[] = $vals; |
65 | 65 | $this->featIndexMap[$col] = count($vals); |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | $tmp = Matrix::getColumn($this->data, $col); |
82 | 82 | $n = count($tmp); |
83 | 83 | $resp = []; |
84 | - for ($i=0; $i<$n; $i++) { |
|
84 | + for ($i = 0; $i < $n; $i++) { |
|
85 | 85 | if (!in_array($tmp[$i], $resp)) { |
86 | 86 | $resp[] = $tmp[$i]; |
87 | 87 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | { |
98 | 98 | $size = count($array); |
99 | 99 | $resp = []; |
100 | - for ($i=0;$i<$size;$i++) { |
|
100 | + for ($i = 0; $i < $size; $i++) { |
|
101 | 101 | $tmp = array_fill(0, $size, 0); |
102 | 102 | $tmp[$i] = 1; |
103 | 103 | $resp[$array[$i]] = $tmp; |
@@ -108,22 +108,22 @@ discard block |
||
108 | 108 | /* |
109 | 109 | * @param |
110 | 110 | */ |
111 | - public function encode(){ |
|
112 | - $transformer = []; |
|
111 | + public function encode() { |
|
112 | + $transformer = []; |
|
113 | 113 | $n = count($this->data); |
114 | 114 | $ndim = count($this->data[0]); |
115 | - for ($j=0; $j<$ndim; $j++) { |
|
116 | - $transformer[] = function($val){ return [$val]; }; |
|
115 | + for ($j = 0; $j < $ndim; $j++) { |
|
116 | + $transformer[] = function($val) { return [$val]; }; |
|
117 | 117 | } |
118 | - foreach($this->sortedEncodedFeatures as $col) { |
|
119 | - $transformer[$col] = function ($val) use ($col) { return $this->featEncode[$col][$val]; }; |
|
118 | + foreach ($this->sortedEncodedFeatures as $col) { |
|
119 | + $transformer[$col] = function($val) use ($col) { return $this->featEncode[$col][$val]; }; |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | $ndata = []; |
123 | - for ($i=0; $i<$n; $i++) { |
|
123 | + for ($i = 0; $i < $n; $i++) { |
|
124 | 124 | $npoint = []; |
125 | - for ($j=0; $j<$ndim; $j++) { |
|
126 | - $npoint = array_merge($npoint,$transformer[$j]($this->data[$i][$j])); |
|
125 | + for ($j = 0; $j < $ndim; $j++) { |
|
126 | + $npoint = array_merge($npoint, $transformer[$j]($this->data[$i][$j])); |
|
127 | 127 | } |
128 | 128 | $ndata[] = $npoint; |
129 | 129 | } |