And you thought it wasn’t easy Linear Regression Plot using plot.ly Machine Learning with JavaScript_. Here’s_ This is Part 1 of the ongoing series Part 2. JAVASCRIPT?! Shouldn’t I be using Python? Am I out of my mind to try those hefty calculations in JavaScript? Am I trying to act cool by using a language that is not Python or R? scikit-learn doesn’t even work in JavaScript? Short Answer: No. I am not drunk. Long Answer: It is possible and I am actually surprised developers haven’t given it the attention it deserves. As far as 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. scikit-learn unsplash.com According to Arthur Samuel, 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. Machine Learning It has been around for quite a while now, with Google going from mobile-first strategy to AI-first. Why JavaScript is not mentioned with ML? Slow. ( ) MYTH!?! Matrix Manipulation is difficult.(There are libraries, for example ) **math.js** Only concerned with Web Development.(Somewhere out there, is laughing.) Node.js Libraries are usually made for Python. (The JS people are not behind) There are a handful of libraries in JavaScript with pre-made Machine Learning algorithms, such as Linear Regression, SVMs, Naive-Bayes’s, et cetera. Here are a few of them, (Neural Networks) brain.js (Neural Networks) Synaptic (Natural Language Processing) Natural (Convolutional Neural Networks) ConvNetJS (A set of sub-libraries with a variety of functions) mljs (Neural Networks) Neataptic (Deep Learning) Webdnn 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 Install the libraries Step 1. $ yarn add ml-regression csvtojson Or if you like npm $ npm install ml-regression csvtojson does what the name implies. ml-regression is a fast csv parser for node.js that allows loading data files and converts it to . csvtojson csv JSON Step 2. Initialize the library and load the Data Download the data file(.csv) from and put it inside your project. here Assuming you have already initialized an empty npm project, open your file and enter the following. (You could copy/paste if you want, but I’d prefer typing it yourself for better understanding.) index.js I put the file at the root of the project, so, if you have put it somewhere else, make sure you update the variable likewise. csvFilePath Pretty neat, eh? Now we are going to use the method of to load our data file. fromFile csvtojson Step 3. Dressing Data to get it ready for execution The JSON objects we saved in 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 function that will populate our and variables. csvData dressData X y 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 function: performRegression The has a method that takes a parameter named precision for floating point outputs. regressionModel toString The function allows you to enter input values, and outputs the predicted output to your console. predictOutput 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 and it will output something like this: node index.js $ node index.js f(x) = 0.202 * x + 9.31Enter input X for prediction (Press CTRL+C to exit) : 151.5At X = 151.5, y = 39.98974927911285Enter input X for prediction (Press CTRL+C to exit) : Congratulations. You just trained your first Linear Regression Model in JavaScript. (Did you notice the speed?) If you are excited, go check out . Part 2 I am going to use and 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. :) PS: _ml_ ! If you liked it, hit the to let others know about how powerful is and why it when it comes to Machine Learning. Thanks for reading green button JS shouldn’t be lagging behind