-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSLR.js
51 lines (42 loc) · 1.18 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
45
46
47
48
49
50
51
const ml = require('ml-regression');
const csv = require("csvtojson");
const SLR = ml.SLR; // Simple Linear Regression
const csvFilePath = 'Advertising.csv'; // Data File
let csvData = [], // Parsed Data
X = [], // Inputs
Y = []; // Outputs
let regressionModel;
const readline = require('readline');
const r1 = readline.createInterface({
input : process.stdin,
output : process.stdout
});
csv()
.fromFile(csvFilePath)
.on('json', (jsonObj) => {
csvData.push(jsonObj);
})
.on('done',() => {
dressData(); // Populating X and Y from JSON objects
performRegression(); // SLR function
});
function dressData() { // Populating X and Y from JSON objects
csvData.forEach((row) => {
X.push(f(row.Radio));
Y.push(f(row.Sales));
});
}
function f(s) {
return parseFloat(s);
}
function performRegression() {
regressionModel = new SLR(X,Y);
console.log(regressionModel.toString(3));
predictOutput();
}
function predictOutput() {
r1.question('Enter input X for prediction : ', (answer) => {
console.log('At X = ' + answer + ', y = ' + regressionModel.predict(parseFloat(answer)));
predictOutput();
});
}