-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (76 loc) · 1.98 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const readline = require("readline-sync");
const fs = require("fs");
class RGB {
R;
G;
B;
Classification;
Distance;
constructor(R, G, B, Classification) {
this.R = R;
this.G = G;
this.B = B;
this.Classification = Classification;
}
}
const BaseData = [
[1, 10, 200, 1],
[2, 20, 230, 1],
[6, 25, 150, 1],
[7, 45, 100, 1],
[10, 50, 125, 1],
[3, 24, 111, 1],
[100, 4, 10, 2],
[250, 7, 50, 2],
[243, 5, 68, 2],
[210, 2, 90, 2],
[200, 1, 95, 2],
[215, 0, 68, 2],
[56, 200, 1, 3],
[79, 234, 3, 3],
[80, 210, 8, 3],
[95, 200, 10, 3],
[80, 210, 4, 3],
[49, 207, 1, 3],
];
async function start() {
const input_RGB = new RGB();
const KNN_LIST = [3, 5, 7];
input_RGB.R = getNumberInput("R");
input_RGB.G = getNumberInput("G");
input_RGB.B = getNumberInput("B");
const distancePerNeighbor = BaseData.map((row) => {
const base_RGB = new RGB(row[0], row[1], row[2], row[3]);
return mapToDistanceObj(base_RGB, input_RGB);
});
KNN_LIST.forEach((K) => {
console.log("==================");
console.log(`KNN(${K})`);
console.log(getClassificationRanking(distancePerNeighbor, K));
console.log("==================");
});
}
function getClassificationRanking(RGBMappedList, NN) {
const resultObj = {};
const RGBRestrictMappedList = RGBMappedList.sort((a, b) =>
a.Distance < b.Distance ? -1 : 1
).slice(0, NN);
const klassList = RGBRestrictMappedList.map(
(RGB_Item) => RGB_Item.Classification
);
klassList.forEach((classification) => {
resultObj[classification] = (resultObj[classification] || 0) + 1;
});
return resultObj;
}
function getNumberInput(value) {
return parseInt(readline.question(`Input ${value} Value: `));
}
function mapToDistanceObj(base_RGB, input_RGB) {
const R_Pow = (base_RGB.R - input_RGB.R) ** 2;
const G_Pow = (base_RGB.G - input_RGB.G) ** 2;
const B_Pow = (base_RGB.B - input_RGB.B) ** 2;
base_RGB.Distance = (R_Pow + G_Pow + B_Pow) ** 0.5;
return base_RGB;
}
start();