Mastering Machine Learning with ML.NET

Artificial intelligence (AI) is transforming sectors by allowing companies to make data-driven decisions, automate complex processes, and improve user experiences. Integrating AI into apps can convert them from simple tools to intelligent systems capable of learning and adapting. .NET’s rich ecosystem provides developers with a powerful platform for effectively incorporating AI and machine learning (ML) capabilities into their applications. This blog examines how.NET enables developers to create intelligent applications , with the introduction of libraries and integrations such as ML.NET, TensorFlow.NET, and Azure Cognitive Services, as well as practical examples and real-world use cases.

Overview of .NET in AI/ML

.NET provides several tools and frameworks that facilitate the integration of AI and ML into applications.

Tool/FrameworkDescriptionUse Cases
ML.NETOpen-source, cross-platform machine learning framework for .NET developers.Sentiment analysis
price prediction
fraud detection.
Product recommendation
Customer segmentation
Object detection
Sales spike detection
Image classification
Sales forecasting
ONNX RuntimeAn open-source scoring engine for Open Neural Network Exchange (ONNX) models. It enables the deployment of trained models across various platforms and is compatible with .NET, providing flexibility in model integrationDeep learning tasks like image recognition and NLP
Azure Cognitive ServicesSuite of pre-built APIs for tasks like image recognition, speech processing, and language understanding.Speech recognition
face detection
text translation
TensorFlow.NET.NET binding for TensorFlow, enabling the use of TensorFlow models in .NET applications.Deploying pre-trained models

Other Tools

  • Accord.NET: Focused on statistical computing and machine learning.
  • Infer.NET: Microsoft’s library for model-based machine learning.

Supported Use Cases in ML.NET

01. Sentiment analysis

Sentiment analysis identifies whether a piece of text expresses positive or negative sentiment. It is widely used for customer feedback analysis, social media monitoring, and brand reputation management. By understanding user sentiment, businesses can improve products, services, and customer interactions.

Key Concepts

  • Binary Classification: Classifies text into two categories, such as positive or negative sentiment.

02. Price Prediction

Price prediction forecasts the price of a product by analyzing historical data and factors like demand and seasonality. This allows businesses to implement dynamic pricing strategies and respond effectively to market changes.

Key Concepts

  • Regression: Predicts a continuous value, such as product price, based on input features.

03. Fraud Detection

Fraud detection identifies suspicious activities in real-time by analyzing patterns in transaction data. This is essential for securing payment systems and preventing financial losses.

Key Concepts

  • Anomaly Detection: Identifies deviations from expected behavior, such as unusual transactions.

04. Product Recommendation

Product recommendation systems suggest personalized products to users based on their browsing history or purchase behavior. These systems enhance user experience and boost sales by offering tailored suggestions.

Key Concepts

  • Recommendation Modeling: Predicts user preferences to recommend relevant products.

05. Customer segmentation

Customer segmentation divides customers into groups based on behavior or demographics. This enables businesses to target specific groups with personalized marketing strategies, leading to better engagement and conversion rates.

Key Concepts

  • Clustering: Groups data points into clusters based on similarities, such as buying habits.

06. Object Detection

Object detection identifies and locates objects within images, enabling applications like automated checkout systems, surveillance, and autonomous vehicles. By recognizing and pinpointing objects in real-time, it enhances operational efficiency and security.

Key Concepts

  • Object Detection Models: Analyze visual data to detect and locate specific objects within images or videos.

07. Sales Spike Detection

Sales spike detection identifies unexpected increases in sales data, helping businesses recognize trends, capitalize on opportunities, or address anomalies. It allows for timely responses by generating alerts for unusual sales activities.

Key Concepts

  • Anomaly Detection: Monitors sales data to highlight deviations, such as sudden sales surges.

08. Image classification

Image classification assigns images to predefined categories, making it ideal for tasks like quality control in manufacturing and diagnostics in medical imaging. This enhances decision-making by categorizing images with high accuracy.

Key Concepts

  • Image Classification Models: Use visual features to group images into specific categories.

09. Sales forecasting

Sales forecasting predicts future sales trends based on historical data, aiding in inventory management, demand planning, and financial decision-making. This helps businesses optimize resources and align strategies with anticipated market trends.

