Skip to content

Instantly share code, notes, and snippets.

@kmaher9
Created September 6, 2018 21:14
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kmaher9/805c5ef3e89b1c2a28f02f1dbb06af1e to your computer and use it in GitHub Desktop.
Save kmaher9/805c5ef3e89b1c2a28f02f1dbb06af1e to your computer and use it in GitHub Desktop.
var brain = require('brain.js')
var fs = require('fs')
// configuration to be used in the brain
const config = {
binaryThresh: 0.5, // arbitary value
hiddenLayers: [3], // the size of the hidden layers in the network
activation: 'sigmoid' // activation function
}
// array to hold the training data after formatting
var trainingData = []
// initialise a new backpropogating neural network
const net = new brain.NeuralNetwork(config)
// load each line of the training data into a new index of an array
var trainingFile = fs.readFileSync("data.csv").toString().split("\n")
// iterate over each line of the training data, adding it to a new array for training later
for (var i = 0; i < trainingFile.length; i++) {
var entry = trainingFile[i]
var values = entry.split(",")
var points = [parseInt(values[0]), parseInt(values[1]), parseInt(values[2]), parseInt(values[3])]
if (i <= 49) {
trainingData.push({input: points, output: {setosa: 1}})
}
else if (i > 49 && i <= 99) {
trainingData.push({input: points, output: {versicolor: 1}})
}
else if (i > 99 && i <= 149) {
trainingData.push({input: points, output: {virginica: 1}})
}
}
// console log the output of each iteration
net.train(trainingData, {
log: true
})
// test data - actually virginica
var output = net.run([6,3,5,2])
console.log(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment