@@ -21,11 +21,11 @@ discard block |
||
21 | 21 | /* |
22 | 22 | * @param |
23 | 23 | */ |
24 | - function setEncodedFeatures($array){ |
|
24 | + function setEncodedFeatures($array) { |
|
25 | 25 | $array = sort($array); |
26 | 26 | $this->encodedValues = []; |
27 | 27 | $this->sortedEncodedFeatures = $array; |
28 | - foreach($this->sortedEncodedFeatures as $feat){ |
|
28 | + foreach ($this->sortedEncodedFeatures as $feat) { |
|
29 | 29 | $vals = getUniqueValues($this->sortedEncodedFeatures, $feat); |
30 | 30 | $this->encodedValues[] = $vals; |
31 | 31 | $this->encodedFeatMapSize[$feat] = count($vals); |
@@ -35,26 +35,26 @@ discard block |
||
35 | 35 | /* |
36 | 36 | * @param |
37 | 37 | */ |
38 | - function getUniqueValues($feat){ |
|
38 | + function getUniqueValues($feat) { |
|
39 | 39 | |
40 | 40 | } |
41 | 41 | |
42 | 42 | /* |
43 | 43 | * @param |
44 | 44 | */ |
45 | - function encode(){ |
|
46 | - $transformer = []; |
|
45 | + function encode() { |
|
46 | + $transformer = []; |
|
47 | 47 | $ndata = []; |
48 | - for ($j=0; $j<$ndim; $j++) { |
|
49 | - $transformer[] = function($val){ return $val; }; |
|
48 | + for ($j = 0; $j < $ndim; $j++) { |
|
49 | + $transformer[] = function($val) { return $val; }; |
|
50 | 50 | } |
51 | - foreach( as $key => $val) { |
|
51 | + foreach (as $key => $val) { |
|
52 | 52 | $transformer[$key] = function($val) { return $this->featEncode[$key][$val]; }; |
53 | 53 | } |
54 | 54 | $ndata = []; |
55 | - for ($i=0; $i<$npoints; $i++) { |
|
55 | + for ($i = 0; $i < $npoints; $i++) { |
|
56 | 56 | $npoint = []; |
57 | - for ($j=0; $j<$ndim; $j++) { |
|
57 | + for ($j = 0; $j < $ndim; $j++) { |
|
58 | 58 | $npoint[] = $transformer[$j]($data[$i][$j]); |
59 | 59 | } |
60 | 60 | $ndata[] = $npoint; |
@@ -65,12 +65,12 @@ discard block |
||
65 | 65 | /* |
66 | 66 | * @param |
67 | 67 | */ |
68 | - function decode($ndata){ |
|
68 | + function decode($ndata) { |
|
69 | 69 | $resp = []; |
70 | - for ($i=0; $i<$npoints; $i++) { |
|
70 | + for ($i = 0; $i < $npoints; $i++) { |
|
71 | 71 | //$point = array_fill(0, $nexpdim; null); |
72 | 72 | $point = array($ndata[$i]); |
73 | - foreach ( as $key => $val) { |
|
73 | + foreach (as $key => $val) { |
|
74 | 74 | $this->featDecode[$key]($point); |
75 | 75 | } |
76 | 76 | $point = $this->removeNulls($point); |
@@ -122,12 +122,12 @@ |
||
122 | 122 | * @param |
123 | 123 | */ |
124 | 124 | public static function eclideanDistance($p1, $p2) { |
125 | - $len = count($p1); |
|
126 | - $acum = 0; |
|
127 | - for($i=0; $i<$len; $i++) { |
|
128 | - $acum += ($p1[$i] - $p2[$i])**2; |
|
129 | - } |
|
130 | - return sqrt($acum); |
|
125 | + $len = count($p1); |
|
126 | + $acum = 0; |
|
127 | + for($i=0; $i<$len; $i++) { |
|
128 | + $acum += ($p1[$i] - $p2[$i])**2; |
|
129 | + } |
|
130 | + return sqrt($acum); |
|
131 | 131 | } |
132 | 132 | |
133 | 133 | } |
@@ -34,7 +34,7 @@ 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 | 40 | //$centroids = self::initCentroids($nclusters, $ndimensions, function(){return rand(0,100)/100;}); |
@@ -47,7 +47,7 @@ discard block |
||
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 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | |
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; |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | $resp = $nresp; |
73 | 73 | var_dump($resp); |
74 | 74 | // Recalculate the centroids |
75 | - $centroids = self::initCentroids($nclusters, $ndimensions, function(){return 0;}); |
|
75 | + $centroids = self::initCentroids($nclusters, $ndimensions, function() {return 0; }); |
|
76 | 76 | $counts = array_fill(0, $nclusters, 0); |
77 | 77 | for ($j = 0; $j < $npoints; $j++) { |
78 | 78 | $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); |
@@ -31,7 +31,9 @@ |
||
31 | 31 | $niter = 0; |
32 | 32 | $maxiter = 100; |
33 | 33 | $npoints = count($data); |
34 | - if ($npoints <= 0) throw new \Exception("Not enough data. "); |
|
34 | + if ($npoints <= 0) { |
|
35 | + throw new \Exception("Not enough data. "); |
|
36 | + } |
|
35 | 37 | $ndimensions = count($data[0]); |
36 | 38 | $centroids = []; |
37 | 39 | for ($i=0;$i<$nclusters;$i++){ |
@@ -24,13 +24,13 @@ discard block |
||
24 | 24 | public static function submatrix($matrix, $columns) |
25 | 25 | { |
26 | 26 | $n = count($matrix); |
27 | - if ($n == 0) return []; |
|
27 | + if ($n==0) return []; |
|
28 | 28 | |
29 | 29 | $m = count($matrix[0]); |
30 | - if ($m == 0) return $matrix; |
|
30 | + if ($m==0) return $matrix; |
|
31 | 31 | |
32 | 32 | $result = []; |
33 | - for ($i=0; $i<$n; $i++) { |
|
33 | + for ($i = 0; $i < $n; $i++) { |
|
34 | 34 | $tmp = []; |
35 | 35 | foreach ($columns as $col) { |
36 | 36 | $tmp[] = $matrix[$i][$col]; |
@@ -47,17 +47,17 @@ discard block |
||
47 | 47 | public static function toSquareMatrix($matrix, $ncol, $mcol, $vcol, $sparse = False) |
48 | 48 | { |
49 | 49 | $n = count($matrix); |
50 | - if ($n == 0) return []; |
|
50 | + if ($n==0) return []; |
|
51 | 51 | |
52 | 52 | $labels = []; |
53 | 53 | $id = 0; |
54 | 54 | $data = []; |
55 | - for ($i=0; $i<$n; $i++) { |
|
56 | - if (!array_key_exists($matrix[$i][$ncol],$labels)) { |
|
55 | + for ($i = 0; $i < $n; $i++) { |
|
56 | + if (!array_key_exists($matrix[$i][$ncol], $labels)) { |
|
57 | 57 | $labels[$matrix[$i][$ncol]] = $id++; |
58 | 58 | } |
59 | 59 | $nid = $labels[$matrix[$i][$ncol]]; |
60 | - if (!array_key_exists($matrix[$i][$mcol],$labels)) { |
|
60 | + if (!array_key_exists($matrix[$i][$mcol], $labels)) { |
|
61 | 61 | $labels[$matrix[$i][$mcol]] = $id++; |
62 | 62 | } |
63 | 63 | $mid = $labels[$matrix[$i][$mcol]]; |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | if ($sparse) return ["data" => $data, "labels" => $labels]; |
68 | 68 | $nlab = count($labels); |
69 | 69 | $result = []; |
70 | - for ($i=0; $i<$nlab; $i++) { |
|
70 | + for ($i = 0; $i < $nlab; $i++) { |
|
71 | 71 | $tmp = array_fill(0, $nlab, 0); |
72 | 72 | $result[] = $tmp; |
73 | 73 | } |
@@ -84,11 +84,11 @@ discard block |
||
84 | 84 | { |
85 | 85 | $result = []; |
86 | 86 | $n = count($matrix1); |
87 | - if ($n != count($matrix2)) throw new \Exception("Summing non compatible matrices. "); |
|
87 | + if ($n!=count($matrix2)) throw new \Exception("Summing non compatible matrices. "); |
|
88 | 88 | $m = count($matrix1[0]); |
89 | - for ($i=0;$i<$n;$i++) { |
|
89 | + for ($i = 0; $i < $n; $i++) { |
|
90 | 90 | $tmp = []; |
91 | - for($j=0;$j<$m;$j++) { |
|
91 | + for ($j = 0; $j < $m; $j++) { |
|
92 | 92 | $tmp[] = $matrix1[$i][$j] + $matrix2[$i][$j]; |
93 | 93 | } |
94 | 94 | $result[] = $tmp; |
@@ -107,16 +107,16 @@ discard block |
||
107 | 107 | $n2 = count($matrix2); |
108 | 108 | $m2 = count($matrix2[0]); |
109 | 109 | |
110 | - if ($n2 !== $m1) throw new \Exception("Incompatible matrices: matrix1.columns != matrix2.rows"); |
|
110 | + if ($n2!==$m1) throw new \Exception("Incompatible matrices: matrix1.columns != matrix2.rows"); |
|
111 | 111 | |
112 | 112 | $result = []; |
113 | - for ($i=0; $i<$n1; $i++) { |
|
113 | + for ($i = 0; $i < $n1; $i++) { |
|
114 | 114 | $result[] = array_fill(0, $m2, 0); |
115 | 115 | } |
116 | - for ($i=0; $i<$n1; $i++) { |
|
117 | - for ($j=0; $j<$m2; $j++) { |
|
118 | - for ($k=0; $k<$n2; $k++) { |
|
119 | - $result[$i][$j] += $matrix1[$i][$k]*$matrix2[$k][$j]; |
|
116 | + for ($i = 0; $i < $n1; $i++) { |
|
117 | + for ($j = 0; $j < $m2; $j++) { |
|
118 | + for ($k = 0; $k < $n2; $k++) { |
|
119 | + $result[$i][$j] += $matrix1[$i][$k] * $matrix2[$k][$j]; |
|
120 | 120 | } |
121 | 121 | } |
122 | 122 | } |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | foreach ($matrix as $row) { |
132 | 132 | $tmp = []; |
133 | 133 | foreach ($row as $dat) { |
134 | - $tmp[] = $dat == 0 ? 0 : 1; |
|
134 | + $tmp[] = $dat==0 ? 0 : 1; |
|
135 | 135 | } |
136 | 136 | $resp[] = $tmp; |
137 | 137 | } |
@@ -154,10 +154,10 @@ discard block |
||
154 | 154 | */ |
155 | 155 | public static function power($matrix, $n) |
156 | 156 | { |
157 | - if($n <= 1) return $matrix; |
|
157 | + if ($n <= 1) return $matrix; |
|
158 | 158 | $resp = self::multiply($matrix, $matrix); |
159 | - if($n == 2) return $resp; |
|
160 | - for($i=2; $i<$n; $i++) { |
|
159 | + if ($n==2) return $resp; |
|
160 | + for ($i = 2; $i < $n; $i++) { |
|
161 | 161 | $resp = self::multiply($resp, $matrix); |
162 | 162 | } |
163 | 163 | return $resp; |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | public static function sumArray($array1, $array2) { |
170 | 170 | $resp = []; |
171 | 171 | $n = min([count($array1), count($array2)]); |
172 | - for ($i=0;$i<$n;$i++){ |
|
172 | + for ($i = 0; $i < $n; $i++) { |
|
173 | 173 | $resp[] = $array1[$i] + $array2[$i]; |
174 | 174 | } |
175 | 175 | return $resp; |