Machine Learning in Java: Libraries for Building Intelligent Systems
Although Python remains the dominant language in the field of machine learning (ML), Java holds a strong position in enterprise development. Many large companies use Java to build high-load ML systems due to its performance, strong typing, and rich ecosystem. In this article, we will review the key machine learning libraries for Java that will help you integrate ML into your projects.
1. Deeplearning4j (DL4J) — Neural Networks and Deep Learning
Deeplearning4j is the leading deep learning library for Java and Scala. It supports the creation, training, and deployment of neural networks of various architectures: from simple perceptrons to complex convolutional and recurrent networks.
Key Features:
- GPU support (CUDA) for accelerated computation
- Integration with Apache Spark and Hadoop for distributed training
- Flexible architecture for building custom layers and models
- Built-in visualization tools (DL4J UI)
Example: A Simple Neural Network for Classification
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;import org.deeplearning4j.nn.conf.NeuralNetConfiguration;import org.deeplearning4j.nn.conf.layers.DenseLayer;import org.deeplearning4j.nn.conf.layers.OutputLayer;import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;import org.deeplearning4j.optimize.listeners.ScoreIterationListener;import org.nd4j.linalg.activations.Activation;import org.nd4j.linalg.dataset.DataSet;import org.nd4j.linalg.factory.Nd4j;import org.nd4j.linalg.lossfunctions.LossFunctions;
public class SimpleDl4jExample { public static void main(String[] args) { // Network configuration MultiLayerConfiguration config = new NeuralNetConfiguration.Builder() .seed(123) .updater(new org.nd4j.linalg.learning.config.Adam(0.01)) .list() .layer(new DenseLayer.Builder().nIn(2).nOut(10) .activation(Activation.RELU).build()) .layer(new OutputLayer.Builder(LossFunctions.LossFunction.MSE) .nIn(10).nOut(1) .activation(Activation.SIGMOID).build()) .build();
MultiLayerNetwork model = new MultiLayerNetwork(config); model.init(); model.setListeners(new ScoreIterationListener(100));
// Training data (XOR) DataSet data = new DataSet( Nd4j.create(new double[][]{{0,0},{0,1},{1,0},{1,1}}), Nd4j.create(new double[][]{{0},{1},{1},{0}}) );
// Training for (int i = 0; i < 1000; i++) { model.fit(data); }
// Prediction System.out.println(model.output(data)); }}2. Weka — Classic ML Toolkit
Weka (Waikato Environment for Knowledge Analysis) is one of the oldest and most popular machine learning libraries for Java. It provides a wide range of algorithms for classification, regression, clustering, and data visualization.
Advantages:
- Graphical interface (Explorer) for rapid experimentation
- Over 100 implemented algorithms
- Easy integration into Java applications
- Support for data preprocessing and filtering
Example: Classification Using a Decision Tree
import weka.classifiers.trees.J48;import weka.core.Instances;import weka.core.converters.ConverterUtils.DataSource;
public class WekaExample { public static void main(String[] args) throws Exception { // Loading data from an ARFF file DataSource source = new DataSource("i Also in library
Email Sending in TypeScript: A Complete Guide with nodemailer and SMTP
Rust Application Monitoring: Overview of Tools and Libraries
Swagger/OpenAPI Generation for C++: Best Tools and Libraries
Working with Dates in PHP: Carbon Library
ORM for Kotlin: Comparison of Exposed, Room, and Ktorm with Examples