Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const readline = require('readline')
- const csv = require('csvtojson')
- const ml = require('shaman')
- const rl = readline.createInterface({
- input: process.stdin,
- output: process.stdout
- })
- const csvFilePath = './economicdata1970-2016-ARG.csv'
- // Let's get it (SKEREEE)
- csv()
- .fromFile(csvFilePath)
- .then(obj => dressData(obj))
- .then(([X, Y]) => performRegression(X, Y))
- function dressData(csvData) {
- const header = Object.keys(csvData[0]).filter(
- // remove String fields, this is regression!
- i =>
- i !== 'Year' &&
- i !== 'ISO_Code' &&
- i !== 'Countries' &&
- i !== 'SUMMARY INDEX'
- )
- let X = [],
- Y = []
- csvData.forEach(row => {
- let k = []
- // header.forEach(h => {
- // k.push(f(row[h]))
- // })
- k.push(f(row['Government enterprises and investment']))
- k.push(f(row['Government Consumption']))
- X.push(k)
- Y.push(f(row['SUMMARY INDEX']))
- })
- return [X, Y]
- }
- function performRegression(X, Y) {
- console.log(Y)
- const lr = new ml.LinearRegression(X, Y, {
- algorithm: 'GradientDescent',
- debug: true
- })
- lr.train(err => {
- if (err) console.log(err)
- else predictOutput(lr)
- })
- }
- function f(s) {
- // return parseFloat(s)
- if (!s) return 0
- return parseFloat(s)
- }
- function predictOutput(lr) {
- rl.question(
- 'Enter input "X,X,X" for prediction (Press Ctrl + C to exit)',
- answer => {
- console.log(f(answer))
- // const response = answer.split(',').map(i => Number(i))
- // HARDCODED
- const response = [
- 4,
- 5.34,
- 21.86,
- 6.05,
- 15,
- 6,
- f(answer),
- 7,
- 35,
- 1,
- 58,
- 4,
- 5.35,
- 3.69,
- 2.93,
- 4.26,
- 7.5,
- 3.33,
- 3.63,
- 6.86,
- 3.39,
- 4.13,
- 0.77,
- 3.9,
- 5.23,
- 23.83,
- 5.22,
- 11.94,
- 2,
- 40,
- 10,
- 5.61,
- 6.06,
- 5.91,
- 7.26,
- 13.7,
- 5.94,
- 10.14,
- 6.42,
- 4.47,
- 5.16,
- 4.81,
- 0,
- 5.36,
- 0.77,
- 7.97,
- 4.7,
- 3.98,
- 5,
- 4.26,
- 7,
- 5.42,
- 2.77,
- 2.19,
- 3.43,
- 10,
- 2.52,
- 10,
- 5.15,
- 2.4,
- 6.67,
- 9.12,
- 3.26,
- 5.25,
- 6.51,
- 5.54,
- 5.37
- ]
- console.log('lenght of answer', response.length)
- const prediction = lr.predict([5.34, f(answer)])
- console.log(`At X = ${answer}, y = ${prediction}`)
- predictOutput(lr)
- }
- )
- }
Add Comment
Please, Sign In to add comment