Key Concepts

  • Forecasting Models: Analyze past data to predict future values, such as sales volumes.

About ML.NET

The addition of machine learning capabilities to apps is made possible by ML.NET, which enables developers to train custom models that transform application data and generate predictions based on that data. With ML.NET, we can make a wide range of predictions.

We can develop our machine learning models using the following process:

  1. Collect our training data and load it into an IDataView object.
  2. Create a pipeline and specify the steps needed to extract features from our data and apply a ML algorithm on it.
  3. Train the model by calling the Fit() method on the pipeline.
  4. Evaluate the model and iterate to improve its performance if needed.
  5. Save the model
  6. Load the model back into a ITransformer object
  7. Finally, make predictions by calling CreatePredictionEngine.Predict(). We can also call CreatePredictionEnginePool in some circumstances (I’ll explain more later on)

Let’s look at some real-world examples, such as building a price prediction model that forecasts continuous numerical values based on input features and a product recommendation system that suggests associated items based on user preferences or purchase history. These examples demonstrate the versatility of ML.NET in managing both regression and recommendation tasks effectively.

Price prediction Using ML.NET

Setting Up the Project

Begin by creating a new .NET Core Console Application and installing the necessary ML.NET packages.

  1. Create a New Project – Open your terminal or command prompt and run
dotnet new console -n LaptopPricePrediction
cd LaptopPricePrediction

2. Install ML.NET NuGet Package

dotnet add package Microsoft.ML

3. Loading and Preparing Data

3.1  Example Dataset ( laptop_data.csv )

 Brand,Processor,RAM,Storage,GraphicsCard,Price
Dell,i5,8,256,NVIDIA,600
HP,i7,16,512,NVIDIA,1200
Acer,i3,4,128,Intel,400
Lenovo,i5,8,256,Intel,650
Asus,i7,16,512,NVIDIA,1100

3.2 Create the Data Model (LaptopData.cs)

public class LaptopData { 
public string Brand { get; set; }
public string Processor { get; set; }
public float RAM { get; set; }
public float Storage { get; set; }
public string GraphicsCard { get; set; }
public float Price { get; set; }
}

public class LaptopPricePrediction {
public float Score { get; set; }
}

3.3 Loading the Data

In Program.cs, load the dataset


{
    static readonly string dataPath = Path.Combine(Environment.CurrentDirectory, "laptop_data.csv");

    static void Main(string[] args)
    {
        var context = new MLContext();

        // Load data
        IDataView data = context.Data.LoadFromTextFile<LaptopData>(
            path: dataPath,
            hasHeader: true,
            separatorChar: ',');

        Console.WriteLine("Data loaded successfully.");
    }
}

4. Building and Training the Model

// Define the data preparation and training pipeline
var pipeline = context.Transforms.Categorical.OneHotEncoding("Brand")
    .Append(context.Transforms.Categorical.OneHotEncoding("Processor"))
    .Append(context.Transforms.Categorical.OneHotEncoding("GraphicsCard"))
    .Append(context.Transforms.Concatenate("Features", "Brand", "Processor", "RAM", "Storage", "GraphicsCard"))
    .Append(context.Regression.Trainers.Sdca(labelColumnName: "Price", maximumNumberOfIterations: 100));

// Train the model
var model = pipeline.Fit(data);
Console.WriteLine("Model training completed.");

5. Evaluating the Model

var splitData = context.Data.TrainTestSplit(data, testFraction: 0.2);

// Evaluate the model
var predictions = model.Transform(splitData.TestSet);
var metrics = context.Regression.Evaluate(predictions, labelColumnName: "Price");

Console.WriteLine($"R^2: {metrics.RSquared}");
Console.WriteLine($"MAE: {metrics.MeanAbsoluteError}");

6. Making Predictions

var predictionEngine = context.Model.CreatePredictionEngine<LaptopData, LaptopPricePrediction>(model);

var newLaptop = new LaptopData
{
    Brand = "Dell",
    Processor = "i5",
    RAM = 8,
    Storage = 256,
    GraphicsCard = "NVIDIA"
};

var pricePrediction = predictionEngine.Predict(newLaptop);
Console.WriteLine($"Predicted Price: {pricePrediction.Score}");

7. Saving and Loading the Model

// Save the trained model for reuse

