-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-features.ts
50 lines (47 loc) · 1.36 KB
/
create-features.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { data, tensor2d, util } from "@tensorflow/tfjs-node-gpu";
import { normalize } from "./normalise";
const shuffle = util.shuffle;
async function createFeatures() {
// import dataset from CSV file
const houseSalesDataset = data.csv("file://data/kc_house_data.csv");
// get x,y values
const pointsDataset = await houseSalesDataset.map((record) => ({
//@ts-ignore
x: record.sqft_living,
//@ts-ignore
y: record.price,
}));
const points = await pointsDataset.toArray();
// if odd number will eliminate last one so can split in two exactly
if (points.length % 2 !== 0) {
points.pop();
}
// shuffle the points
shuffle(points);
// Features (input)
const featureValues = points.map(({ x }) => x as number);
const featureTensor = tensor2d(featureValues, [featureValues.length, 1]);
// Labels (output)
const labelValues = points.map(({ y }) => y as number);
const labelTensor = tensor2d(labelValues, [labelValues.length, 1]);
// normalize Tensors
let {
tensor: normalizedFeatureTensor,
min: minFeature,
max: maxFeature,
} = normalize(featureTensor);
let {
tensor: normalizedLabelTensor,
min: minLabel,
max: maxLabel,
} = normalize(labelTensor);
return {
normalizedFeatureTensor,
normalizedLabelTensor,
minFeature,
maxFeature,
minLabel,
maxLabel,
};
}
export { createFeatures };