-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (49 loc) · 1.79 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const csvFilePath='./data/choices.csv';
const csv = require('csvtojson');
const ML = require('ml-random-forest');
const { performance } = require('perf_hooks');
// Classes that will define the expected output
const choices = ['Burger', 'Burrito', 'Cafe', 'Indian', 'Japanese', 'Korean', 'Pizza', 'Salad', 'Thai', 'Vietnamese', 'Wrap'];
// Reading the dataset from CSV
csv()
.fromFile(csvFilePath)
.then((data)=>{
const training = [];
const prediction = [];
for (let i in data) {
const { Price, WeekOfDay, Day, Month, Sandra } = data[i];
// Arranging training set
training.push([
parseInt(Price),
parseInt(WeekOfDay),
parseInt(Day),
parseInt(Month),
parseInt(Sandra)
]);
prediction.push(data[i].Food);
}
const perf = performance.now();
console.log('Data ready, running the model now..');
// Options can be tweaked to improve results
const options = {
seed: 11,
maxFeatures: 2,
replacement: true,
nEstimators: 25
};
// Pick a classifier, and see how you go.
const classifier = new ML.RandomForestClassifier(options);
classifier.train(training, prediction.map((elem) =>
choices.indexOf(elem)
));
const timeElapsed = Math.ceil((performance.now() - perf));
console.log(`Model trained in ${timeElapsed}ms.`);
// Calculate result for tomorrow
const price = 21;
const today = new Date();
today.setDate(today.getDate()+1);
const result = classifier.predict([[price, today.getDay(), today.getDate(), today.getMonth() + 1, 1]]);
// const result = classifier.predict([[6, 2, 21,11, 1]]);
console.log(`Tomorrow's menu is: ${choices[result[0]]}`);
return result;
});