| @@ 157-170 (lines=14) @@ | ||
| 154 | * 2) k(x) mode: only x is given |
|
| 155 | */ |
|
| 156 | switch ($this->kernel) { |
|
| 157 | case self::KERNEL_RBF: |
|
| 158 | // k(x,y)=exp(-γ.|x-y|) where |..| is Euclidean distance |
|
| 159 | $dist = new Euclidean(); |
|
| 160 | return function ($x, $y = null) use ($dist) { |
|
| 161 | if ($y === null) { |
|
| 162 | foreach ($x as $i => $element) { |
|
| 163 | $x[$i] = exp(-$this->gamma * $element); |
|
| 164 | } |
|
| 165 | ||
| 166 | return $x; |
|
| 167 | } |
|
| 168 | ||
| 169 | return exp(-$this->gamma * $dist->distance($x, $y)); |
|
| 170 | }; |
|
| 171 | ||
| 172 | case self::KERNEL_SIGMOID: |
|
| 173 | // k(x,y)=tanh(γ.xT.y+c0) where c0=0 |
|
| @@ 190-203 (lines=14) @@ | ||
| 187 | return tanh($this->gamma * $res->toScalar()); |
|
| 188 | }; |
|
| 189 | ||
| 190 | case self::KERNEL_LAPLACIAN: |
|
| 191 | // k(x,y)=exp(-γ.|x-y|) where |..| is Manhattan distance |
|
| 192 | $dist = new Manhattan(); |
|
| 193 | return function ($x, $y = null) use ($dist) { |
|
| 194 | if ($y === null) { |
|
| 195 | foreach ($x as $i => $element) { |
|
| 196 | $x[$i] = exp(-$this->gamma * $element); |
|
| 197 | } |
|
| 198 | ||
| 199 | return $x; |
|
| 200 | } |
|
| 201 | ||
| 202 | return exp(-$this->gamma * $dist->distance($x, $y)); |
|
| 203 | }; |
|
| 204 | } |
|
| 205 | } |
|
| 206 | ||