Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Machine Learning with Rust: A practical attempt to explore Rust and its libraries across popular machine learning techniques
Machine Learning with Rust: A practical attempt to explore Rust and its libraries across popular machine learning techniques
Machine Learning with Rust: A practical attempt to explore Rust and its libraries across popular machine learning techniques
Ebook242 pages2 hours

Machine Learning with Rust: A practical attempt to explore Rust and its libraries across popular machine learning techniques

Rating: 0 out of 5 stars

()

Read preview

About this ebook

In this stimulating journey of Rust, you'll learn how to use the Rust programming language in conjunction with machine learning. It's not a full guide to learning machine learning with Rust. Instead, it's more of a journey that shows you what's possible when you use Rust to solve machine learning problems. Some people like Rust because it is qui

LanguageEnglish
PublisherGitforGits
Release dateJan 31, 2024
ISBN9788119177714
Machine Learning with Rust: A practical attempt to explore Rust and its libraries across popular machine learning techniques

Related to Machine Learning with Rust

Related ebooks

Intelligence (AI) & Semantics For You

View More

Related articles

Reviews for Machine Learning with Rust

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Machine Learning with Rust - Keiko Nakamura

    Machine Learning with Rust

    A practical attempt to explore Rust and its libraries across popular machine learning techniques

    Keiko Nakamura

    Preface

    In this stimulating book, you'll learn how to use the Rust programming language in conjunction with machine learning. It's not a full guide to learning machine learning with Rust. Instead, it's more of a journey that shows you what's possible when you use Rust to solve machine learning problems. Some people like Rust because it is quick and safe. This book shows how those qualities can help machine learning a lot.

    To begin, we will show you what Rust is and how it works. This is so that everyone, even those who are new to Rust, can follow along. Then, we look at some basic machine learning concepts, such as linear and logistic regression, and show how to use Rust's tools and libraries to make these ideas work.

    You will learn more complex techniques like decision trees, support vector machines, and how to work with data as we go along. It goes all the way up to neural networks and image recognition, and we show you how to use Rust for these types of tasks step by step. We use real-world examples, such as COVID data and the CIFAR-10 image set, to show how Rust works with issues that come up in the real world.

    This book is all about discovery and experimentation. To see what you can do with them, we use various Rust tools for machine learning. It's a fun way to see how Rust can be used in machine learning, and it will make you want to try new things and learn more on your own. This is only the beginning; there is so much more to uncover as you continue to explore machine learning with Rust.

    In this book you will learn how to:

    Exploit Rust's efficiency and safety to construct fast machine learning models.

    Use Rust's ndarray crate for numerical computations to manipulate complex machine learning data.

    Find out how Rust's extensible machine learning framework, linfa, works across algorithms to comprehend the ecosystem.

    Use Rust's precision and speed to construct linear and logistic regression.

    See how Rust crates simplify decision trees and random forests for prediction and categorization.

    Learn to implement and optimize probabilistic classifiers, SVMs and closest neighbor methods in Rust.

    Use Rust's computing power to study neural networks and CNNs for picture recognition and processing.

    Apply learnt strategies to COVID and CIFAR-10 datasets to address realistic problems and obtain insights.

    Prologue

    Prior to starting this writing expedition, I had a clear but challenging objective: to discover how the language Rust, which is known for being fast and safe, can work with the constantly changing field of machine learning. This book was written out of a mix of interest and a strong desire to go where no one has gone before, mapping out the places where Rust and machine learning meet. It's more of an exploration and story of finding than a claim to mastery. As the author, I'm not inviting you on this journey as a perfect teacher, but as a fellow learner eager to find out what can be done by combining the strength of Rust with the flexibility of machine learning methods.

    It was important to make it clear from the start that this book is not meant to be the only way to learn machine learning with Rust. It's more of a starting point or introduction for people who are interested in what Rust can do for machine learning projects. Sharing knowledge and trying new things is what these pages are all about. The goal is to get you interested and give you a base from which to build.

    We start with the basics and show you how Rust works so that even people who are new to the language can see what it can do. The steps in the book are similar to how I learned Rust: from getting to know its syntax and features to looking into how its ideas can be used to make machine learning methods work better, they are similar.

    The exploring spirit of this book stays true as we learn about different machine learning techniques, from basic

    algorithms to more complex neural networks. The datasets of COVID, CIFAR-10 and practical examples are meant to show the way by showing how Rust's features, such as its speed, memory safety, and concurrency, can be used to deal with the problems that come up in machine learning projects.

    The main idea of this book is to ask questions, learn, and discover what hasn't been explored before. Developers and experts alike think that combining Rust and machine learning can lead to new discoveries. Although there are technical details, the story does not present them as a declaration of knowledge. Instead, it does so as a conversation, encouraging you to try new things, dig deeper, and help us learn more.

    Finally, I want to recommend this book to anyone who is interested in what can happen when Rust and machine learning come together. It opens the door to new ideas by showing how the rules of a systems programming language can be combined with machine learning to encourage new ideas. As we turn the pages together, let's start this journey with an open mind and a desire to learn, ready to discover the huge possibilities that lie ahead. At any point, you encounter correction, please do not forget to highlight them up to us at support@gitforgits.com

    Copyright © 2024 by GitforGits

    All rights reserved. This book is protected under copyright laws and no part of it may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without the prior written permission of the publisher. Any unauthorized reproduction, distribution, or transmission of this work may result in civil and criminal penalties and will be dealt with in the respective jurisdiction at anywhere in India, in accordance with the applicable copyright laws.

    Published by: GitforGits

    Publisher: Sonal Dhandre

    www.gitforgits.com

    support@gitforgits.com

    Printed in India

    First Printing: January 2024

    ISBN: 9788119177936

    Cover Design by: Kitten Publishing

    For permission to use material from this book, please contact GitforGits at support@gitforgits.com.

    Content

    Preface

    GitforGits

    Acknowledgement

    Chapter 1: Rust Basics for Machine Learning

    Machine Learning Landscape

    Structure and Components of Machine Learning

    Types of Machine Learning

    Why Rust for ML?

    Prospects of Machine Learning

    Basics of Rust Programming

    Memory Safety and Ownership

    Concurrency

    Error Handling

    Comprehensive Ecosystem and Package Management

    Interoperability with Different Languages

    Cargo and Popular Rust Packages

    Cargo Overview

    Managing Dependencies

    Cargo's Extensibility

    ndarray and ndarray-linalg

    Linfa

    tch-rs and tract

    Setting up Rust Environment

    Installing Rust and Cargo

    Selecting IDE or Text Editor

    Initializing New Project

    Installing Essential Packages

    Testing Environment with Simple Program

    Structure Machine Learning Projects

    Root Directory and Workspace Configuration

    Source Code Organization

    Data Folder for Datasets and Resources

    Model Artifacts and Evaluation Metrics

    Documentation and Notebooks

    My First Rust Program

    Setting up Project Directory

    Creating Main Rust File

    Importing Library and Initializing Arrays

    Performing Array Operations

    Running the Program

    Organizing Dependencies in Cargo.toml

    Creating Feature Flags

    Utilizing Dev and Build Dependencies

    Versioning in Cargo

    Rust IDEs

    Why IntelliJ?

    Downloading and Installing IntelliJ IDEA

    Installing Rust Plugin

    Configuring IDE

    Creating First Rust Project in IntelliJ

    Debugging and Testing in IntelliJ

    Preparing ML Environment for Release

    Optimizing Code for Release

    Managing Dependencies and Updating Crates

    Setting Version Number and Metadata

    Creating Release Profile

    Packaging and Distributing the Project

    Summary

    Chapter 2: Data Wrangling with Rust

    Introduction

    Read CSV Files

    Adding CSV Crate and File I/O Libraries

    Reading CSV File

    Dealing with Headers and Specific Fields

    Data Validation

    Storing Records in a Data Structure

    Data Cleaning with Polars

    Installing Polars and Importing Data

    Handling Missing Values

    Removing Outliers

    Normalizing and Scaling Features

    Data Transformation

    Feature Engineering

    Creating Polynomial Features

    Creating Interaction Terms

    Categorical Variable Encoding

    Binning and Discretization

    Normalization and Standardization

    Plotters and Data Visualization

    Install Plotters

    Basic Plot Creation

    Populating Data Points

    Advanced Customization

    Plotting Multiple Series

    Data Export: JSON, Parquet, Feather and Binary

    JSON (JavaScript Object Notation)

    Parquet

    Feather

    Native Binary

    Data Serialization and Deserialization

    Understanding Serialization

    Using Serde for Serialization

    Deserialization in Rust

    Custom Serialization and Deserialization

    Summary

    Chapter 3: Linear Regression by Example

    Introduction to Linear Regression

    Overview

    Linear Regression: Problem Statement

    Definition

    Variable Selection and Feature Engineering

    Data Splitting

    Feature Scaling

    Target Variable

    Popular Linear Regression Strategies

    Exploratory Data Analysis (EDA)

    Hypothesis Testing

    Model Selection

    Feature Interaction Terms

    Model Training and Validation

    Iterative Refinement

    Interpreting Results and Making Decisions

    Implement Linear Regression Model

    Model Training

    Interpreting Coefficients

    Model Prediction and Validation

    Iterative Refinement

    Other Linear Regression Libraries

    Rustlearn

    Smartcore

    Alumina

    Evaluate Linear Regression Performance

    Root Mean Square Error (RMSE)

    Coefficient of Determination (R-squared)

    Mean Absolute Error (MAE)

    Residual Plots

    Save and Reload Model

    Serialization using Serde

    Writing and Reading Files

    Deserialization and Model Loading

    Linear Regression Model Deployment

    Packaging the Model

    Creating a Web Service

    Deploying to Server

    Monitoring and Logging

    Versioning and Rollback

    Summary

    Chapter 4: Logistic Regression for Classification

    Overview

    Introduction to Logistic Regression

    Implement Logistic Regression

    Problem Statement and Data Splitting

    Model Training

    Making Predictions

    Evaluation Metrics

    Hyperparameter Tuning

    Performance Metrics Deep Dive

    Understanding Accuracy

    Precision and Recall

    F1 Score and Confusion Matrix

    ROC Curve and AUC-ROC

    Comparing Metrics

    Model Hyperparameter Tuning Process

    Grid Search Methodology

    Random Search Methodology

    Cross-Validation in Tuning

    Fine-Tuning and Iterative Process

    Final Model Training and Verification

    Sample Program: Hyperparameter Tuning

    Define Hyperparameter Space and Cross-Validation

    Grid Search Loop

    Train and Evaluate the Model

    Serialize and Deserialize the Model

    Summary

    Chapter 5: Decision Trees in Action

    Introduction to Decision Trees

    Interpretable and Transparent Model

    Feature Importance and Selection

    Handling Categorical and Numerical Data

    Ensemble Methods and Random Forests

    Online Purchase Retail Dataset

    Data Processing for Decision Trees

    Reading the Excel File and Initial Exploration

    Handling Missing Values

    Encoding Categorical Variables

    Data Splitting

    Data Normalization

    Performing Feature Extraction

    Creating Customer Lifetime Value

    Temporal Feature Engineering

    Average Purchase Value

    Seasonality Features

    Product Categories

    Geographical Features

    Initialize and Train Decision Trees

    Separate Features and Labels

    Initialize Decision Tree Model

    Training the Model

    Model Evaluation

    Making Predictions

    Batch Predictions

    Model Interpretation

    Summary

    Chapter 6: Mastering Random Forests

    Introduction to Random Forests

    Random Forest Classifier Architecture

    Building Random Forest Model

    Up and Running with RustLearn

    Defining Random Forest Model

    Training and Predict Model

    RustLearn’s Model Evaluation

    Comparison with Decision Tree Model

    Creating Respective Models

    Evaluating Models

    Model Interpretability

    Computational Cost

    Overfitting

    Feature Importance

    Tuning Random Forests Hyperparameters

    Manual Grid Search

    Randomized Search

    Cross-Validation

    Errors and Troubleshooting

    #1 Overfitting

    Solution

    #2 Memory Exhaustion

    Solution

    #3 Imbalanced Data

    Solution

    #4 High Dimensionality

    Solution

    #5 Parallelization Errors

    Solution

    #6 Numerical Instabilities

    Solution

    #7 Non-Numerical Data

    Solution

    #8 Incomplete or Missing Data

    Solution

    #9 Incorrect Evaluation Metrics

    Solution

    Summary

    Chapter 7: Support Vector Machines In Action

    Introduction to Support Vector Machines

    Types of Support Vector Machines

    Linear SVM

    Architecture of Linear SVM

    Kernel SVM

    Architecture of Kernel SVM

    Differences and Commonality

    SVM Modeling: Data Load, Preprocess and Model Build

    Coronavirus Tweets Dataset

    Loading Data with CSV Crate

    Using Rust's String Methods

    Using Bag-of-Words or TF-IDF

    SVM Model Building

    Perform Training and Predictions

    Splitting Data into Training and Test Sets

    Training the SVM Model

    Making Predictions

    Assessing Model Accuracy

    Predictions Evaluation and Hyperparameter Tuning

    Calculating Accuracy

    Calculating Precision and Recall

    Grid Search in Rust

    Dataset and Model Update

    Incremental Learning

    Data Retention Policies

    Versioning Models

    Automating Updates

    Model Rollbacks

    Model Versioning System

    Metadata Storage

    Version Catalog

    Rollback and Testing

    Summary

    Chapter 8: Simplifying Naive Bayes and k-NN

    Naive Bayes and k-NN Overview

    Naive Bayes Classifier Architecture

    Building Naive Bayes Model

    Building k-NN Model

    Naive Bayes and k-NN Model Comparison

    Errors and Troubleshooting

    #1 Model not Converging

    Solution

    #2 File not Found during Serialization/Deserialization

    Enjoying the preview?
    Page 1 of 1