-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (32 loc) · 1.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// require('@tensorflow/tfjs-node');
const tf = require('@tensorflow/tfjs');
const loadCSV = require('./load-csv');
function knn(features, labels, predictionPoint, k) {
return features
.sub(predictionPoint)
.pow(2)
.sum(1, { keepDims: true })
.pow(0.5)
.concat(labels, 1)
.unstack()
.sort((a, b) => {
a.slice(0, 1).dataSync()[0] > b.slice(0, 1).dataSync()[0] ? 1: -1;
})
.slice(0, k)
.reduce((acc ,pair) => acc + pair.slice(1, 1).dataSync()[0], 0) / k;
}
let { features, labels, testFeatures, testLabels } = loadCSV('kc_house_data.csv', {
shuffle: true,
splitTest: 10,
dataColumns: ['lat', 'long'],
labelColumns: ['price']
});
features = tf.tensor(features);
labels = tf.tensor(labels);
// testFeatures = tf.tensor(testFeatures);
// testLabels = tf.tensor(testLabels);
testFeatures.forEach((testPoint, i) => {
const result = knn(features, labels, tf.tensor(testPoint), 10);
const err = (testLabels[i][0] - result) / testLabels[i][0]
console.log('Error', err * 100);
});