In the previous article in this Machine Learning series, we looked at the Apache® Ignite™ Machine Learning Grid. Now let’s take the opportunity to drill-down further into some of the Machine Learning algorithms that are supported in Apache Ignite and try out some examples using popular datasets.

If we search for suitable datasets to use, we can find many that are available. However, one dataset that is a good candidate for Linear Regression is House Prices. Very conveniently, we can find suitable data available through the UCI web site.

In this article we will train a Linear Regression model and calculate the R^{2} score.

Some data preparation is required to get the data into a suitable format for Apache Ignite. This is often what a Data Scientist may spend time doing.

First, we need to take the raw data and split it into training data (80%) and test data (20%). At the time of writing this article, Ignite does not support dedicated data splitting, but this functionality is on the roadmap for a future release. In the meantime, there are many free and open source tools available that can perform this type of data splitting or we could code this ourselves in one of the programming languages supported by Ignite. For this article we'll use Scikit-learn, and my colleague Anton Dmitriev at GridGain very kindly wrote the following code to achieve this task:

This code takes the dataset available from the UCI web site, performs the data split and then calculates the R^{2} score. The value returned is 0.745021053016975, or 74.5%. Later, we’ll compare this against the value returned by Ignite.

With our training and test data ready, we can start coding the application. My colleague Anton very kindly wrote a Java application for use with Ignite and you can download it from GitHub if you would like to follow along. Our algorithm is therefore:

- Read the training data and test data
- Store the training data and test data in Ignite
- Use the training data to fit the Linear Regression model
- Apply the model to the test data
- Determine the R
^{2}score of the model

Since the dataset is quite small, we could just load it into standard Java data structures and run Linear Regression directly from within the Java program. Alternatively, we could load the data into Apache Ignite storage and then run Linear Regression on the stored data. The advantage of using Apache Ignite storage is that the data will be distributed across an entire cluster and, therefore, we will be performing distributed training. For large datasets, using Ignite storage could therefore have great benefits. In our example we will load the data into Ignite storage.

## Read the training data and test data

We have two CSV files to read in - one for the training data and the other for the test data. We can use the following code to read values in from the CSV files:

The code simply reads the data line-by-line and splits fields on a line by the CSV field separator. Each field value is then converted to double format and then the data are stored in Ignite.

## Store the training data and test data in Ignite

The previous code stores data values in Ignite. To use this code, we need to create the Ignite storage first, as follows:

## Use the training data to create the Linear Regression model

Now that our data are stored, we can create the trainer as follows:

and fit a linear model to the training data, as follows:

Ignite stores data in a Key-Value (K-V) format, so the above code uses the value part. The target value is *Price* and the features are in the other columns.

## Apply the model to the test data

Next, we are ready to check the test data against the trained linear model. On the Apache Ignite Machine Learning roadmap, there is a plan to provide built-in score calculators. For now, we can do the following:

Here we calculate the residual sum of squares (u) and the total sum of squares (v).

## Determine the R^{2} score of the model

We can find the value of R^{2} as 1 - u / v:

This gives us the value 0.7450194305206714 or 74.5%. This percentage is identical to what we achieved earlier with Scikit-learn.

## Summary

Apache Ignite provides a library of Machine Learning algorithms. Through a Linear Regression example, we have seen the ease with which we can create a model, test the model and determine the R^{2} score of the model. We can now also use this model to make predictions.

Today, many Machine Learning tools are available, but they cannot scale beyond a single node and can only handle small quantities of data. In contrast, the benefits that Ignite provides are its ability to scale both:

- The size of the cluster (hundreds or thousands of machines).
- The quantity of data stored (hundreds of Gigabytes, Terabytes or even Petabytes).

Ignite can therefore run Machine Learning at scale. It can truly manage Machine Learning on Big Data using distributed processing.

In the next part of this Apache Ignite Machine Learning series, we’ll look at another Machine Learning algorithm. Stay tuned!