Main navigation

Add To Bookmark

An Easy Machine Learning Example in Java

By NIIT Editorial

Published on 09/08/2021

6 minutes

Machine learning plays an important role in the fields of academia and industry by bringing remarkable promises to these fields. Moreover, machine learning has expanded its engagement among a comprehensive list of applications, i.e., image, speech recognition, pattern recognition, optimization, natural language processing, and many more.

Machine learning contains four main techniques: regression, classification, clustering, and reinforcement learning. These techniques solve problems with different approaches in two different forms, i.e., supervised and unsupervised learning.

The data that needs to be labeled and prepared ahead of training the model is known as Supervised learning. In contrast, unlabeled data, which is handy and has unknown characteristics, is known as Unsupervised learning.

Machine Learning Libraries in Java:

Given below is a list of well-known libraries used in Java for Machine learning. The article aims to focus on the Weka library and its use in Java.

  • Weka
  • Apache Mahout
  • Deeplearning4j
  • Mallet
  • Spark MLlib
  • The Encog Machine Learning framework
  • MOA

Apache Mahout:

Apache Mahout uses the MapReduce paradigm and provides a scalable machine learning library. Mahout can also be used in classification, clustering, collaborating, and filtering. In addition, it provides algorithms like collaborating filtering, which facilitates the scalability of building a model quickly and efficiently. 


Deeplearning4j is yet another library in Java that focuses on deep learning. It is a great open-sourced library written in Java and Scala that can be integrated with Hadoop and Spark, which provides high processing capabilities.


Mallet is one of few specialized toolkits that is abbreviated as Machine Learning for Language Toolkit used for natural language processing. It has the capability of modeling,  clustering, classification, and information extraction. In addition, Mallet also helps in machine learning models to process textual documents.


It helps in accelerating the scalability and the overall performance of processing massive amounts of data. To be able to run on Spark and plugged into Hadoop workflows, Spark MLlib has high power algorithms. 

The Encog Machine Learning Framework:

This framework has libraries for building SVM, NN, Bayesian Networks, HMM, and Genetic algorithms used in Java and the C# framework of machine learning. 


Massive Online Analysis provides libraries for outlier and drifts detection. It also provides algorithms for classification, regression, recommendations, and clustering. MOA is designed on a stream of produced data for real-time processing.


Weka was developed by the University of Waikato in New Zealand, which is an open-sourced library. This form of a library is very well-known for general-purpose machine learning, written in Java.

The given ‘Hello World' example of machine learning in Java gives a clear picture of understanding a better taste of machine learning in Java.

Environment: Java 1.6+ and Eclipse

Step 1: Download Weka Library

Download Weka library:

Now, download, unzip the file, add weka.jar to the Java library path project in Eclipse.

Step 2: Prepare Data

Create “weather.txt” file by using the following format:

ML in Java


The above dataset is from the weka download package that is located at "/data/weather.numeric.arff".

The name of the file extension is “arff” but it can be used as “txt”.

Step 3: Training and Testing using Weka

The given code uses a set of classifiers provided by Weka. The code trains the model on the given database and tests with the help of 10-split cross-validation.

ML in JavaML in Java


ML in JavaML in Java

This is how the package view of your project must look like:

ML in Java


When it comes to the development of machine learning, anyone can look up the choices of various development languages. The subject is vast, so learning and practicing each machine learning technique will help become a Machine Learning Expert. Take into consideration each technique and implement the algorithms used in it with the help of any programming language of your choice. Practicing one technique at a time helps in applying the technique effectively. You can get a better insight into Java with the Java Developer Program and Machine Learning with Advanced PGP in Data Science and Machine Learning (Full Time).

Advanced PGP in Data Science and Machine Learning (Full-Time)

Become an industry-ready StackRoute Certified Data Science Professional.

Job Assured Program*

Practitioner Designed