Skip to content

Instantly share code, notes, and snippets.

@johnkazer
Last active May 9, 2019 07:28
Show Gist options
  • Save johnkazer/f381540c039945448b47d90340a1e5b4 to your computer and use it in GitHub Desktop.
Save johnkazer/f381540c039945448b47d90340a1e5b4 to your computer and use it in GitHub Desktop.
exports.start = function (res) { // res is the Express response object, being the server package I used
let training = true
let trainingDataSet = []
let testResult = []
const network = new NN.NeuralNetwork();
let dataTypes = []
let listOfTrainingData = []
let listOfTestData = []
let trainingDataRaw = []
let testDataRaw = []
var parser = csv({ delimiter: ',' }, function (err, data) {
if (err) {
return null
} else {
data.forEach(function (element, index) { // data[][] represents elements of each row in the CSV
if (index === 0) {
// header row
element.forEach(function (header) {
dataTypes.push(header)
})
} else {
if(element[0] === 'TEST') {
// flag start of test data
training = false
} else { // last 3 rows (elements) of data[][] are test data
if(training) {
listOfTrainingData.push(element[0]) // first element is a unique designator, not data (is for user feedback)
trainingDataRaw.push(element.slice(1))
} else {
listOfTestData.push(element[0]) // designator
testDataRaw.push(element.slice(1))
}
}
}
})
trainingDataRaw.forEach((data) => {
const input = data.slice(0, data.length - 1)
const output = data.slice(data.length - 1)
trainingDataSet.push({ input: input, output: output })
})
network.train(trainingDataSet, { /* some config settings */ })
testDataRaw.forEach((dataPoint) => {
const input = dataPoint.slice(0, dataPoint.length - 1)
const output = dataPoint.slice(dataPoint.length - 1)
testResult.push({ actual: network.run(input), desired: output })
})
}
})
try {
const stream = fs.createReadStream(path.join(__dirname, 'data.csv'))
stream.pipe(parser)
stream.on('close', () => {
res.send(testResult)
})
stream.on('error', (error) => {
throw(error)
})
} catch (err) {
res.send(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment