-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSLR.js
44 lines (37 loc) · 1.15 KB
/
SLR.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
const SLR = require('ml-regression').SLR;
const csv = require('csvtojson');
const readLine = require('readline');
const csvFilePath = 'advertising.csv'; // the data
let csvData = []; // parsed data
let x = []; // Input
let y = []; // Output
let regressionModel;
const rl = readLine.createInterface({
input: process.stdin,
output: process.stdout
});
csv().fromFile(csvFilePath)
.on('json', (jsonObj) => {
csvData.push(jsonObj);
})
.on('done', () => {
dressData(); // To get data points, from JSON objects
performRegression();
});
function dressData() {
csvData.forEach(row => {
x.push(parseFloat(row.Radio));
y.push(parseFloat(row.Sales));
})
}
function performRegression() {
regressionModel = new SLR(x, y); // Train the model on training data
console.log(regressionModel.toString(3));
predictOutput();
}
function predictOutput() {
rl.question('Enter input x for prediction (press CTRL + C to exit) :', (answer) => {
console.log(`At X = ${answer}, y = ${regressionModel.predict(parseFloat(answer))}`);
predictOutput();
})
}