var modelPath = Path.Combine(Environment.CurrentDirectory, "LaptopPriceModel.zip");
context.Model.Save(model, data.Schema, modelPath);
Console.WriteLine("Model saved.");

// Load the saved model for predictions
var loadedModel = context.Model.Load(modelPath, out var inputSchema);
var loadedPredictionEngine = context.Model.CreatePredictionEngine<LaptopData, LaptopPricePrediction>(loadedModel);

var loadedPricePrediction = loadedPredictionEngine.Predict(newLaptop);
Console.WriteLine($"Predicted Price with loaded model: {loadedPricePrediction.Score}");

Product Recommendation Using ML.NET

Setting Up the Project

Begin by creating a new .NET Core Console Application and installing the necessary ML.NET packages.

  1. Create a New Project – Open your terminal or command prompt and run
dotnet new console -n ProductRecommendation
cd ProductRecommendation

2. Install ML.NET NuGet Package

dotnet add package Microsoft.ML
dotnet add package Microsoft.ML.Recommender

3. Loading and Preparing Data

3.1  Example Dataset ( product-recommendation.csv )

 ProductId,AssociatedProductId,Label
Laptop,Mouse,1
Laptop,Keyboard,1
Laptop,Monitor,0
Mouse,MousePad,1
Keyboard,USBHub,1
Monitor,Cable,1

3.2 Create the Data Model (LaptopData.cs)

public class ProductPair
{

public string ProductId;
public string AssociatedProductId;
public float Label; // Indicates whether the products are associated
}

public class ProductRecommendation
{
public float Score; // Predicted score or likelihood of association
}


3.3 Loading the Data

In Program.cs, load the dataset


var mlContext = new MLContext();

// Load the data
string dataPath = "product-recommendation.csv";
IDataView dataView = mlContext.Data.LoadFromTextFile<ProductPair>(
    dataPath, hasHeader: true, separatorChar: ',');

// Split the data into training and test sets
var dataSplit = mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);
var trainingData = dataSplit.TrainSet;
var testData = dataSplit.TestSet;

4. Building and Training the Model

// Define the data processing pipeline
var pipeline = mlContext.Transforms.Conversion
    .MapValueToKey("ProductIdEncoded", "ProductId")
    .Append(mlContext.Transforms.Conversion.MapValueToKey("AssociatedProductIdEncoded", "AssociatedProductId"))
    .Append(mlContext.Recommendation().Trainers.MatrixFactorization(
        new Microsoft.ML.Trainers.MatrixFactorizationTrainer.Options
        {
            LabelColumnName = "Label",
            MatrixColumnIndexColumnName = "ProductIdEncoded",
            MatrixRowIndexColumnName = "AssociatedProductIdEncoded",
            NumberOfIterations = 5,
            ApproximationRank = 10
        }));

// Train the model
Console.WriteLine("Training the model...");
var model = pipeline.Fit(trainingData);

5. Evaluating the Model

// Evaluate the model using recommendation metrics
Console.WriteLine("Evaluating the model...");
var predictions = model.Transform(testData);
var metrics = mlContext.Recommendation().Evaluate(predictions, labelColumnName: "Label", scoreColumnName: "Score");

Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError}");

6. Making Predictions

// Create a prediction engine
Console.WriteLine("Making predictions...");
var predictionEngine = mlContext.Model.CreatePredictionEngine<ProductPair, ProductRecommendation>(model);

// Sample prediction
var sample = new ProductPair { ProductId = "Laptop", AssociatedProductId = "Mouse" };
var prediction = predictionEngine.Predict(sample);
Console.WriteLine($"Likelihood of buying 'Mouse' with 'Laptop': {prediction.Score}");

7. Saving and Loading the Model

// Save the model
string modelPath = "complementary-product-model.zip";
mlContext.Model.Save(model, dataView.Schema, modelPath);
Console.WriteLine($"Model saved to {modelPath}");

// Load the model
var loadedModel = mlContext.Model.Load(modelPath, out var modelInputSchema);
Console.WriteLine("Model loaded successfully.");

// Create a prediction engine with the loaded model
var loadedPredictionEngine = mlContext.Model.CreatePredictionEngine<ProductPair, ProductRecommendation>(loadedModel);

