Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Use Weka Java API?

  • Last Updated : 21 Sep, 2021

To use the weka API you need to install weka according to your operating system. After downloading the archive and extracting it you’ll find the weka.jar file. The JAR file contains all the class files required i.e. weka API. Now we can find all the information about the classes and methods in the Weka Java API documentation. We need to add this jar as a classpath to our program.

Also, let us discuss the classpath before landing upon the implementation part. So classpath is something that tells the JDK about the external libraries (user class file). In order to add a classpath the recommended way is to use -cp option of JDK commands. If you are using any framework then the classpath can be added to the respective manifest file.

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.



Example:



Java




// Java Program to Illustrate Usage of Weka API
 
// 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
// BreastCancer
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Try block to check for exceptions
        try {
 
            // Create J48 classifier by
            // creating object of J48 class
            J48 j48Classifier = new J48();
 
            // Dataset path
            String breastCancerDataset
                = "/home/droid/Tools/weka-3-8-5/data/breast-cancer.arff";
 
            // Creating 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);
 
            // Evaluating by creating object of Evaluation
            // class
            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 handle the exceptions
        catch (Exception e) {
 
            // Print message on the console
            System.out.println("Error Occurred!!!! \n"
                               + e.getMessage());
        }
    }
}

Output: 

After coding your model using the weka API you can run the program using the following commands

$ javac -cp weka-3-8-5/weka.jar program.java

$ java -cp .:weka-3-8-5/weka.jar program

The weka-3-8-5/weka.jar is the path to the jar file available in the installation.

This will be the desired output generated as shown below: 

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :