And you thought it wasn’t easy
Long Answer: It is possible and I am actually surprised developers haven’t given it the attention it deserves. As far as
scikit-learn is concerned, the JS people have made their own set of libraries to counter it, and I am gonna use one too. But first, a little bit about Machine Learning. Feel free to board this rocket 🚀 and jump to the code, though.
According to Arthur Samuel, Machine Learning provides computers with the ability to learn without being explicitly programmed. In other words, it gives computer the ability to learn on their own and execute the correct instructions, without you providing them directions.
It has been around for quite a while now, with Google going from mobile-first strategy to AI-first.
- Slow. (MYTH!?!)
- Matrix Manipulation is difficult.(There are libraries, for example
- Only concerned with Web Development.(Somewhere out there, Node.js is laughing.)
- Libraries are usually made for Python. (The JS people are not behind)
- brain.js (Neural Networks)
- Synaptic (Neural Networks)
- Natural (Natural Language Processing)
- ConvNetJS (Convolutional Neural Networks)
- mljs (A set of sub-libraries with a variety of functions)
- Neataptic (Neural Networks)
- Webdnn (Deep Learning)
We are going to use mljs’s regression library to perform some linear regression sorcery. All the code is on Github: machine-learning-with-js
Step 1. Install the libraries
$ yarn add ml-regression csvtojson
Or if you like
$ npm install ml-regression csvtojson
ml-regression does what the name implies.
csvtojson is a fast csv parser for node.js that allows loading
csv data files and converts it to
Step 2. Initialize the library and load the Data
Download the data file(.csv) from here and put it inside your project.
Assuming you have already initialized an empty npm project, open your
index.js file and enter the following. (You could copy/paste if you want, but I’d prefer typing it yourself for better understanding.)
I put the file at the root of the project, so, if you have put it somewhere else, make sure you update the
csvFilePath variable likewise.
Pretty neat, eh?
Now we are going to use the
fromFile method of
csvtojson to load our data file.
Step 3. Dressing Data to get it ready for execution
The JSON objects we saved in
csvData are well, objects, and we need an array of input data points as well as output data points. We are going to run our data through a
dressData function that will populate our
Step 4. Train your model and start predicting
Now that our data has successfully been dressed, it’s time to train our model.
For this, we are going to write a
regressionModel has a method
toString that takes a parameter named precision for floating point outputs.
predictOutput function allows you to enter input values, and outputs the predicted output to your console.
Here’s how it looks: (Note that I am using Node.js’ readline utility)
And here’s the code for adding reading user input:
Step 5. Hooray! You did it. Pat yourself in the back.
If you followed the steps, this is how your index.js should look:
Go to your Terminal and run
node index.js and it will output something like this:
$ node index.js
f(x) = 0.202 * x + 9.31
Enter input X for prediction (Press CTRL+C to exit) : 151.5
At X = 151.5, y = 39.98974927911285
Enter input X for prediction (Press CTRL+C to exit) :
If you are excited, go check out Part 2.
PS: I am going to use
mland other libraries(listed above!) to execute popular machine learning algorithms on various data-sets. Keep an eye on my profile, or you could cut yourself some slack and follow me. :)
Thanks for reading! If you liked it, hit the green button to let others know about how powerful JS is and why it shouldn’t be lagging behind when it comes to Machine Learning.
Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!