-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate.js
68 lines (60 loc) · 1.39 KB
/
calculate.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
importScripts("math.min.js");
var dataForTest = [];
var xmlHttp = new XMLHttpRequest();
try
{
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "data.json", true);
xmlHttp.send(null);
}
catch(exception)
{
alert("xmlHttp Fail");
}
function handleStateChange()
{
if(xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200 || xmlHttp.status == 0)
{
var result = xmlHttp.responseText;
var json = eval("(" + result + ")");
dataForTest = json;
}
}
}
onmessage = function(event){
var real = math.matrix(event.data.real);
var test = dataForTest;
var results = [];
for(var i in test){
var testMat = math.matrix(test[i].value);
var label = test[i].num;
var result = math.sum(math.square(math.subtract(real,testMat)));
results.push({label:label,score:result});
}
var kNNOrigins = results.sort(function(a,b){
return a.score-b.score;
});
var result = kNN(kNNOrigins,3);
postMessage(result);
}
function kNN(kNNOrigins,n){
var result = {};
var maxLabel = "";
for(var i=0;i<n;i++){
var label = kNNOrigins[i].label;
if(!result[label]){
result[label] = 0;
}
result[label]++;
}
for(var key in result){
if(maxLabel === ""){
maxLabel = key;
}else if(result[key]>result[maxLabel]){
maxLabel = key;
}
}
return key;
}