// Sample prediction with the loaded model
var loadedSample = new ProductPair { ProductId = "Laptop", AssociatedProductId = "Mouse" };
var loadedPrediction = loadedPredictionEngine.Predict(loadedSample);
Console.WriteLine($"Likelihood of buying 'Mouse' with 'Laptop' (loaded model): {loadedPrediction.Score}");

Real-world Use Cases

Integrating AI into .NET is being applied across various sectors to solve complex problems, drive innovation, and enhance user experiences by leveraging machine learning, data analytics, and automation to streamline processes, improve decision-making, and deliver smarter solutions.

Industry : E Commerce

1. Predictive Maintenance

Utilize ML models to predict machinery failures before they occur, reducing downtime and maintenance costs.

Implementation Steps

  1. Data Collection: Gather sensor data from machinery (e.g., temperature, vibration).
  2. Model Training: Use ML.NET to build a regression model that predicts equipment failure.
  3. Integration: Embed the model into a .NET application that monitors real-time data and alerts maintenance teams.
// model is trained and saved as predictiveMaintenanceModel.zip
{
        var mlContext = new MLContext();

        // Load sensor data
        var dataPath = "sensorData.csv"; // Replace with your dataset path
        var data = mlContext.Data.LoadFromTextFile<SensorData>(dataPath, separatorChar: ',', hasHeader: true);

        // Define the pipeline
        var pipeline = mlContext.Transforms.Concatenate("Features", 
                nameof(SensorData.Temperature), 
                nameof(SensorData.Vibration))
            .Append(mlContext.Regression.Trainers.Sdca(labelColumnName: nameof(SensorData.TimeToFailure), featureColumnName: "Features"));

        // Train the model
        var model = pipeline.Fit(data);

        // Save the model
        mlContext.Model.Save(model, data.Schema, "predictiveMaintenanceModel.zip");
        Console.WriteLine("Model training completed and saved.");
    }
// Making Predictions
{
        var mlContext = new MLContext();

        // Load the trained model
        var model = mlContext.Model.Load("predictiveMaintenanceModel.zip", out _);

        // Create a prediction engine
        var predictionEngine = mlContext.Model.CreatePredictionEngine<SensorData, MaintenancePrediction>(model);

        // Example input data
        var inputData = new SensorData { Temperature = 85, Vibration = 0.5f };

        // Predict
        var prediction = predictionEngine.Predict(inputData);
        Console.WriteLine($"Predicted Time to Failure: {prediction.TimeToFailure}");
    }

2. Customer Segmentation

Segment customers based on their shopping behavior and predict which products they are most likely to purchase. This can help with targeted marketing and promotional campaigns.

Implementation Steps

  • Data Collection: Collect customer transaction history, demographic data, and behavioral data (e.g., clicks, time spent on site).
  • Model Training: Use clustering algorithms or classification algorithms to group customers into segments or predict future purchases.
  • Integration: Use the trained model to suggest personalized products or send personalized offers based on the customer segment.
 {
        var mlContext = new MLContext();

        // Load data
        var dataPath = "customerData.csv";
        var data = mlContext.Data.LoadFromTextFile<CustomerData>(dataPath, separatorChar: ',', hasHeader: true);

        // Define pipeline
        var pipeline = mlContext.Transforms.Concatenate("Features", nameof(CustomerData.Age), nameof(CustomerData.SpendingScore))
            .Append(mlContext.Clustering.Trainers.KMeans(numberOfClusters: 3));

        // Train model
        var model = pipeline.Fit(data);

        // Save the model
        mlContext.Model.Save(model, data.Schema, "segmentationModel.zip");
    }

3. Sales Spike Detection

Sales spike detection helps identify unusual increases in sales over a short period. This can help businesses identify the impact of promotions, seasonal trends, or even fraudulent activities (like bot-driven traffic). Sales spikes can also indicate product popularity or unexpected events (e.g., influencer marketing campaigns).

