Wednesday, November 20, 2024
Google search engine
HomeLanguagesJavaBuilding a Machine Learning Model Using J48 Classifier

Building a Machine Learning Model Using J48 Classifier

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.

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 block to 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 for rexceptions
        catch (Exception e) {
 
            // Print and display the display message
            // using getMessage() method
            System.out.println("Error Occurred!!!! \n"
                               + e.getMessage());
        }
 
        // Display message to be printed ion console
        // when program is successfully executed
        System.out.print("Successfully executed.");
    }
}


Output:

Successfully executed.

RELATED ARTICLES

Most Popular

Recent Comments