Hands-On Machine Learning with ML.NET

Book description

Create, train, and evaluate various machine learning models such as regression, classification, and clustering using ML.NET, Entity Framework, and ASP.NET Core

Key Features

  • Get well-versed with the ML.NET framework and its components and APIs using practical examples
  • Learn how to build, train, and evaluate popular machine learning algorithms with ML.NET offerings
  • Extend your existing machine learning models by integrating with TensorFlow and other libraries

Book Description

Machine learning (ML) is widely used in many industries such as science, healthcare, and research and its popularity is only growing. In March 2018, Microsoft introduced ML.NET to help .NET enthusiasts in working with ML. With this book, you'll explore how to build ML.NET applications with the various ML models available using C# code.

The book starts by giving you an overview of ML and the types of ML algorithms used, along with covering what ML.NET is and why you need it to build ML apps. You'll then explore the ML.NET framework, its components, and APIs. The book will serve as a practical guide to helping you build smart apps using the ML.NET library. You'll gradually become well versed in how to implement ML algorithms such as regression, classification, and clustering with real-world examples and datasets. Each chapter will cover the practical implementation, showing you how to implement ML within .NET applications. You'll also learn to integrate TensorFlow in ML.NET applications. Later you'll discover how to store the regression model housing price prediction result to the database and display the real-time predicted results from the database on your web application using ASP.NET Core Blazor and SignalR.

By the end of this book, you'll have learned how to confidently perform basic to advanced-level machine learning tasks in ML.NET.

What you will learn

  • Understand the framework, components, and APIs of ML.NET using C#
  • Develop regression models using ML.NET for employee attrition and file classification
  • Evaluate classification models for sentiment prediction of restaurant reviews
  • Work with clustering models for file type classifications
  • Use anomaly detection to find anomalies in both network traffic and login history
  • Work with ASP.NET Core Blazor to create an ML.NET enabled web application
  • Integrate pre-trained TensorFlow and ONNX models in a WPF ML.NET application for image classification and object detection

Who this book is for

