Building a Machine Learning Model Using J48 Classifier
Last Updated :
06 Mar, 2023
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.
Similar Reads
Building Your First Machine Learning Model Today, we're exploring a comprehensive guide to building a wine quality prediction model using some of the most powerful tools and libraries available in Python. Whether you're a beginner looking to understand the basics or an experienced data scientist aiming to refine your skills, this guide has s
5 min read
Deploy Machine Learning Model using Flask In this article, we will build and deploy a Machine Learning model using Flask. We will train a Decision Tree Classifier on the Adult Income Dataset, preprocess the data, and evaluate model accuracy. After training, weâll save the model and create a Flask web application where users can input data a
7 min read
Steps to Build a Machine Learning Model Machine learning models offer a powerful mechanism to extract meaningful patterns, trends, and insights from this vast pool of data, giving us the power to make better-informed decisions and appropriate actions.Steps to Build a Machine Learning Model In this article, we will explore the Fundamentals
9 min read
Creating a simple machine learning model Machine Learning models are the core of smart applications. To get a better insight into how machine learning models work, let us discuss one of the most basic algorithms: Linear Regression. This is employed in predicting a dependent variable based on one or multiple independent variables by utilizi
3 min read
Learning Model Building in Scikit-learn Building machine learning models from scratch can be complex and time-consuming. Scikit-learn which is an open-source Python library which helps in making machine learning more accessible. It provides a straightforward, consistent interface for a variety of tasks like classification, regression, clu
8 min read