K-nearest-neighbor

node v18.11.0
version: 1.0.0
endpointsharetweet
Medium article: https://medium.com/@waywardverities/ineffici%C3%ABnt-guide-3-k-nearest-neighbor-in-javascript-c54befdf2954
const X_train = [[1, 2], [2, 1], [2, 3], [3, 2]] const y_train = [0, 0, 1, 1] // settings const k = 3 const extraLogging = false
function euclidean_distance(testArr, trainArr) { const sub = testArr.map((testVal, i) => { return testVal - trainArr[i] }) if (extraLogging) console.log(`sub ${sub}`) const power = sub.map(val => val**2) if (extraLogging) console.log(`power ${power}`) const sum = power.reduce((total, val) => total + val, 0) if (extraLogging) console.log(`sum ${sum}`) const sqrt = Math.sqrt(sum) if (extraLogging) console.log(`sqrt ${sqrt}`) return sqrt } // test euclidean_distance([1, 1], [1, 2])
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] }))
Loading…

no comments

    sign in to comment