kNN(k-Nearest Neighbors)JavaScript实现
作者:互联网
一个简单的demo(可以通过浏览器开发者工具或者node执行):
// Key terminology: // training set has training examples (features + target variable) // In the classification problem the target variables are called classes // test set // knowledge representation function makeTrainingSet(dimension_of_feature, num_of_examples) { let result = []; for (let i = 0; i != num_of_examples; ++i) { let example = { features: [], label: Math.floor(Math.random() * 3), } for (let j = 0; j != dimension_of_feature; ++j) { example.features[j] = Math.random() * 10 } result.push(example) } return result } let trainingSet = makeTrainingSet(3, 20) // 模拟训练集; 特征向量维数为3; 数据量为20 console.log(trainingSet) let features = makeTrainingSet(3, 1)[0].features // 此条数据用于测试 console.log(features) function getDistance(f1, f2) { const DIMENSION = f1.length // 将距离定义为欧氏距离 let sumOfSquares = 0 for (let i = 0; i != DIMENSION; ++i) { sumOfSquares += Math.pow(f1[i] - f2[i], 2) } return Math.sqrt(sumOfSquares) } function getKNearestNeighbors(features, k, trainingSet) { // 返回值: 数组; k个距离最近的邻居; 对象属性为【距离+标签】 // 参数: k 邻居个数 ; features 未知数据的特征向量 ; trainingSet 已经贴了标签的数据集 let distanceSet = trainingSet.map(training_example => { let distance = getDistance(features, training_example.features) return { distance: distance, label: training_example.label, } }) distanceSet.sort((a, b) => a.distance - b.distance) return distanceSet.slice(0, k) } console.log(getKNearestNeighbors(features, 5, trainingSet))
输出示例:
标签:kNN,Nearest,training,features,JavaScript,let,trainingSet,example,Math 来源: https://www.cnblogs.com/xkxf/p/15932198.html