Skip to content

Machine Learning with Java libraries

Machine Learning with Java libraries

When I think about Machine Learning there comes R and Python into my mind. There’s a nice set of ML libraries and packages that can be used to perform analysis or visualize data in both of those languages. But when it comes to Java ML libraries there aren’t that many. Of course there are nice Java frameworks, but they are mostly designed in such a way that you don’t actually do the coding. So how can a Java programmer easily incorporate ML into their application? I used 2 libraries which allowed me to do exactly that.




Weka is a data mining software written in Java. It provides many Machine Learning algorithms, that can be used out of the box. Weka has a nice clickable GUI, with which the end-to-end analysis can be performed. Additionally it also provides the Java API which allows to use ML in any Java app. There are many algorithms implemented, for supervised and unsupervised ML, for statistics, data analysis, filtering, attribute’s extraction and more. It allows to perform simple analysis without going deep into the algorithm’s implementation and it’s very useful for advanced users as well.



All the libraries can be imported with Maven:

Weka provides nice tutorial to get the work started. Although it takes some time to apply more complex analysis.


Classification tree example

Before Weka version 3.5.5 we had to start the analysis with formatting our data to the ARFF format. We don’t have to do it any more, Weka handles the conversion by itself.

Example is based on public Breast Cancer Wisconsin dataset.


Apache Commons Math

As name suggests Apache Commons Math is a Java library with mathematics and statistics components. Those components can be easily added into the code and used for analysis. You may find there stuff for geometry, algebra, neural networks, matrices, machine learning and more. Examples mentioned on the main page do not cover all the cases that can be done with Commons Math, but fortunately the docs complement that.



Libraries for Apache Commons Math can be imported with Maven:


k-means example

For this example I used publicly available Wine dataset.
In order to use Apache Commons Math clustering algorithm we need to make our data Clusterable. Let’s create a wrapper class WineCluster for the Wine objects, which we create from the dataset.

Now Wine objects can be wrapped by WineCluster objects.

Both Weka and Apache Commons Math are pretty useful and a nice alternative to the clickable frameworks. Although it takes more time to code stuff than to click it all through.

1 thought on “Machine Learning with Java libraries

Leave a Reply

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