How to make predictions using Machine Learning in .NET and Python

How to make predictions using Machine Learning in .NET

Jacob Malling-Olesen is part of our Software Craftsman Program in which we ask our graduates and residents to write about different topics as part of their training to become Software Craftsmen.

In part 1 we saw that raw gathered data is rarely ready for use in ML. We then showed how to clean and transform the data to make it ready for Machine Learning.

Data Selection

It is now time to select the features to build the model upon. This is step 4 in the figure from part 1. In ML, features are the data values used for the prediction. The features being selected can be all available, but it can also be just one. This all depends on the performance of the trained model as this change depending on the selected features.

In the C# part this is where ML.NET starts to be used. First, a reader is specified where the header names are entered as the column list.

These names are then used when selecting which of these features to train the model with. After reading the data an EstimatorChain is created. This is basically a pipeline where features are selected using ML.NET transforms functions. In this pipeline it is also possible to do other kinds of transforms on the data, but since the data is already transformed, they are not used. Therefore, this pipeline is just a simple one where the feature to be predicted is selected and added to a column named “Label” while the features to predict is added to a column named “Features”.

In Python a library called Pandas is used. This is used for reading the data from disk and then in a list of strings it is specified which features to use while a single string specifies the value we predict.

Since scikit-learn needs us to specify which value is the label (“y”) and which value is the features (“X”) these are specified.

In both examples it is easy to remove or add the features that are used in the model without changing anywhere else in the code. So, there is not much difference on how to do this part in ML.NET or Python.

Model training

Figure 1: Step 5 of the ML Process

Now it is time to train the model as shown on figure 1. Since the goal is to predict a numerical value, a Regression training algorithm is chosen.

ML.NET contains some implementations of regression models. So first a trainer is selected and appended to the pipeline. When adding this it is possible to specify the featureColumn and the labelColumn (“Features” and “Label” by default) as well as some hyperparameters to tune the algorithm. Trying out the different algorithms with the right hyperparameters can be cumbersome only a few models with default parameters were tested before selecting the FastTreeTweedie model.

The model is validated by testing how it performs on the data it was trained upon. This validation gives an average error of 0.72 degrees Celsius. Comparing this to general weather predictions it is not good, but not that bad either. The general way to determine if a model is “good enough” comes down to the problem it is trying to fix. For temperature prediction an average error of 0.72 might be fine while the same error for sensor data prediction could be unacceptable. Since this is just a small sample test, the model is deemed “good enough” to move on.

Model evaluation

The evaluation process resembles validation a lot, however there is one big difference. The data for the evaluation must never be part of the training. The way the model is evaluated is by performing the prediction on the test set. The predicted temperature is then compared to the correct temperature from the test set.

In ML.NET the Transform function is used to do the prediction. This transformed data is then given to a Regression Evaluation to get some evaluation metrics like Mean Absolute Error.

The evaluation gives an average error of 0.52 degrees Celsius. Since it is not much worse than the validation average error it seems the model is not overfitting. Overfitting is when a model reaches close to perfect on the training data and then does not generalize well to new unseen input. I.e. the model only performs well in testing, not in production.

In Python the predict function is used on the features of the test data. This returns a list of temperature values that can be compared with the label of the test data. This comparison can be evaluation metrics like Mean Absolute Error.

The evaluation gives an average error of 0.36 degrees Celsius. Again, the value is not worse than the validation average error.

Instead of relying on raw data values like Mean Absolute Error we can also plot our true data and our predicted data next to each other to visualize the comparison. In Python this can be done using PyPlot. Adding the true evaluation values (blue) and the predicted (orange) to the same plot it can be hard to see the difference, so we need to zoom in:

Here it is possible to see some difference. Generally, the predictions seem to follow the true values. However, at some points the values have the same increase and decrease but are shifted a little along the x-axis. This means the prediction on the given time date would be a little off resulting in more error in the prediction when comparing it to the true value.

A problem with these evaluations is that for weather prediction just looking ahead 1 hour doesn’t seem that interesting. It would be much more interesting to be able to predict multiple hours ahead using its own temperature predictions as input and see how well the models perform on this task. So, implementing a specific evaluation function to evaluate the performance each hour up to five hours ahead seems like a good idea. Since the models uses multiple parameters, all other features but the temperatures are faked in the evaluation as they would otherwise have to be predicted as well by other models or more sophisticated models able to return multiple values.

In ML.NET this is a function that takes a PredictionFunction generated from the trained model and then a data file containing the test set. Then going through five timeseries datapoints at a time it calculates the FutureTemperature and uses this value as the temperature value in the next datapoint.

The table again shows the tendency of the increasing error the further into the future the prediction goes.

Plotting these data in a similar fashion as above:

Figure 4: First hourPart 2 Figure 5: First hour zoomed in

Figure 6: Second hourFigure 7: Second hour zoomed in

Figure 8: Third hourFigure 9: Third hour zoomed in

Figure 10: Fourth hourFigure 11: Fourth hour zoomed in

Figure 12: Fifth hourFigure 13: Fifth hour zoomed in

These plots show the same tendency as the tables. And in the fifth plot there is not much coherence between the blue and orange lines.

These evaluations show how important it is to fit the evaluations to the task that should be completed. Using the standard evaluation, it could be tempting to release the model, as an average error of half a degree Celsius in temperature predictions does not seem that bad. This evaluation would also deem the model not “good enough”. Therefore, going back to step 5 and selecting another training algorithm or even back to step 4 and select other data features would be the next step in this process. Should it happen that testing all kinds of different training algorithms and data features still doesn’t provide a model that performs “good enough” gathering more data or different data could also be a way to tackle this.

All in all, there is not much difference in the coding style of ML.NET and Python with Pandas and scikit-learn. The frameworks also work similarly and uses many of the same terminologies. One of the biggest differences is the number of training algorithms in the two frameworks where scikit-learn takes the lead. However, ML.NET is being worked on extensively with new updates coming monthly. So, the choice of which framework to select comes down to which language best fits the system that should incorporate machine learning as well as other organizational demands such as performance and maintenance.

About the author

Jacob Malling-Olesen is a computer scientist specialised in Machine Learning. He is now a graduate attending our Software Craftsman Program. We encourage our graduates to write about the things they learn as we believe that software craftsmanship is based on continuous learning.