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
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;
public class BreastCancer {
public static void main(String args[])
{
try {
J48 j48Classifier = new J48();
String breastCancerDataset
= "/home/droid/Tools/weka-3-8-5/data/breast-cancer.arff" ;
BufferedReader bufferedReader
= new BufferedReader(
new FileReader(breastCancerDataset));
Instances datasetInstances
= new Instances(bufferedReader);
datasetInstances.setClassIndex(
datasetInstances.numAttributes() - 1 );
Evaluation evaluation
= new Evaluation(datasetInstances);
evaluation.crossValidateModel(
j48Classifier, datasetInstances, 10 ,
new Random( 1 ));
System.out.println(evaluation.toSummaryString(
"\nResults" , false ));
}
catch (Exception e) {
System.out.println( "Error Occurred!!!! \n"
+ e.getMessage());
}
System.out.print( "Successfully executed." );
}
}
|
Output:
Successfully executed.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...