-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (33 loc) · 1.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const euclidean = (arr1, arr2) =>
Math.hypot(...arr1.map((val, index) => val - arr2[index]));
const sortBy = (dataset, f) => dataset.sort((a, b) => f(a) - f(b));
const identity = val => val;
const getKitens = (arr, k) => arr.slice(0, k);
const makeOdd = val => (val % 2 ? val : val + 1);
const countBy = (arr, f = identity) => [
...arr
.map(item => f(item))
.reduce((acc, val) => acc.set(val, 1 + (acc.get(val) || 0)), new Map())
];
const maxBy = (arr, f = identity) => {
const transform = arr.map(item => f(item));
return arr[transform.indexOf(Math.max(...transform))];
};
const distances = {
euclidean
};
const defaultOptions = dataset => ({
k: Math.round(dataset.length ** 0.5),
distance: "euclidean"
});
const classify = (unknown, dataset, options = defaultOptions(dataset)) => {
const datasetSorted = sortBy(dataset, ({ data }) =>
distances[options.distance](data, unknown.data)
);
const arrKItens = getKitens(datasetSorted, makeOdd(options.k));
const countClass = countBy(arrKItens, ({ group }) => group);
return maxBy(countClass, item => item[1])[0];
};
module.exports = {
classify
};