Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Building a Machine Learning Model Using J48 Classifier

  • Last Updated : 22 Sep, 2021

What is the J48 Classifier?

J48 is a machine learning decision tree classification algorithm based on Iterative Dichotomiser 3. It is very helpful in examine the data categorically and continuously.

Attention reader! Don’t stop learning now. Get hold of all the important Machine Learning Concepts with the Machine Learning Foundation Course at a student-friendly price and become industry ready.



Note: To build our J48 machine learning model we’ll use the weka tool.



What is Weka?

Weka is an open-source tool developed by the University of Waikato, New Zealand licensed under GNU public license. You can download weka on any operating system. Weka has GUI and APIs available to use.

Steps to follow:

Step 1: Create a model using GUI

Step 2: After opening Weka click on the “Explorer” Tab

Step 3: In the “Preprocess” Tab Click on “Open File” and select the “breast-cancer.arff” file which will be located in the installation path, inside the data folder.



In this tab, you can view all the attributes and play with them.

Step 4: In the “Classify” tab click on the choose button. Now under weka/classifiers/trees/ select J48

Step 5: Now one can click on the J48 Classifier selection and play around with it like changing batch size, confidence factor, etc. There under “Test Options” we’ll use the default cross-validation option as folds 10 and click on start.

Implementation:

Now we are done with discussing that Weka has Java API that you can use to create machine learning models so di now let us create a model using API

Example

Java




// Java Program for Creating a Model Based on J48 Classifier
  
// Importing required classes
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Random;
import weka.classifiers.Evaluation;
import weka.classifiers.trees.J48;
import weka.core.Instances;
  
// Main class
public class BreastCancer {
  
    // Main driver method
    public static void main(String args[])
    {
  
        // Try bloc kto check for exceptions
        try {
  
            // Creating J48 classifier
            J48 j48Classifier = new J48();
  
            // Dataset path
            String breastCancerDataset
                = "/home/droid/Tools/weka-3-8-5/data/breast-cancer.arff";
  
            // Create bufferedreader to read the dataset
            BufferedReader bufferedReader
                = new BufferedReader(
                    new FileReader(breastCancerDataset));
  
            // Create dataset instances
            Instances datasetInstances
                = new Instances(bufferedReader);
  
            // Set Target Class
            datasetInstances.setClassIndex(
                datasetInstances.numAttributes() - 1);
  
            // Evaluation
            Evaluation evaluation
                = new Evaluation(datasetInstances);
  
            // Cross Validate Model with 10 folds
            evaluation.crossValidateModel(
                j48Classifier, datasetInstances, 10,
                new Random(1));
            System.out.println(evaluation.toSummaryString(
                "\nResults", false));
        }
  
        // Catch block to check fo rexceptions
        catch (Exception e) {
  
            // Print and display the display message
            // using getMessage() method
            System.out.println("Error Occured!!!! \n"
                               + e.getMessage());
        }
  
        // Display message to be printed ion console
        // when program is successfully executed
        System.out.print("Successfully executed.");
    }
}

Output:

Successfully executed.



My Personal Notes arrow_drop_up
Recommended Articles
Page :