Poisson Regression using F# and ML.NET

linear regression using F# and ML.NET

I have already written articles about using ML.NET, one was about the Naive Bayes Classifier implementation and the other one about the L-BFGS algorithm. In these articles, I showed examples in C#. Today I decided to write to them in F#. It’s a language that combines the features of procedural and functional language and is part of the .NET platform. If you have not had contact with this before, you will have the opportunity. I will introduce you to the subject of Regression, which is an important element of machine learning algorithms and very often used task of supervised learning.

Theoretical introduction

I think it is best to explain it by some example. So let’s assume that you own a sunglasses store and you have noticed that the number of sunny days in a month has a pretty high impact on sales. You checked in the weather forecast that next month meteorologists predict that there will be 12 sunny days. Sales statistics from recent years for several months with the number of sunny days per month are as follows:

Average Sunny DaysAverage Sunglasses Sold
20102
1585
1067
1796
536
945
1374

Here is a diagram of the above data:

diagram 1 linear regression article

Now the question is, how can you predict what the next month’s sales will be based on this data? Well, you can take this as an example of the Poisson distribution. This is a discrete distribution that describes the probability of successive events with a given frequency at a given time. These events are independent of each other. It is described by the following formula:

You can treat the number of glasses sold as a Poisson variable, where the predictor will be the number of sunny days in a given month and month is the explanatory variable. Therefore, the Poisson Regression model is expressed by the formula:

where W is a vector of unknown parameters informing about the direction and strength of the influence of explanatory variables.

I think I have made you aware of what this regression is about and when it is applied. However, let me not go into further details because I do not want to bore you. I wanted to briefly show you what it is about.

Sign up for the newsletter to keep up to date with new articles!

Implementation part

The first thing you need to do is create a .NET Core console application project in F# and then download the ML.NET library from NuGet Packages.

New project in F#

Now you can declare the necessary libraries and paths to the testing and training sets.

Then you can create classes that correspond to the attributes in the dataset. The created classes are visible below:

After that, you can go on to load the dataset, adapt the model structure to the standards adopted by the ML.NET library and creating a training pipeline. Here you also choose a kind of regression in the form of Poisson Regression.

Finally, you can start the learning process. To check the effectiveness of learning, it is also worth checking the results of the metrics.

There is still code to write that will be responsible for prediction value or the number of sunglasses sold, provided that will be 12 sunny days in the month.

Summary

The estimated number of glasses sold is approximately 59. The value seems sensible looking at the dataset. Poisson regression is not a very often used type of regression in real systems. However, in some cases, it seems irreplaceable. For this reason, I decided to bring it closer to you and on this occasion show you F#, because maybe you have not had contact with it before.

Leave a Reply

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

Scroll to top