Using ML.NET for Predicting Hourly User Session Counts

Harshal Sonparote 1 Reputation point
2024-10-30T12:51:31.8033333+00:00

I am new to AI/ML and need assistance with a business scenario that involves machine learning, specifically using ML.NET as our preferred technology.

We have collected hourly user session count data per country for the last year through log analytics. The goal is to train a model using this data to predict hourly user session counts for future dates.

Could anyone suggest a suitable model for this use case? We've explored time series and regression models, but we're currently struggling to achieve 24-hour predictions for specific future days.

Any sample code or resources would be greatly appreciated.

Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
3,044 questions
.NET Machine learning
.NET Machine learning
.NET: Microsoft Technologies based on the .NET software framework.Machine learning: A type of artificial intelligence focused on enabling computers to use observed data to evolve new behaviors that have not been explicitly programmed.
159 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 14,551 Reputation points
    2024-10-30T15:07:16.3066667+00:00

    Hello Harshal Sonparote,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you would like to know how you can use ML.NET for Predicting Hourly User Session Counts.

    You are on the right track and welcome to the world of AI/ML! Your use case / scenario of predicting hourly user session counts is a classic time series forecasting problem, and the below are some suggestions and resources to help you achieve your goal with ML.NET:

    1. For predicting hourly user session counts, a time series forecasting model is appropriate. In ML.NET, you can use the Singular Spectrum Analysis (SSA) algorithm, which is well-suited for univariate time series data. SSA can help you capture trends, seasonality, and noise in your data.
    2. To achieve 24-hour predictions for specific future days, you can use the SsaForecastingEstimator in ML.NET. This estimator allows you to forecast multiple steps ahead, which is ideal for your requirement of predicting hourly counts for an entire day.
    3. The sample code as requested:
      1. Open Visual Studio and create a new C# Console Application.
      2. Install the necessary NuGet packages: Microsoft.ML, Microsoft.ML.TimeSeries.
      3. Ensure your data is in a suitable format, with columns for the timestamp and the session counts.
      4. Load your data into the application.
      5. Define Input and Output Classes:
              public class ModelInput
              {
                  public DateTime Timestamp { get; set; }
                  public float SessionCount { get; set; }
              }
              public class ModelOutput
              {
                  public float[] ForecastedCounts { get; set; }
                  public float[] LowerBoundCounts { get; set; }
                  public float[] UpperBoundCounts { get; set; }
              }
        
      6. Create and Train the Model:
              var mlContext = new MLContext();
              var dataView = mlContext.Data.LoadFromTextFile<ModelInput>("data.csv", hasHeader: true, separatorChar: ',');
              var forecastingPipeline = mlContext.Forecasting.ForecastBySsa(
                  outputColumnName: nameof(ModelOutput.ForecastedCounts),
                  inputColumnName: nameof(ModelInput.SessionCount),
                  windowSize: 24,
                  seriesLength: 365 * 24,
                  trainSize: 365 * 24,
                  horizon: 24);
              var model = forecastingPipeline.Fit(dataView);
        
      7. Make Predictions:
              var forecastEngine = model.CreateTimeSeriesEngine<ModelInput, ModelOutput>(mlContext);
              var forecast = forecastEngine.Predict();
        

    For more reading and resources use the following links:

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.