function knn(X_train, y_train, X_test, k) {
const y_pred = [] // for multiple predictions
X_test.forEach(test_point => {
// Calculate distance between the test point and all training points
let distances = []
X_train.forEach((train_point, i) => {
const label = y_train[i]
const distance = euclidean_distance(test_point, train_point)
distances.push([distance, label])
})
if (extraLogging) console.log({distances})
// Ascending
const sortedDistances = distances.sort(([distance], [distance2]) => {
return distance - distance2
})
if (extraLogging) console.log({sortedDistances})
// Get labels for k nearest neighbors
const k_nearest_labels = sortedDistances.slice(0,3).map(([x,lab]) => lab)
if (extraLogging) console.log({k_nearest_labels})
// Predict label by majority vote
const setval = new Set(k_nearest_labels)
if (extraLogging) console.log(setval)
// const max = Math.max(...setval)
const pred_label = [...setval].reduce((a, b) => k_nearest_labels.filter(v => v === a).length >= k_nearest_labels.filter(v => v === b).length ? a : b);
y_pred.push(pred_label)
})
return y_pred
}
// Predict
X_test = [[1, 1], [3, 3], [1, 3], [3, 1], [2, 3], [3, 2], [2, 2]]
const y_pred = knn(X_train, y_train, X_test, k)
X_test.forEach((test, i) => console.log({ test, pred: y_pred[i] }))