I have a dataset which looks like this (Original data is here):
var irisjson = [
{"sepalLength": 5.1, "sepalWidth": 3.5, "petalLength": 1.4, "petalWidth": 0.2, "species": "setosa"},
{"sepalLength": 4.9, "sepalWidth": 3.0, "petalLength": 1.4, "petalWidth": 0.2, "species": "setosa"},
{"sepalLength": 4.7, "sepalWidth": 3.2, "petalLength": 1.3, "petalWidth": 0.2, "species": "setosa"}....]
I want to calculate Euclidean Distance and find the closest points. The function I wrote is as follows:
function findClosest(irisjson){
var result = [];
//Calculate Euc. Dist
for(var i=0; i < irisjson.length; i++){
for(var j = 1; j < irisjson.length-1 ; j++){
var a1, a2 , a3 , a4 ;
a1 = irisjson[i].sepalLength -
irisjson[j].sepalLength;
a2 = irisjson[i].sepalWidth -
irisjson[j].sepalWidth;
a3 = Math.pow(a1, 2);
a4 = Math.pow(a2, 2);
result[i] = Math.sqrt(a3 + a4);
}
console.log(result[i]);
}
}
When I print the result to console, I see 1.1045361017187265
in the first line. However, when I manually test it, as follows, I see result 0.5385164807134502
:
a1 = irisjson[0].sepalLength -
irisjson[1].sepalLength;
a2 = irisjson[0].sepalWidth -
irisjson[1].sepalWidth;
a3 = Math.pow(a1, 2);
a4 = Math.pow(a2, 2);
result = Math.sqrt(a3 + a4);
console.log("res:", result);
Any ideas on why I receive different results?
I would appreciate any help, Thanks!