|
4 | 4 | class KNNBase { |
5 | 5 | /** |
6 | 6 | * @param {number} [k=5] Number of neighborhoods |
7 | | - * @param {'euclid' | 'manhattan' | 'chebyshev' | 'minkowski'} [metric=euclid] Metric name |
| 7 | + * @param {'euclid' | 'manhattan' | 'chebyshev' | 'minkowski' | function (number[], number[]): number} [metric=euclid] Metric name |
8 | 8 | */ |
9 | 9 | constructor(k = 5, metric = 'euclid') { |
10 | 10 | this._p = [] |
11 | 11 | this._c = [] |
12 | 12 | this._k = k |
13 | 13 |
|
14 | 14 | this._metric = metric |
15 | | - switch (this._metric) { |
16 | | - case 'euclid': |
17 | | - this._d = (a, b) => Math.sqrt(a.reduce((s, v, i) => s + (v - b[i]) ** 2, 0)) |
18 | | - break |
19 | | - case 'manhattan': |
20 | | - this._d = (a, b) => a.reduce((s, v, i) => s + Math.abs(v - b[i]), 0) |
21 | | - break |
22 | | - case 'chebyshev': |
23 | | - this._d = (a, b) => Math.max(...a.map((v, i) => Math.abs(v - b[i]))) |
24 | | - break |
25 | | - case 'minkowski': |
26 | | - this._dp = 2 |
27 | | - this._d = (a, b) => |
28 | | - Math.pow( |
29 | | - a.reduce((s, v, i) => s + (v - b[i]) ** this._dp, 0), |
30 | | - 1 / this._dp |
31 | | - ) |
32 | | - break |
| 15 | + if (typeof this._metric === 'function') { |
| 16 | + this._d = this._metric |
| 17 | + } else { |
| 18 | + switch (this._metric) { |
| 19 | + case 'euclid': |
| 20 | + this._d = (a, b) => Math.sqrt(a.reduce((s, v, i) => s + (v - b[i]) ** 2, 0)) |
| 21 | + break |
| 22 | + case 'manhattan': |
| 23 | + this._d = (a, b) => a.reduce((s, v, i) => s + Math.abs(v - b[i]), 0) |
| 24 | + break |
| 25 | + case 'chebyshev': |
| 26 | + this._d = (a, b) => Math.max(...a.map((v, i) => Math.abs(v - b[i]))) |
| 27 | + break |
| 28 | + case 'minkowski': |
| 29 | + this._dp = 2 |
| 30 | + this._d = (a, b) => |
| 31 | + Math.pow( |
| 32 | + a.reduce((s, v, i) => s + (v - b[i]) ** this._dp, 0), |
| 33 | + 1 / this._dp |
| 34 | + ) |
| 35 | + break |
| 36 | + } |
33 | 37 | } |
34 | 38 | } |
35 | 39 |
|
@@ -72,7 +76,7 @@ class KNNBase { |
72 | 76 | export class KNN extends KNNBase { |
73 | 77 | /** |
74 | 78 | * @param {number} [k=5] Number of neighborhoods |
75 | | - * @param {'euclid' | 'manhattan' | 'chebyshev' | 'minkowski'} [metric=euclid] Metric name |
| 79 | + * @param {'euclid' | 'manhattan' | 'chebyshev' | 'minkowski' | function (number[], number[]): number} [metric=euclid] Metric name |
76 | 80 | */ |
77 | 81 | constructor(k = 5, metric = 'euclid') { |
78 | 82 | super(k, metric) |
@@ -144,7 +148,7 @@ export class KNN extends KNNBase { |
144 | 148 | export class KNNRegression extends KNNBase { |
145 | 149 | /** |
146 | 150 | * @param {number} [k=5] Number of neighborhoods |
147 | | - * @param {'euclid' | 'manhattan' | 'chebyshev' | 'minkowski'} [metric=euclid] Metric name |
| 151 | + * @param {'euclid' | 'manhattan' | 'chebyshev' | 'minkowski' | function (number[], number[]): number} [metric=euclid] Metric name |
148 | 152 | */ |
149 | 153 | constructor(k = 5, metric = 'euclid') { |
150 | 154 | super(k, metric) |
@@ -192,7 +196,7 @@ export class KNNRegression extends KNNBase { |
192 | 196 | export class KNNAnomaly extends KNNBase { |
193 | 197 | /** |
194 | 198 | * @param {number} [k=5] Number of neighborhoods |
195 | | - * @param {'euclid' | 'manhattan' | 'chebyshev' | 'minkowski'} [metric=euclid] Metric name |
| 199 | + * @param {'euclid' | 'manhattan' | 'chebyshev' | 'minkowski' | function (number[], number[]): number} [metric=euclid] Metric name |
196 | 200 | */ |
197 | 201 | constructor(k = 5, metric = 'euclid') { |
198 | 202 | super(k, metric) |
@@ -239,7 +243,7 @@ export class KNNDensityEstimation extends KNNBase { |
239 | 243 | // https://home.hiroshima-u.ac.jp/tkurita/lecture/prnn/node12.html |
240 | 244 | /** |
241 | 245 | * @param {number} [k=5] Number of neighborhoods |
242 | | - * @param {'euclid' | 'manhattan' | 'chebyshev' | 'minkowski'} [metric=euclid] Metric name |
| 246 | + * @param {'euclid' | 'manhattan' | 'chebyshev' | 'minkowski' | function (number[], number[]): number} [metric=euclid] Metric name |
243 | 247 | */ |
244 | 248 | constructor(k = 5, metric = 'euclid') { |
245 | 249 | super(k, metric) |
@@ -306,7 +310,7 @@ export class SemiSupervisedKNN extends KNNBase { |
306 | 310 | // https://products.sint.co.jp/aisia/blog/vol1-20 |
307 | 311 | /** |
308 | 312 | * @param {number} [k=5] Number of neighborhoods |
309 | | - * @param {'euclid' | 'manhattan' | 'chebyshev' | 'minkowski'} [metric=euclid] Metric name |
| 313 | + * @param {'euclid' | 'manhattan' | 'chebyshev' | 'minkowski' | function (number[], number[]): number} [metric=euclid] Metric name |
310 | 314 | */ |
311 | 315 | constructor(k = 5, metric = 'euclid') { |
312 | 316 | super(k, metric) |
|
0 commit comments