If you are a .NET developer who wants to implement machine learning models using ML.NET, then this book is for you. This book will also be beneficial for data scientists and machine learning developers who are looking for effective tools to implement various machine learning algorithms. A basic understanding of C# or .NET is mandatory to grasp the concepts covered in this book effectively.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Machine Learning with ML.NET
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Section 1: Fundamentals of Machine Learning and ML.NET
  8. Getting Started with Machine Learning and ML.NET
    1. The importance of learning about machine learning today
    2. The model building process
      1. Defining your problem statement
      2. Defining your features
      3. Obtaining a dataset
      4. Feature extraction and pipeline
      5. Model training
      6. Model evaluation
    3. Exploring types of learning
      1. Supervised learning
      2. Unsupervised learning
    4. Exploring various machine learning algorithms
      1. Binary classification
      2. Regression
      3. Anomaly detection
      4. Clustering
      5. Matrix factorization
    5. What is ML.NET?
      1. Technical details of ML.NET
      2. Components of ML.NET
      3. Extensibility of ML.NET
    6. Summary
  9. Setting Up the ML.NET Environment
    1. Setting up your development environment
      1. Installing Visual Studio
      2. Installing .NET Core 3
      3. Creating a process
    2. Creating your first ML.NET application
      1. Creating the project in Visual Studio
      2. Project architecture
      3. Running the code
        1. The RestaurantFeedback class
        2. The RestaurantPrediction class
        3. The Trainer class
        4. The Predictor class
        5. The BaseML class
        6. The Program class
      4. Running the example
    3. Evaluating the model
    4. Summary
  10. Section 2: ML.NET Models
  11. Regression Model
    1. Breaking down regression models
      1. Choosing the type of regression model
      2. Choosing a linear regression trainer
      3. Choosing a logistic regression trainer
    2. Creating the linear regression application
      1. Diving into the trainer
      2. Exploring the project architecture
      3. Diving into the code
        1. The ExtensionMethods class
        2. The EmploymentHistory class
        3. The EmploymentHistoryPrediction class
        4. The Predictor class
        5. The Trainer class
        6. The Program class
      4. Running the application
    3. Creating the logistic regression application
      1. Exploring the project architecture
      2. Diving into the code
        1. The FeatureExtractor class
        2. The FileInput class
        3. The FilePrediction class
        4. The BaseML class
        5. The Predictor class
        6. The Trainer class
        7. The Program class
      3. Running the application
    4. Evaluating a regression model
      1. Loss function
      2. Mean squared error
      3. Mean absolute error
      4. R-squared
      5. Root mean squared error
    5. Summary
  12. Classification Model
    1. Breaking down classification models
      1. Choosing a classification trainer
    2. Creating a binary classification application
      1. Diving into the trainer
      2. Exploring the project architecture
      3. Diving into the code
      4. The CarInventory class
      5. The CarInventoryPrediction class
      6. The Predictor class
      7. The Trainer class
      8. The Program class
      9. Running the application
    3. Creating a multi-class classification application
      1. Diving into the trainer
      2. Exploring the project architecture
      3. Diving into the code
      4. The Email class
      5. The EmailPrediction class
      6. The Predictor class
      7. The Trainer class
      8. Running the application
    4. Evaluating a classification model
      1. Accuracy
      2. Area Under ROC Curve
      3. F1 Score
      4. Area Under Precision-Recall Curve
      5. Micro Accuracy
      6. Macro Accuracy
      7. Log Loss
      8. Log-Loss Reduction
    5. Summary
  13. Clustering Model
    1. Breaking down the k-means algorithm
      1. Use cases for clustering
      2. Diving into the k-means trainer
    2. Creating the clustering application
      1. Exploring the project architecture
      2. Diving into the code
      3. The Constants class
      4. The BaseML class
      5. The FileTypes enumeration
      6. The FileData class
      7. The FileTypePrediction class
      8. The FeatureExtractor class
      9. The Predictor class
      10. The Trainer class
      11. The Program class
      12. Running the application
    3. Evaluating a k-means model
      1. Average distance
      2. The Davies-Bouldin Index
      3. Normalized mutual information
    4. Summary
  14. Anomaly Detection Model
    1. Breaking down anomaly detection
      1. Use cases for anomaly detection
      2. Diving into the randomized PCA trainer
      3. Diving into time series transforms
    2. Creating a time series application
      1. Exploring the project architecture
      2. Diving into the code
      3. The NetworkTrafficHistory class
      4. The NetworkTrafficPrediction class
      5. The Predictor class
      6. The Trainer class
      7. The Program class
      8. Running the application
    3. Creating an anomaly detection application
      1. Exploring the project architecture
      2. Diving into the code
      3. The Constants class
      4. The LoginHistory class
      5. The LoginPrediction class
      6. The Predictor class
      7. The Trainer class
      8. Running the application
    4. Evaluating a randomized PCA model
      1. Area under the ROC curve
      2. Detection rate at false positive count
    5. Summary
  15. Matrix Factorization Model
    1. Breaking down matrix factorizations
      1. Use cases for matrix factorizations
      2. Diving into the matrix factorization trainer
    2. Creating a matrix factorization application
      1. Exploring the project architecture
      2. Diving into the code
        1. The MusicRating class
        2. The MusicPrediction class
        3. The Predictor class
        4. The Trainer class
        5. The Constants class
      3. Running the application
    3. Evaluating a matrix factorization model
      1. Loss function
      2. MSE
      3. MAE
      4. R-squared 
      5. RMSE
    4. Summary
  16. Section 3: Real-World Integrations with ML.NET
  17. Using ML.NET with .NET Core and Forecasting
    1. Breaking down the .NET Core application architecture
      1. .NET Core architecture
      2. .NET Core targets
      3. .NET Core future
    2. Creating the stock price estimator application
      1. Exploring the project architecture
      2. Diving into the code
      3. The ProgramActions enumeration
      4. The CommandLineParser class
      5. The BaseML class
      6. The StockPrediction class
      7. The StockPrices class
      8. The Predictor class
      9. The Trainer class
      10. The ProgramArguments class
      11. The Program class
      12. Running the application
    3. Exploring additional production application enhancements
      1. Logging
      2. Utilizing Reflection further
      3. Utilizing a database
    4. Summary
  18. Using ML.NET with ASP.NET Core
    1. Breaking down ASP.NET Core
      1. Understanding the ASP.NET Core architecture
        1. Controllers
        2. Models
        3. Views
      2. Blazor
    2. Creating the file classification web application
      1. Exploring the project architecture
      2. Diving into the library
        1. The FileClassificationResponseItem class
        2. The FileData class
        3. The FileDataPrediction class
        4. The Converters class
        5. The ExtensionMethods class
        6. The HashingExtensions class
        7. The FileClassificationFeatureExtractor class
        8. The FileClassificationPredictor class
        9. The FileClassificationTrainer class
      3. Diving into the web application
        1. The UploadController class
        2. The Startup class
        3. The Index.razor file
      4. Diving into the trainer application
        1. The ProgramArguments class
        2. The ProgramActions enumeration
        3. The Program class
      5. Running the trainer application
      6. Running the web application
    3. Exploring additional ideas for improvements
      1. Logging
      2. Utilizing a caching layer
      3. Utilizing a database
    4. Summary
  19. Using ML.NET with UWP
    1. Breaking down the UWP architecture
      1. Views
      2. Models
      3. View Models
    2. Creating the web browser classification application
      1. Exploring the project architecture
      2. Diving into the library
        1. The Constants class
        2. The WebPageResponseItem class
        3. The Converters class
        4. The ExtensionMethods class
        5. The WebPageInputItem class
        6. The WebPagePredictionItem class
        7. The WebContentFeatureExtractor class
        8. The WebContentPredictor class
        9. The WebContentTrainer class
      3. Diving into the UWP browser application
        1. The MainPageViewModel class
        2. MainPage.xaml
        3. MainPage.xaml.cs
      4. Diving into the trainer application
        1. The ProgramArguments class
        2. The Program class
      5. Running the trainer application
      6. Running the browser application
    3. Additional ideas for improvements
      1. Single-download optimization
      2. Logging
      3. Utilizing a database
    4. Summary
  20. Section 4: Extending ML.NET
  21. Training and Building Production Models
    1. Investigating feature engineering
      1. PNG image files with embedded executables
        1. Creating a PNG parser
    2. Obtaining training and testing datasets
    3. Creating your model-building pipeline
      1. Discussing attributes to consider in a pipeline platform
      2. Exploring machine learning platforms
        1. Azure Machine Learning
        2. Apache Airflow
        3. Apache Spark
    4. Summary
  22. Using TensorFlow with ML.NET
    1. Breaking down Google's Inception model
    2. Creating the WPF image classification application
      1. Exploring the project architecture
      2. Diving into the WPF image classification application
        1. The MainWindowViewModel class
        2. The MainWindow.xaml class
        3. The MainWindow.xaml.cs file
        4. The BaseML class
        5. The ImageDataInputItem class
        6. The ImageDataPredictionItem class
        7. The ImageClassificationPredictor class
      3. Running the image classification application
    3. Additional ideas for improvements
      1. Self-training based on the end user's input
      2. Logging
      3. Utilizing a database
    4. Summary
  23. Using ONNX with ML.NET
    1. Breaking down ONNX and YOLO
      1. Introducing ONNX
      2. The YOLO ONNX model
    2. Creating the ONNX object detection application
      1. Exploring the project architecture
      2. Diving into the code
        1. The DimensionsBase class
        2. The YoloBoundingBox class
        3. The MainWindow.xaml file
        4. The ImageClassificationPredictor class
        5. The MainWindowViewModel class
      3. Running the application
    3. Exploring additional production application enhancements
      1. Logging
      2. Image scaling
      3. Utilizing the full YOLO model
    4. Summary
  24. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Machine Learning with ML.NET
  • Author(s): Jarred Capellman
  • Release date: March 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781789801781