Implementation Steps

  • Data Collection: Collect time-series sales data (units sold, price, etc.) Include metadata such as promotional activities, discounts, external events (e.g., holidays).
  • Model Training: Use anomaly detection models or classification models to detect spikes.The model can identify spikes by comparing current sales to expected or historical trends.
  • Integration: Use the trained model to monitor sales data in real time and flag sales spikes when detected.Trigger alerts, notifications, or further analysis when a spike occurs.
 {
        var mlContext = new MLContext();

        // Load data
        var dataPath = "salesData.csv";
        var data = mlContext.Data.LoadFromTextFile<SalesData>(dataPath, separatorChar: ',', hasHeader: true);

        // Define pipeline
        var pipeline = mlContext.Transforms.DetectIidSpike(
            outputColumnName: nameof(SalesSpikePrediction.Prediction),
            inputColumnName: nameof(SalesData.Sales),
            confidence: 95,
            pvalueHistoryLength: 7);

        // Train model
        var model = pipeline.Fit(data);

        // Save the model
        mlContext.Model.Save(model, data.Schema, "spikeDetectionModel.zip");
    }

3. Sales Forecasting

Sales forecasting involves predicting future sales based on historical data, allowing businesses to plan inventory, marketing campaigns, and workforce needs. In e-commerce, accurate sales forecasts help businesses maintain optimal stock levels, reduce waste, and manage cash flow efficiently.

Implementation Steps

  • Data Collection: Collect historical sales data with variables such as Product sales (units sold) ,Product categories ,Dates and times of sales, Promotions and discounts , External factors like seasonality, weather, or holidays
  • Model Training: Use regression or time series forecasting models like ARIMA or SDCA regression in ML.NET to predict future sales.
  • Integration: Deploy the model to provide ongoing forecasts, helping optimize inventory and sales strategies.
 {
        var mlContext = new MLContext();

        // Load data
        var dataPath = "salesForecastingData.csv";
        var data = mlContext.Data.LoadFromTextFile<SalesData>(dataPath, separatorChar: ',', hasHeader: true);

        // Define pipeline
        var pipeline = mlContext.Forecasting.ForecastBySsa(
            outputColumnName: "ForecastedSales",
            inputColumnName: nameof(SalesData.Sales),
            windowSize: 7,
            seriesLength: 30,
            trainSize: 90,
            horizon: 7);

        // Train model
        var model = pipeline.Fit(data);

        // Save the model
        mlContext.Model.Save(model, data.Schema, "salesForecastingModel.zip");
    }

Conclusion

In this journey through Mastering Machine Learning with ML.NET, we’ve looked at numerous real-world use examples that demonstrate ML.NET’s enormous potential for altering company operations across e-commerce industries.  Businesses that use ML.NET can gain useful insights from their data, allowing them to streamline operations, improve decision-making, and provide better customer experiences.

Key use examples, such as Sales Forecasting and Sales Spike Detection, show how machine learning may be used to predict future sales patterns, optimize inventory, and spot abnormalities in sales behavior. These solutions not only increase operational efficiency, but they also give businesses a competitive advantage in fast-changing industries.

The simplicity and flexibility of ML.NET allow developers to seamlessly integrate machine learning models into their applications without requiring deep expertise in data science. By leveraging this powerful framework, businesses can harness the potential of machine learning to drive innovation, reduce risks, and make data-driven decisions that lead to success. Mastering ML.NET enables organizations to move from theoretical knowledge to practical implementation, unlocking the true value of machine learning in real-world applications. With its robust tools and scalable models, ML.NET empowers businesses to turn data into actionable insights, ultimately enhancing growth and profitability.

Sewwandi JM

Share
Published by
Sewwandi JM

Recent Posts

Optimizing Video Processing Pipelines with FFmpeg and .NET

In today’s fast-paced digital world, video content dominates the internet. From streaming platforms to social…

1 month ago

Multi-Tenancy Strategies in .NET

Web application development revolves around scalability and security, especially when serving multiple clients from a…

2 months ago

Unforgettable Team Outing at Pearl Bay

Our team had an absolutely amazing day at Pearl Bay, enjoying thrilling karting adventures and…

3 months ago

Real-Time Collaboration in SaaS Applications Using SignalR in .NET

In real-time applications, data is given to subscribers as soon as it is published by…

4 months ago

Our Experience with Shopify App Development: Challenges, Wins and Lessons

When we completed our first project using the Hydrogen framework for a client, we truly…

4 months ago

Mastering Docker: A Deep Dive into Containers, Images, and the Docker Daemon

Docker has transformed the way we build, deploy, and manage applications, enabling developers to create…

5 months ago

This website uses cookies.