House rent prediction using Machine Learning in JavaScript
By: Vrushit Bharucha
Tags: Lodash, JavaScript, Tensorflowjs, Predictions, Machine Learning

Some of us would be in a confusion that why Machine Learning with JavaScript and not with python. But as you’ll read this article further you will notice that it quite similar to shifting languages when we have a strong Data Structures and Algorithm hands. The Main concepts of Machine Learning and that to Linear Regression remains same with predicting a final Answer.

Firstly understanding the JavaScript concept of Machine Learning:

The Libraries used here are NodeJs( which is common for any JavaScript lover), TensorFlowJs( Sibling of TensorFlow.py ) and finally Lodash( Calculation purpose, just some common concepts with pandas and numpy). If We want, we can also add additional CSV loading libraries used in any Editor.

Secondly the Editor,

Ahh!! the most important thing( even important than libraries or concept ). So keeping expectations low, I have used VS Code and nothing else. Atom can also be used but it does not comes with a per-loaded terminal and additional support. It was just a statement and not comparison of 2 world Class JavaScript Code Editor.

Retrieving back on the topic and one of most hectic and waiting period of any JavaScript developer, which is installing libraries. So as i said above, we only need 3 libraries to be installed.

Note: NodeJS is a must which should be preinstalled on the system.

(i) TensorFlowJS

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script>

or

npm install @tensorflow/tfjs

and also

npm install @tensorflow/tfjs-node

Please don't forget to install both the libraries (tfjs and tfjs-node). I don't want any other person to face issues like me ( Please read if you have time or else go to Lodash installation: So As being a JavaScript Developer, node modules will be your enemy throughout the journey. So without GPU you are already facing a installing time period which is 100¹⁰ times JavaScript execution time. So please install the libraries when your mind is free)

(ii) Lodash

npm i --save lodash

So that’s 40% job done WOOF!!

I won’t cover the deep understanding of these libraries although you can visit the documentation below:

tensorflowJS: https://www.tensorflow.org/js/

LodashJs: https://lodash.com/

The Dataset used in this prediction is been collected manually alongside my partner in this project( Krutarth Jaiswal ) so please do at your own risk otherwise search on Internet or use ours ( We don’t have any issue with that). The Dataset can be found or downloaded from my github repo( link at the end of Article, please do clap if you go down, Ahh just kidding your wish).

So now you have Datasets and Libraries installed lets start some Coding and predict some rents for testing.

Note: Please follow the steps for files and codes. If any problem faced please use my repo as reference( you know where the link is ??, at the end of article )

So there are basically 3 main file in this project.

index.js ( Entry point )

linear-regression.js ( Algorithms or Black box ( people from software engineering will definitely love it) )

load-csv.js ( Loading a CSV file specifically for JS purpose )

So starting from Load CSV, it is a JavaScript file which basically load the whole csv file into your JavaScript program which can be manipulated and saved just like in python. Before starting the main thing , load-csv.js is easily available on internet. So We will mainly focus on index.js and linear-regression.js. If any problem faced in load-csv.js which is in my github repo, you can comment down the error and I will get back to you in a short while.

index.js

Figure.1 (Loading Libraries)

So the above 3 line of code represent the libraries which are been included in the index.js file and now we will create the variables and inputs which are to be tested upon

Figure.2 ( Creating variable and connecting with other 2 files)

So in above line of Code we have create links to our other 2 files or libraries you can say. And if you would notice in the lines we have also included the CSV Loading thing, which as mentioned brings us to our dataset.

Another important thing to notice here is the prediction points, As we are predicting Price or rent, we can directly include the pricing column of the dataset. And for prediction as you can we have used Longitudes and Latitudes which are basically access pint which help us in Linear regression to predict the exact or near to position and tells its price. We have included only 2 attribute there are other attributes which you can use, just don’t mess the notations and syntax.

Figure.3 (Specifying things)

Here is where we decide the learning, epochs(batch size) and iteration for the Linear Regression algorithm to learn and predict upon the Model.

Figure.4 (Graph)

Now this step is all up to you. this steps make or plot a graph for us which shows us the quantities which we are working upon. And we can also print the R2 value for it.

Figure.5 ( Actual Graph)

The output would look something like this.

Now comes the final step for our index.js file which is just the prediction point which works based on the example or Longitude and latitude point provided to the Linear Regression Algorithm.

Figure.6 (Prediction)

So this was all about index.js, the rest of the stuff in the first line in above Screenshot is basically how Lodash helps us to create a variable in JavaScript which can be used in any way after executing.

This forms around 10% of our project. ( 40 + 10 = 50 )

Now 50% more to go

linear-regression.js

Figure.7 ( Including libraries )

So this comes the same way, of including the libraries of Machine Learning.

Figure.8 ( Creating class and constructor for Linear Regression )

Note: Please check the original code for indentation and brackets closing. The Original code will be in my repo whose link is mentioned at the last.

Now here we give the constructor the predefined values and labels and features are just the Longitude, Latitudes and pricing which we have provided in index.js. and on gradient descent function, for people who are new, Gradient descent is a first-order iterative optimization algorithm for finding a local minimum of a differentiable function. To find a local minimum of a function using gradient descent, we take steps proportional to the negative of the gradient (or approximate gradient) of the function at the current point. And after that we are creating matrix which would be further in form of tensor which can predict the line on graph.

Figure.9 (Training method)

So here comes the most important part which is training our dataset which are basically partitioning the datasets into training and testing dataset. Here we can see we have used much amount of Lodash function to create and manipulate matrices in the form of tensors. And we have also included gradient descent, MSE and learning rate updating methods. For people who are not aware about the training dataset , it is the most precious dataset which is been modeled upon and then predicted sing the test data.

Figure.10 ( Data normalization and Standardization)

So the above 2 methods are used to manipulate the dataset so that no error occurs when we are predicting or testing the data. What the above line of code will do is that it will eliminate the unwanted datasets and rows which would create ambiguity further.

Figure.11 ( Calculating Error and updating the Learning rate accordingly)

These are the last steps in the linear regression file which helps in solving the error percentage and updating the learning rate which we have manually provided in the index.js file. This will result in optimized output and results.

Figure.12 ( Testing Dataset )

Now comes another important step which is manipulating testing dataset and bringing it to the optimum level of training dataset which can help us predict the Rent or Pricing. testing features will work on the same dataset which we provide it with and which we split in the index.js file

Figure.13 ( Prediction )

Oh yes, Everyone was waiting for this thing which is prediction of our provided values and getting the output. So as we have seen in the index.js file the prediction function which basically this function which is been called upon.

Now this constitute around 50 % of our program.

Wait what completed the Project. Yes you are right that’s all for predicting the Rent or pricing using TensorFlow in JavaScript.

Now the question arises that how to run the program program, It is quite simple Just go to the index.js file and change your values for longitude and latitude , you can even add some other attributes if you want, its all up to you.

open a terminal in VS Code or our most valued start run terminal and write the following command:

Figure.14

typing this in the folder where you have located all the file, make sure that the node modules folder is also included in this folder otherwise it will cause an error.

And that's a wrap.

Yeah yeah I know, below is the link for my repo and my github profile.

Github profile: https://github.com/vrushit

Github repo: https://github.com/vrushit/House-Rent-Prediction

Please comment any error faced, I will surely answer them.

Thank you, Please give a clap and star my repo if you like it.