-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalidacja.js
188 lines (171 loc) · 6.7 KB
/
walidacja.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const splitValidation = document.getElementById("splitValidation");
let validationTables;
let toSlice = [];
splitValidation.addEventListener("click", () => {
if (!(data.length > 0)) {
node.textContent = "Nie wczytano pliku!";
komunikat.appendChild(node);
setTimeout(() => {
komunikat.removeChild(node);
}, 1500);
return;
}
k = Number.parseInt(kInput.value);
p = Number.parseInt(pInput.value);
toSlice = [];
for (let i = 0; i < data.length; i++) {
toSlice[i] = [];
for (let j = 0; j < data[i].length; j++) {
toSlice[i][j] = data[i][j];
}
}
const parts = 10;
validationTables = split(toSlice, parts);
knn10(validationTables);
});
function split(arr, parts) {
let vT = [];
let l = [];
let r = [];
for (let i = 0; i < arr.length; i++) {
l[i] = Math.floor(arr[i].length / parts);
r[i] = arr[i].length % parts;
}
for (let k = 0; k < parts; k++) {
vT[k] = [];
}
for (let i = 0; i < arr.length; i++) {
let lol = 0;
while (arr[i].length && lol < parts) {
vT[lol++].push(arr[i].splice(0, l[i]));
}
}
for (let i = 0; i < arr.length; i++) {
for (let m = r[i] - 1; m >= 0; m--) {
vT[m][i].push(arr[i][0]);
arr[i].splice(0, 1);
}
}
return vT;
}
function knn10(arr) {
k = Number.parseInt(kInput.value);
p = Number.parseInt(pInput.value);
const wal = document.getElementById('wal');
wal.textContent = "";
const hh = document.createElement('h3');
wal.appendChild(hh);
let s = 0;
for (let m = 0; m < arr.length; m++) {
let t = [];
for (let o = 0; o < arr.length; o++) {
if (m !== o)
t.push(arr[o]);
}
const div1 = document.createElement('div');
div1.className = "section-table-1";
const h = document.createElement('h3');
h.innerHTML = "Tabela nr: " + (m + 1);
div1.appendChild(h);
const testH = document.createElement('h4');
div1.appendChild(testH);
const table = document.createElement('table');
table.className = "table";
let classificationPercent = 0;
let iksde = 0;
let sum = 0;
for (let i = 0; i < arr[m].length; i++) {
sum += arr[m][i].length;
for (let j = 0; j < arr[m][i].length; j++) {
const fn = find_neighbors2(arr[m][i][j], t);
const mv = majority_vote(fn);
//tworzymy wiersze
const tr = document.createElement('tr');
let td = document.createElement('td');
td.appendChild(document.createTextNode(++iksde));
tr.appendChild(td);
td = document.createElement('td');
const divPoint = document.createElement('div');
divPoint.appendChild(document.createTextNode(arr[m][i][j]));
divPoint.id = iksde.toString() + m;
divPoint.className = "classClickk";
const neighborsText = document.createElement('p');
neighborsText.id = divPoint.id + "n";
neighborsText.className = "hideText";
fn.forEach(neigh => {
const neighborText = document.createElement("p");
let przesuniecie = [];
let odleglosc = 0;
for(let iii = 0; iii<neigh.length; iii++){
przesuniecie.push(Number.parseFloat(arr[m][i][j]) - Number.parseFloat(neigh[0][iii]));
}
przesuniecie.forEach(prz => {
odleglosc += prz * prz;
});
odleglosc = Math.sqrt(odleglosc);
neighborText.textContent = neigh[0] + " " + names[neigh[1]] + " Odległość: " + odleglosc.toPrecision(4).toString();
neighborsText.appendChild(neighborText);
});
divPoint.appendChild(neighborsText);
td.appendChild(divPoint);
tr.appendChild(td);
td = document.createElement('td');
td.appendChild(document.createTextNode(names[mv]));
tr.appendChild(td);
//dla każdego wiersza sprawdzamy czy wyliczona wartość mv jest zgodna z typem punktu z excela
//jeśli jest zgodna to w tabeli zaznaczy się na kolor zielony jeśli nie to na czerwony
for (let ii = 0; ii < data.length; ii++) {
for (let jj = 0; jj < data[ii].length; jj++) {
if (arr[m][i][j] === data[ii][jj]) {
if (mv === ii) {
tr.style.backgroundColor = "green";
classificationPercent++;
break;
}
else {
tr.style.backgroundColor = "red";
break;
}
}
}
table.appendChild(tr);
}
}
}
s += classificationPercent / sum;
testH.innerHTML = "Dokładność klasyfikacji wynosi: " + ((classificationPercent / sum) * 100).toPrecision(4).toString() + "%";
div1.appendChild(table);
wal.appendChild(div1);
}
const els = document.getElementsByClassName("classClickk");
Array.from(els).forEach((el) => {
el.addEventListener("click", () => {
const neighbor = document.getElementById(el.id.toString() + "n");
if (neighbor.className === "hideText") {
console.log("u " + el.id.toString() + "n");
neighbor.className = "";
} else {
console.log("h " + el.id.toString() + "n");
neighbor.className = "hideText";
}
});
});
hh.innerHTML = "Dokładność klasyfikacji ogólnej wynosi: " + ((s / arr.length) * 100).toPrecision(4).toString() + "%";
}
function find_neighbors2(point, tt) {
const dists = [];
for (let kk = 0; kk < tt.length; kk++) {
for (let i = 0; i < tt[kk].length; i++) {
for (let j = 0; j < tt[kk][i].length; j++) {
const dist = distance(point, tt[kk][i][j]);
dists.push([dist, [tt[kk][i][j], i]]);
}
}
}
dists.sort(function (a, b) { return a[0] - b[0] });
const neighbors = [];
for (let i = 0; i < k && i < dists.length; i++) {
neighbors.push(dists[i][1]);
}
return neighbors;
}