我试图在Javascript中实现基本的KNN算法。
首先,我需要将神秘矢量的平方距离与所有其他矢量相加。
我希望能够检索'数组的名称。这些载体一旦完成就完成了。
例如,在我找到3个最接近的相邻向量之后,如何记录这些向量来自哪个数组,例如;三个最接近的向量是' 3,4' (香蕉) 。 ' 3,5'(香蕉),' 3,1'(草莓)
//colours on y axis
// 1 = blue
// 2 = green
// 3 = Red
// 4 = Yellow
//Size on x axis (cm)
function storeCoordinate(x, y, array) {
array.push(x);
array.push(y);
}
var Apples = [];
storeCoordinate(3, 5, Apples); // a - Apple
storeCoordinate(3, 6, Apples); // b - Apple
storeCoordinate(3, 7, Apples); // c - Apple
storeCoordinate(3, 4, Apples); // d - Apple
var Bananas = [];
storeCoordinate(4, 8, Bananas); // e - Banana
storeCoordinate(4, 9, Bananas); // f - Banana
storeCoordinate(4, 10, Bananas); //g - Banana
storeCoordinate(4, 7, Bananas); // h - Banana
var Strawberries = [];
storeCoordinate(3, 3, Strawberries); // i - Strawberry
storeCoordinate(3, 2, Strawberries); //j - Strawberry
storeCoordinate(3, 1, Strawberries); // k - Strawberry
storeCoordinate(3, 4, Strawberries); //l - Strawberry
var X = 3 ;
var Y = 5;
// mystery vector
var a = ( (Apples[0]-X)*(Apples[0]-X) + (Apples[1]-Y)*(Apples[1]-Y) );
var b = ( (Apples[2]-X)*(Apples[2]-X) + (Apples[3]-Y)*(Apples[3]-Y) );
var c = ( (Apples[4]-X)*(Apples[4]-X) + (Apples[5]-Y)*(Apples[5]-Y) );
var d = ( (Apples[6]-X)*(Apples[6]-X) + (Apples[7]-Y)*(Apples[7]-Y) );
console.log(c);