Last active
April 15, 2019 13:45
-
-
Save Robert-G-J/4dbabc09684bb3ee898caaa2f9aa7157 to your computer and use it in GitHub Desktop.
KNN Classifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const harness = require("../harness/harness.js"); | |
class Classifier { | |
constructor(knn) { | |
this.trainingData = []; | |
this.trainingLabels = []; | |
this.knn = knn; | |
} | |
fit(trainingData, trainingLabels) { | |
this.trainingData = trainingData; | |
this.trainingLabels = trainingLabels; | |
} | |
predict(testingData) { | |
const predictions = testingData.map((testRow, testIdx) => { | |
const distances = this.trainingData.map((trainRow, trainIdx) => { | |
return { | |
distance: calculateEuclideanDistance(testRow, trainRow), | |
index: trainIdx, | |
outcome: this.trainingLabels[trainIdx] | |
}; | |
}); | |
distances.sort(sortDistances); | |
const knn = distances.splice(0, this.knn); | |
const counts = getMode(knn); | |
const mode = Object.keys(counts).reduce((a, b) => | |
counts[a] > counts[b] ? a : b | |
); | |
return [parseInt(mode, 10)]; | |
}); | |
return predictions; | |
} | |
} | |
const getMode = (arr, result) => { | |
result = result || {}; | |
if (arr.length === 0) return result; | |
let head = arr.shift().outcome; | |
if (result[head]) result[head]++; | |
else result[head] = 1; | |
return getMode(arr, result); | |
}; | |
const calculateEuclideanDistance = (arr1, arr2) => { | |
const sum = arr1.reduce( | |
(acc, data, idx, arr) => | |
acc + Math.pow(parseFloat(data) - parseFloat(arr2[idx]), 2) | |
); | |
return Math.sqrt(sum); | |
}; | |
const sortDistances = function(a, b) { | |
if (a.distance < b.distance) { | |
return -1; | |
} | |
if (a.distance > b.distance) { | |
return 1; | |
} | |
// a must be equal to b | |
return 0; | |
}; | |
const calculateF1 = k => { | |
const algo = new Classifier(k); | |
const result = harness.evaluator("../diabetes.csv", algo); | |
return result.f1 | |
} | |
const findBestK = (maxK) => { | |
let bestK = 0 | |
let bestResult = 0 | |
for(let k = 1; k < maxK; k++) { | |
const f1 = calculateF1(k) | |
if (bestResult < f1) { | |
bestResult = f1 | |
bestK = k | |
} | |
} | |
return [bestResult, bestK] | |
} | |
console.log("RESULT", findBestK(100)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment