ANGADJAVA Code Samples Bytes Example: Using Java and Machine Learning to Classify Iris Flowers

Example: Using Java and Machine Learning to Classify Iris Flowers

0 Comments 9:22 pm

pictures, by machine, to learn-3853117.jpg

In this example, we will use Java and machine learning to classify iris flowers based on their petal and sepal length and width. We will use the popular iris dataset, which contains measurements for 150 iris flowers of three different species: setosa, versicolor, and virginica.

We will use the Weka library, which is a popular machine learning library for Java. Weka provides a wide range of machine learning algorithms and tools for data preprocessing, visualization, and evaluation.

Step 1: Load the Dataset

The first step is to load the iris dataset into our Java program. We can use the DataSource class in Weka to load the dataset from a file or URL. Here’s an example:

import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;

// Load the iris dataset
DataSource source = new DataSource("");
Instances data = source.getDataSet();

Step 2: Preprocess the Dataset

The next step is to preprocess the dataset. We need to convert the categorical class attribute (the species of the iris flower) into numerical values, and we also need to split the dataset into training and testing datasets. Here’s an example:

import weka.filters.Filter;
import weka.filters.unsupervised.attribute.NumericToNominal;
import weka.filters.unsupervised.instance.Randomize;
import weka.filters.unsupervised.instance.RemovePercentage;

// Convert the class attribute to numerical values
NumericToNominal filter = new NumericToNominal();
data = Filter.useFilter(data, filter);

// Randomize the dataset
Randomize randomize = new Randomize();
data = Filter.useFilter(data, randomize);

// Split the dataset into training and testing datasets
RemovePercentage split = new RemovePercentage();
Instances trainData = Filter.useFilter(data, split);
Instances testData = Filter.useFilter(data, split);

Step 3: Train a Machine Learning Model

The next step is to train a machine learning model on the training dataset. We will use the J48 decision tree algorithm, which is a popular algorithm for classification tasks. Here’s an example:

import weka.classifiers.trees.J48;

// Train a J48 decision tree on the training dataset
J48 tree = new J48();

Step 4: Evaluate the Model

The final step is to evaluate the performance of the model on the testing dataset. We will use the Evaluation class in Weka to evaluate the model and calculate performance metrics such as accuracy, precision, and recall. Here’s an example:

import weka.classifiers.Evaluation;

// Evaluate the model on the testing dataset
Evaluation eval = new Evaluation(trainData);
eval.evaluateModel(tree, testData);

// Print the performance metrics

Step 5: Make Predictions

Once we have trained and evaluated the model, we can use it to make predictions on new data. Here’s an example:

import weka.core.DenseInstance;

// Create a new instance to classify
DenseInstance newInstance = new DenseInstance(4);
newInstance.setValue(0, 5.1);
newInstance.setValue(1, 3.5);
newInstance.setValue(2, 1.4);
newInstance.setValue(3, 0.2);

// Classify the new instance
double classIndex = tree.classifyInstance(newInstance);
String className = data.classAttribute().value((int) classIndex);
System.out.println("Predicted class: " + className);

This code creates a new instance with petal and sepal length and width values of 5.1, 3.5, 1.4, and 0.2, and uses the trained decision tree model to classify it as one of the three iris species.


In this example, we used Java and the Weka library to classify iris flowers based on their petal and sepal length and width. We loaded the dataset, preprocessed it, trained a J48 decision tree model, evaluated its performance, and made predictions on new data. This is just one example of how Java and machine learning can be used together to solve real-world problems. With the wide range of machine learning algorithms and tools available in Java, the possibilities are endless.

Leave a Reply

Your email address will not be published. Required fields are marked *