Data Mining Model Creation with Tensorflow.js

 



Introduction to Machine Learning

Humans learns from their past experiences while Machines follow instructions given by HumansHow ever we can train / learn machines to follow instructions by learning from past experiences which we called as “Machine Learning”. 

So… how do we train or learn machines to follow instructions since machines doesn’t have a brain to self thinking or doing the analysing process?  We need to provide data (past data records for specific scope) as inputs to the machine and then it will analyse those past  data through a specific process (data mining model) and then will output the relevant output based on the input data.

So, to improve the accuracy of the model what should we do?
The Answer is :  If we provide more data, we’ll  get a better model which outputs data with higher accuracy.

Application of Machine Learning: Health  Care, Sentiment Analysis (Likes or Dislikes), Fraud Detection, E-Commerce (supermarkets), Time series or data prediction analysis.


Supervised Learning

  • In Supervised Learning, before doing the analysis, it builds the model and then we can apply the algorithm to estimate the parameters of the model.
  • Some common examples for supervised learning are:
    • Regression (Simple Linear Regression, Multiple Regression, Logistic Regression)
    • Classification (Binary, Multinomial Classification)
    • Decision Tree
    • Bayesian Classification
    • Neural Networks

Unsupervised Learning

  • In Unsupervised learning, we don’t create a model before analysis is done, instead we apply the algorithm directly to the dataset and observe the result.
  • Then a model may be created according to the basis of the obtained results.
  • Some common examples for unsupervised learning are:
    • Clustering
    • Association

Other Data Mining Techniques

  • Association Rule Mining
  • Prediction
  • Time Series Analysis
  • Sequential Patterns

Difference Among Machine Learning, Deep Learning and AI

  • Artificial Intelligence (AI):
    • Basically AI means the ability of machines to function like the human brain.
  • Machine Learning (ML): 
    • Machine Learning is a subset of AI.
    • It consists of techniques that enable computers to figure things out from the data (past data) and deliver Artificial Intelligence applications.
  • Deep Learning:
    • Deep Learning is a subset of ML.
    • It enables computers to solve more complex problems.


What is Data Mining?

  • Data Mining is the process of finding anomalies, patterns and correlations within large data sets to predict outcomes or to take data-driven decisions (specially in businesses). 
  • It helps entrepreneurs, researchers, and individuals to extract valuable information from huge data sets. (data => Information)
  • Data Mining is also called as Knowledge Discovery in Database (KDD). 
  • Data Mining includes several major processes such as:

    • Data Cleaning
    • Data Integration
    • Data Selection
    • Data Transformation
    • Data Mining
    • Patterns Evaluation
    • Knowledge Presentation

Advantages and Disadvantages of Data Mining

  • Advantages
    • It enables organizations to obtain knowledge based data.
    • Data mining is cost effective compared to other statistical data applications.
    • It helps decision-making  process of an organization.
    • It facilitates to automatically discover hidden patterns as well as prediction of trends and behaviours.
    • It allows new users quickly and easily  analyse large no of data in a short time of period.
  • Disadvantages
    • Many Data Mining analytics software is difficult and needs advance training to work on.
    • Different Data Mining Instruments operate in different ways due to the different algorithm types used in their design. Therefore, selecting a proper data mining tool may be challenging.
    • The data mining techniques are not precise, so, it may lead to severe consequences in certain conditions.

Types of Data Mining

Data Mining can be performed using any kind of following types of data.
    • Relational Database (Collection of multiple data sets formally organized by  tables, records, and columns which refers to different tables)
    • Data Warehouse (Collects various kind of sources within a given organization to provide meaningful business insights)
    • Data Repositories (Group of Databases of a given Organization)
    • Object-Relational Database (Languages which  supports ORM such as C++, Java, C# etc.)
    • Transactional Database (DBMS which has the potential to undo a database transaction  if  it is not performed appropriately)


Applications of Data Mining

Here are some of the examples for applications of data mining.
  • Health Care
  • Fraud Detection
  • Education
  • Financial Banking
  • Lie Detection
  • Share Market Analysis
  • Supermarket Goods Storing Analysis

Data Pre Processing

  • During the Data Mining Process Data Pre Processing is one of the main stage we have to pass through.
  • Among the dataset which we are looking to evaluate may contain some Noisy/Incomplete Data (Inaccurate or Unreliable), Missing Data and also may have Complex data.
  • So, the data should be Pre Processed before using those data to train the model because it may lead the model to predict inaccurate results.


Introduction to Tensorflow.js

  • TensorFlow.js is an end-to-end free and open source library/platform which helps us to develop and train Machine Learning models. 
  • It has a comprehensive, flexible ecosystem of tools, libraries and community resources that lets researchers and developers to easily build and deploy ML powered applications.
  • It provides feature of easily using intuitive high-level APIs like Keras with eager execution, which makes for immediate model iteration and easy debugging.
  • It allows to easily train and deploy models in the cloud, on-prem, in the browser, or on-device no matter what language you use.
  • It allows a simple and flexible architecture to take new ideas from concept to code, to state-of-the-art models, and to publication faster.  

Sample Model Creation with Tensorflow.js

Here is a simple prediction model creation using tenserflow using the height and weight of a given audience combination of male and female as the dataset which use to predict the gender according to the inputted height and the weight of the given person.

Let's use a Node.js project to perform this task. You may refer the below package.json file to identify all the dependancies used and the other project details. 

package.json

{
  "name": "com.org.devsdebugger.tensorflow",
  "version": "1.0.0",
  "description": "Sample Machine Learning Model Creation Using Tensorflow.js",
  "main": "server.js",
  "scripts": {
    "start": "cross-env NODE_ENV=production node server",
    "dev": "cross-env NODE_ENV=development nodemon server"
  },
  "author": "devs'-debugger",
  "license": "MIT",
  "dependencies": {
    "@tensorflow/tfjs": "^3.3.0",
    "express": "^4.17.1",
    "nodemon": "^2.0.7"
  }
}


GenderPredictionModel.js

// Import Tenserflow
const tf = require("@tensorflow/tfjs");

class GenderPredictionModel {

    compileModel() {

        // Uses a Sequential Model
        const model = tf.sequential();

        // Input Layer
        model.add(tf.layers.dense({
            units: 2,
            inputShape: [2]
        }));

        // Dense Layer 1
        model.add(tf.layers.dense({
            units: 16,
            activation: 'relu'
        }));

        // Dropout Layer 1
        model.add(tf.layers.dropout({
            args: 0.2
        }));

        // Dense Layer 2
        model.add(tf.layers.dense({
            units: 8,
            activation: 'relu'
        }));

        // Dropout Layer 2
        model.add(tf.layers.dropout({
            args: 0.2
        }));

        // output Layer
        model.add(tf.layers.dense({
            units: 1,
            activation: 'sigmoid'
        }));

        model.compile({
            loss: 'meanSquaredError',
            optimizer: 'sgd',
            metrics: ['accuracy']
        });

        return model;
    }

    executeModel() {
        const model = this.compileModel();
         // Input Data Sets
        const inputXLayerData = [
            // Male
            [73.84701702, 241.8935632],
            [68.78190405, 162.3104725],
            [74.11010539, 212.7408556],
            [71.7309784, 220.0424703],
            [69.88179586, 206.3498006],
            [67.25301569, 152.2121558],
            [68.78508125, 183.9278886],
            [68.34851551, 167.9711105],
            [67.01894966, 175.9294404],
            [63.45649398, 156.3996764],
            [71.19538228, 186.6049256],
            [71.64080512, 213.7411695],
            [64.76632913, 167.1274611],
            [69.2830701, 189.4461814],
            [69.24373223, 186.434168],
            [67.6456197, 172.1869301],
            [72.41831663, 196.0285063],
            [63.97432572, 172.8834702],
            [69.6400599, 185.9839576],
            [67.93600485, 182.426648],

            // Female
            [58.91073204, 102.0883264],
            [65.23001251, 141.3058226],
            [63.36900376, 131.0414027],
            [64.47999743, 128.1715112],
            [61.79309615, 129.781407],
            [65.96801895, 156.8020826],
            [62.85037864, 114.9690383],
            [65.65215644, 165.0830012],
            [61.89023374, 111.6761992],
            [63.67786815, 104.1515596],
            [68.10117224, 166.5756608],
            [61.79887853, 106.233687],
            [63.37145896, 128.1181691],
            [58.89588635, 101.6826134],
            [58.4382491, 98.19262093],
            [60.80979868, 126.9154633],
            [70.12865283, 151.2542704],
            [62.25742965, 115.7973934],
            [61.73509022, 107.8668724],
            [63.05955669, 145.5899291]
        ];

        // Declare Male as 0 and Female as 1
        const inputYLayerData = [
            [0], [0], [0], [0], [0],
            [0], [0], [0], [0], [0],
            [0], [0], [0], [0], [0],
            [0], [0], [0], [0], [0],

            [1], [1], [1], [1], [1],
            [1], [1], [1], [1], [1],
            [1], [1], [1], [1], [1],
            [1], [1], [1], [1], [1],
        ];

        // xs - input layer
        const xs = tf.tensor2d(inputXLayerData);

        // ys - input layer
        const ys = tf.tensor(inputYLayerData);

        model.fit(xs, ys, {epochs: 1000}).then(r => {
            // Evaluate Hight and Weight as (58.89588635,101.6826134)
// to check whether he/she is male or female
            const data = tf.tensor2d([[58.89588635,101.6826134]]);

            // Evaluate the Predicted Result
            let prediction = model.predict(data);
            // Get the Result to be Printed
            const result = prediction.print();
            // Print the Result
            console.log(result);
            // Declare as Male or Female according the result value
            console.log(result > 0.5 ? 'Female' : 'Male');
        });
    }
}

module.exports = GenderPredictionModel;


Post a Comment

0 Comments