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

Only $11.99/month after trial. Cancel anytime.

Machine Learning in Production: Master the art of delivering robust Machine Learning solutions with MLOps (English Edition)
Machine Learning in Production: Master the art of delivering robust Machine Learning solutions with MLOps (English Edition)
Machine Learning in Production: Master the art of delivering robust Machine Learning solutions with MLOps (English Edition)
Ebook758 pages4 hours

Machine Learning in Production: Master the art of delivering robust Machine Learning solutions with MLOps (English Edition)

Rating: 0 out of 5 stars

()

Read preview

About this ebook

‘Machine Learning in Production’ is an attempt to decipher the path to a remarkable career in the field of MLOps. It is a comprehensive guide to managing the machine learning lifecycle from development to deployment, outlining ways in which you can deploy ML models in production.

It starts off with fundamental concepts, an introduction to the ML lifecycle and MLOps, followed by comprehensive step-by-step instructions on how to develop a package for ML code from scratch that can be installed using pip. It then covers MLflow for ML life cycle management, CI/CD pipelines, and shows how to deploy ML applications on Azure, GCP, and AWS. Furthermore, it provides guidance on how to convert Python applications into Android and Windows apps, as well as how to develop ML web apps. Finally, it covers monitoring, the critical topic of machine learning attacks, and A/B testing.

With this book, you can easily build and deploy machine learning solutions in production.
LanguageEnglish
Release dateApr 29, 2023
ISBN9789355518095
Machine Learning in Production: Master the art of delivering robust Machine Learning solutions with MLOps (English Edition)

Related to Machine Learning in Production

Related ebooks

Intelligence (AI) & Semantics For You

View More

Related articles

Reviews for Machine Learning in Production

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 in Production - Suhas Pote

    Chapter 1

    Python 101

    Introduction

    Python 101 guides you with everything from the installation of Python to data manipulation in Pandas DataFrame. In a nutshell, this chapter is designed to equip you with the prerequisites required for this book. It is a quick refresher for those who have worked with Python. And if you are a beginner, this chapter is going to cover the most common Python commands required to build and deploy machine models.

    Structure

    This chapter covers the following topics:

    Installation of Python

    Hello World!

    Data structures

    Control statements and loops

    Functions

    OOPs

    NumPy

    Pandas

    Objectives

    After studying this chapter, you should be able to install Python on your machine, and store and manage data using common data structures in Python. You should be able to use Python's data processing packages for data manipulation, and you should also know how to create classes, methods, and objects.

    Install Python

    Make sure the Python version you are installing is compatible with the libraries and applications you will work on. Also, maintain the same Python version throughout the project to avoid any errors or exceptions. Here, you will install Python version 3.6.10.

    On Windows and Mac OS

    Here’s the link to install Python 3.6.10:

    https://www.python.org/downloads/release/python-3610/

    On Linux

    Ubuntu 16.10 and 17.04:

    sudo apt update

    sudo apt install python3.6

    Ubuntu 17.10, 18.04 (Bionic) and onward:

    Ubuntu 17.10 and 18.04 already come with Python 3.6 as default. Just run python3 in the terminal to invoke it.

    Install Anaconda

    Anaconda Individual Edition contains conda and Anaconda Navigator, as well as Python and hundreds of scientific packages. When you install Anaconda, all of these will also get installed.

    https://docs.anaconda.com/anaconda/install/

    Note: Review the system requirements listed on the site before installing Anaconda Individual Edition.

    Install code editor

    Any of the following code editors can be chosen; however, the preferred one is Visual Studio Code:

    Visual Studio Code (https://code/visualstudio.com)

    Sublime Text (https://www.sublimetext.com)

    Notepad++ (https://notepad-plus-plus.org/downloads/)

    Hello World!

    Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its simple, easy-to-learn syntax emphasizes readability and therefore, reduces the cost of program maintenance. Python supports modules and packages, which encourages modular programming and code reusability. It is developed under an OSI-approved open-source license, making it freely usable and distributable, even for commercial use. The Python Package Index (PyPI) hosts thousands of third-party modules for Python.

    Open the terminal and type a python to open the Python console.

    Now you are in the Python console.

    Example: 1.1 Hello world in Python

    print(Hello World)

    Hello World

    To come out of the console you can use:

    exit()

    Execution of Python file

    Let’s create a file named hello_world.py and add the print(Hello World) to it.

    Now, run the file in the terminal:

    python hello_world.py

    You should get the following output:

    Hello World

    Data structures

    Data structures are nothing but particular ways of storing and managing data in memory so that it can be easily accessed and modified later. Python comes with a comprehensive set of data structures, which play an important role in programming because they are reusable, easily accessible, and manageable.

    Common data structures

    You will study some of the common data structures in this section.

    Array

    Arrays are collections of homogeneous items. One can use the same data type in a single array.

    Example: 1.2 Array data type

    import array as arr

    my_array = arr.array(i, (1, 2, 3, 4, 5))

    print(my_array)

    array('i', [1, 2, 3, 4, 5])

    print(my_array[1])

    2

    Dictionary

    Dictionaries are defined as comma separated key:value pairs enclosed in curly braces.

    Example: 1.3 Dictionary data type

    my_dict = {'name': 'Adam', 'emp_id': 3521, 'dept':'Marketing'}

    print(my_dict['name'])

    Adam

    List

    It is a collection of heterogeneous items enclosed in square brackets. One can use the same or different data types in a list.

    Example: 1.4 List data type

    my_list=['apple', 4, 'banana', 6]

    print(my_list[0])

    apple

    Set

    A set is a collection of unique (non-duplicate) elements enclosed in curly braces. A set can take heterogeneous elements.

    Example: 1.5 Set data type

    my_set = {3,5,6}

    print(my_set)

    {3, 5, 6}

    my_set = {1, 2.0, (3,5,6), 'Test'}

    print(my_set)

    {1, 2.0, 'Test', (3, 5, 6)}

    String

    A string is used to store text data. It can be represented by single quotes ('') or double quotes ().

    Example: 1.6 String data type

    print('The cat was chasing the mouse')

    The cat was chasing the mouse

    Tuple

    Tuples are collections of elements surrounded by round brackets (optional), and they are immutable, that is, they cannot be changed.

    Example: 1.7 Tuple data type

    my_tuple_1 = ('apple', 4, 'banana', 6)

    print(my_tuple_1[0])

    apple

    Tuple can be declared without round brackets, as follows:

    my_tuple_2 = 1, 2.0, (3,5,6), 'Test'

    print(my_tuple_2[0])

    1

    Control statements and loops

    Python has several types of control statements and loops. Let’s look at them.

    if…else

    The if statement executes a block of code if the specified condition is true. Whereas, the else statement executes a block of code if the specified condition is false. Furthermore, you can use the elif (else if) statement to check the new condition if the first condition is false.

    Syntax:

    if condition1:

    Code to be executed

    elif condition2:

    Code to be executed

    else:

    Code to be executed

    Example: 1.8 If…else control statement

    x = 26

    y = 17

    if(x > y):

    print('x is greater than y')

    elif(x == y):

    print('x and y are equal')

    else:

    print('y is greater than x')

    x is greater than y

    for loop

    It is used when you want to iterate over a sequence or iterable such as a string, list, dictionary, or tuple. It will execute a block of code a certain number of times.

    Syntax:

    for val in sequence:

    Code to be executed

    Example: 1.9 For loop

    num = 2

    for i in range(1, 11):

    print(num, 'X', i, '=', num*i)

    2 X 1 = 2

    2 X 2 = 4

    2 X 3 = 6

    2 X 4 = 8

    2 X 5 = 10

    2 X 6 = 12

    2 X 7 = 14

    2 X 8 = 16

    2 X 9 = 18

    2 X 10 = 20

    In the preceding example, the range() function generates a sequential set of numbers using start, stop, and step parameters.

    while loop

    It is used when you want to execute a block of code indefinitely until the specified condition is met. Furthermore, you can use an else statement to execute a block of code once when the first condition is false.

    Syntax:

    while condition:

    Code to be executed

    Example: 1.10 While loop

    num = 1

    while num<= 10:

    if num % 2 == 0:

    print(num)

    num = num + 1

    2

    4

    6

    8

    10

    pass statement

    In Python, the pass statement acts as a placeholder. If you are planning to add code later in loops, function definitions, class definitions, or if statements, you can write a pass to avoid any errors as it does nothing. It allows developers to write the logic or condition afterward and continue the execution of the remaining code.

    Syntax:

    if condition:

    pass

    Example: 1.11 Pass statement

    num = 10

    if num % 2 == 0:

    pass

    Functions

    Developers use functions to perform the same task multiple times. A function is a block of code that can take arguments, perform some operations, and return values. Python enables developers to use predefined or built-in functions, or they can write user-defined functions to perform a specific task.

    Example: 1.12 Pre-defined or built-in functions

    print(Hello World)

    Hello World # Output

    Example: 1.13 User-defined functions

    a = 5

    b = 2

    # Function definition

    defadd():

    print(a+b)

    # Calling a function

    add()

    # Output

    7

    Object Oriented Programming (OOP)

    Python is an object-oriented language, so everything in Python is an object. Let’s understand its key concepts and their importance:

    Class: A class acts like a template for the objects. It is defined using the class keyword like the def keyword is used while creating a new function. A Python class contains objects and methods, and they can be accessed using the period (.).

    Object: An object is an instance of a class. It depicts the structure of the class and contains class variables, instance variables, and methods.

    Method: In simple words, it is a function defined while creating a class.

    __init__: For the automatic initialization of data members by assigning values, you should use the __init__ method while creating an instance of a class. It gets called every time you create an object of the class. The usage is equivalent to the constructor in C++ and Java.

    Self: It helps us access the methods and attributes of the class. You are free to name it anything; however, as per convention and for readability, it is better to declare it as self.

    Example: 1.14 Class definition

    classMultiply:

    defmul(self):

    result = self.num1 * self.num2Class definition

    return result

    def__init__(self,num1,num2):

    self.num1 = num1

    self.num2 = num2

    a = Multiply(5,6)

    Passing parameters to class

    a.mul() Calling method of class

    30

    Note: Follow standard naming conventions for variables, functions, and methods. For example:

    For variables, functions, methods, packages, and modules: my_variable

    For classes and exceptions: MyClass

    For constants: MY_CONSTANT

    Numerical Python (NumPy)

    In a nutshell, it is an array processing package. NumPy stands for Numerical Python. By default, the data type of the NumPy array is defined by its elements. In NumPy, the dimension of arrays is referred to by rank; for example, a 2-D array means Rank 2 array.

    NumPy is popular because of its speed. While working on a large dataset, you will see the difference if you use NumPy.

    You can install the NumPy package using pip:

    pip install numpy

    Example: 1.15 NumPy array

    import numpy as np

    # Rank 0 Array (scaler)

    my_narray = np.array(42)

    print(my_narray)

    42

    print(Dimension: , my_narray.ndim)

    Dimension: 0

    print(Shape: , my_narray.shape)

    Shape: ()

    print(Size: , my_narray.size)

    Size: 1

    # Rank 1 Array (vector)

    my_narray = np.array([4,5,6])

    print(my_narray)

    [456]

    print(Dimension: , my_narray.ndim)

    Dimension: 1

    print(Shape: , my_narray.shape)

    Shape: (3,)

    print(Size: , my_narray.size)

    Size: 3

    # Rank 2 Array (matrix)

    my_narray = np.array([[1,2,3],[4,5,6]])

    print(my_narray)

    [[123]

    [456]]

    print(Dimension: , my_narray.ndim)

    Dimension: 2

    print(Shape: , my_narray.shape)

    Shape: (2, 3)

    print(Size: , my_narray.size)

    Size: 6

    # Rank 3 Array

    my_narray = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])

    print(my_narray)

    [[[ 123]

    [ 456]]

    [[ 789]

    [101112]]]

    print(Dimension: , my_narray.ndim)

    Dimension: 3

    print(Shape: , my_narray.shape)

    Shape: (2, 2, 3)

    print(Size: , my_narray.size)

    Size: 12

    Reshaping array

    Suppose you want to change the shape of a ndarray (N-dimension array) without losing the data; it can be done using the reshape() or flatten() method.

    Example: 1.16 Reshaping NumPy array

    # Rank 1 Array

    my_1array = np.array([1,2,3,4,5,6])

    print(my_1array)

    [123456]

    # converting Rank 1 array to Rank 2 array

    my_2array = my_1array.reshape(2, 3)

    print(my_2array)

    [[123]

    [456]]

    #converting Rank 2 array to Rank 1 array

    my_1array = my_2array.flatten()

    print(my_1array)

    [123456]

    Note: Here, you can use my_2array.reshape(6) instead of flatten().

    Pandas

    Pandas is an open-source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language. A DataFrame is a 2-dimensional labeled data structure with columns of potentially different types.

    You can install the Pandas package using pip:

    pip install pandas

    Example: 1.17 Pandas DataFrame usage

    import pandas as pd

    df = pd.DataFrame(data={'fruit': ['apple', 'banana', 'orange', 'mango',

    'size': ['large', 'medium', 'small', 'medium'],

    'quantity': [5,4,8, 6]})

    print(df)

    fruit size quantity

    0 apple large 5

    1 banana medium 4

    2 orange small 8

    3 mango medium 6

    In the following example, head() retrieves the top rows of the Pandas DataFrame.

    Example: 1.18 Get first n rows of DataFrame

    df.head(2)

    fruit size quantity

    0 apple large 5

    1 banana medium 4

    Whereas tail() retrieves the bottom rows of the Pandas DataFrame.

    Example: 1.19 Get the last n rows of DataFrame

    df.tail(2)

    fruit size quantity

    2 orange small 8

    3 mango medium 6

    In the following example, describe() shows a quick statistical summary of numerical columns of the DataFrame.

    Example: 1.20 Get basic statistical information

    df.describe()

    quantity

    count 4.000000

    mean 5.750000

    std 1.707825

    min 4.000000

    25% 4.750000

    50% 5.500000

    75% 6.500000

    max 8.000000

    df['fruit']

    0 apple

    1 banana

    2 orange

    3 mango

    Name: fruit, dtype: object

    loc - selection by label

    A loc gets rows (and/or columns) with specific labels. To get all the rows in which fruit size is medium, loc is written as follows:

    Example: 1.21 Get the data by location

    df.loc[df['size'] == 'medium']

    fruit size quantity

    1 banana medium 4

    3 mango medium 6

    iloc - selection by position

    iloc gets rows (and/or columns) at the index’s locations. To get the row at index 2, iloc is written as follows:

    Example: 1.22 Get the data by position

    df.iloc[2]

    fruit orange

    size small

    quantity 8

    Name: 2, dtype: object

    To retrieve the rows from index 2 to 3 and columns 0 to 1, iloc is written as follows:

    df.iloc[2:4, 0:2]

    fruit size

    2 orange small

    3 mango medium

    You can call groupby() and pass the size, that is, the column you want the group on, and then use the sum() aggregate method.

    Example: 1.23 Group the data, then use an aggregate function

    df.groupby(['size'])['quantity'].sum()

    size

    large 5

    medium 10

    small 8

    Name: quantity, dtype: int64

    Save and load CSV files

    Example: 1.24 Save and load CSV files

    df.to_csv(fruit.csv)

    df = pd.read_csv(fruit.csv)

    Save and load Excel files

    Example: 1.25 Save and load Excel files

    df.to_excel(fruit.xlsx, sheet_name=Sheet1)

    df = pd.read_excel(fruit.xlsx, Sheet1, index_col=None)

    For more information, you can refer to the official documentation at https://pandas.pydata.org/docs/.

    Conclusion

    This chapter discussed the essential concepts of the Python language with examples. At the beginning of this chapter, you learned how to install Python in the system. Then, you studied the common data structures and object-oriented programming concepts in Python with examples. Further on, data pre-processing commands were covered using packages like NumPy and Pandas, which will play a major role in developing machine learning models.

    In the next chapter, you will learn about git and GitHub with examples.

    Points to remember

    Ensure to use the same Python version throughout the machine learning project.

    The __init()__ method in a class is used for auto initialization of data members and methods. However, it is not mandatory.

    Tuples are immutable, that is, they are not changeable, but if they contain elements like a list, then the list is mutable but not the entire tuple.

    Multiple choice questions

    What is the output of np.array([[1,2,3],[4,5,6]])?

    Rank 1 array

    Rank 2 array

    Rank 2 array

    All of the above

    Sets can take _________ elements.

    heterogenous

    homogeneous

    only integer

    only string

    Answers

    b

    a

    Questions

    What is the difference between a list and a tuple?

    What is the use of __init__()?

    When should you use a pass statement?

    Key terms

    Object-Oriented Programming: It refers to a programming paradigm based on the concept of objects, which can contain data and methods.

    Join our book's Discord space

    Join the book's Discord Workspace for Latest updates, Offers, Tech happenings around the world, New Release and Sessions with the Authors:

    https://discord.bpbonline.com

    Chapter 2

    Git and GitHub Fundamentals

    Introduction

    It is difficult to maintain multiple versions of files while working in a team. Git solves this problem by allowing developers to collaborate and share files with other team members. This chapter covers the most needed basic Git and GitHub concepts that form the foundation of this book. It explains Git configuration, forking Git repo, pushing your codebase to GitHub, and the cloning process. In addition, it covers an introduction to GitHub Actions, common Git commands with syntax, and examples. Maintaining multiple versions of files when working in a team can be difficult, but Git is the solution.

    Structure

    The following topics will be covered in this chapter:

    Git concepts

    Common Git workflow

    Install Git and create a GitHub account

    Common Git commands

    Let's Git

    Objectives

    After studying this chapter, you should know how to execute Git commands and connect to remote GitHub repositories. Understand and execute Git processes. You should know how to push, fetch, and revert changes to the remote Git repository, and you should be familiar with how to install and perform the basic configuration of Git on your machine or server.

    Git concepts

    Git is an open-source Distributed Version Control System (DVCS). A version control system allows you to record changes to files over a period.

    Git is used to maintain the historical and current versions of source code. In a project, developers have a copy of all versions of the code stored in the central server.

    Git allows developers to do the following:

    Track the changes, who made the changes, and when

    Rollback/restore changes

    Allow multiple developers to coordinate and work on the same files

    Maintain a copy of the files at the remote and local level

    The following image depicts Git as a VCS in a team where developers can work simultaneously on the same files and keep track of who made the changes. Here, F1, F2, and F3 are file names.

    Figure%202.1.PNG

    Figure 2.1: Git scenario in team

    Git + Hub = GitHub

    Git and GitHub are separate entities. Git is a command-line tool, whereas GitHub is a platform for collaboration. You can store files and folders on GitHub and implement changes to existing projects. By creating a separate branch, you can isolate these changes from your existing project files.

    GitHub Actions makes it easy to automate all your software workflows with Continuous Integration/Continuous Deployment. You can build, test, and deploy the code right from GitHub.

    Common Git workflow

    The following figure depicts the general Git workflow. It covers operations like creating or cloning the Git repository, updating the local repository by pulling files from the remote repository, and pushing the local changes to the remote repository.

    Figure%202.2.png

    Figure 2.2: Git workflow

    Install Git and create a GitHub account

    First off, you will need to install Git and then create an account on GitHub. To install Git on your machine, follow the given instructions.

    Linux (Debian/Ubuntu)

    In Ubuntu, open the terminal and install Git using the following commands:

    $ sudo apt-get update

    $ sudo apt-get install git

    Git for all platforms

    Download Git for your machine from the official website:

    https://git-scm.com/downloads

    GUI clients

    Git comes with built-in GUI tools; however, you can explore other third-party GUI tools at https://git-scm.com/downloads/guis.

    Click on the download link for your operating system and then follow the installation steps.

    After installing it, start your terminal and type git --version to verify the Git installation.

    If everything goes well, you will see the Git installed successfully.

    Create a GitHub account

    If you are new to GitHub, then can join GitHub at https://github.com/join.

    If you’re an existing user, sign in to your account.

    Create a new repository by clicking on the plus sign + (top-right corner):

    Figure%202.3.png

    Figure 2.3: Creating a new repository on GitHub

    Hit the Create repository button. Complete the required fields, and your Git repo will be created.

    You can download and install the GitHub desktop from https://desktop.github.com/.

    Common Git commands

    Some of the common Git commands are discussed in this section.

    Setup

    Set a name that is identifiable for credit when reviewing the version history:

    git config --global user.name [firstname lastname]

    Set an email address that will be associated with each history marker:

    git config --global user.email [valid-email-id]

    New repository

    Initialize an existing directory as a Git repository:

    git init

    Retrieve an entire repository from a hosted location via URL:

    git clone [url]

    Update

    Fetch and merge any commits from the remote branch:

    git pull

    Fetch all the branches from the remote Git repo:

    git fetch [alias]

    Changes

    View modified files in the working directory staged for your next commit:

    git status

    Add a file to your next commit (stage):

    git add [file]

    Commit your staged content as a new commit snapshot:

    git commit -m [descriptive message]

    Transfer local branch commits to the remote repository branch:

    git push [alias] [branch]

    Revert

    View all the commits in the current branch’s history:

    git log

    Switch to another branch:

    git checkout ['branch_name']

    Let’s Git

    Create a new directory code and switch to the code directory:

    suhas@test:~/code$ sudo chmod -R 777 /home/suhas/code

    Configuration

    Provide the username and the user’s email:

    suhas@test:~/code$ git config --global user.name Suhas

    suhas@test:~/code$ git config --global user.email "suhasp.ds@gmail.com"

    Note: Always read the output of commands to know what happened in the background.

    Initialize the Git repository

    suhas@test:~/code$ git init

    Initialized empty Git repository in /home/suhas/code/.git/

    Check Git status

    suhas@test:~/code$ git status

    On branch master

    No commits yet

    nothing to commit (create/copy files and use git add to track)

    Add a new file

    A new file hello.py has been added using the touch command:

    suhas@test:~/code$ touch hello.py

    You can notice the change in the output of the git status:

    suhas@test:~/code$ git status

    On branch master

    No commits yet

    Untracked files:

    (use git add ... to include in what will be committed)

    hello.py

    nothing added to commit but untracked files present (use git add to track)

    Add the hello.py file to staging:

    suhas@test:~/code$ git add hello.py

    Again, check the output of the Git status:

    suhas@test:~/code$ git status

    On branch master

    No commits yet

    Changes to be committed:

    (use git rm --cached ... to unstage)

    new file: hello.py

    Commit with the message New file:

    suhas@test:~/code$ git commit -m New file

    [master (root-commit) bd49a5e] New file

    1 file changed, 0 insertions(+), 0 deletions(-)

    create mode 100755 hello.py

    Check the output of Git status:

    suhas@test:~/code$ git status

    On branch master

    nothing to commit, working tree clean

    Update the hello.py file using the nano command:

    suhas@test:~/code$ sudo nano hello.py

    Now, add the print (Hello Word!) to the hello.py file.

    You can see the changes in the output of the git status:

    suhas@test:~/code$ git status

    On branch master

    Changes not staged for commit:

    (use git add ... to update what will be committed)

    (use git checkout -- ... to discard changes in working directory)

    modified: hello.py

    no changes added to commit (use git add and/or git commit -a)

    Add the hello.py file to staging:

    suhas@test:~/code$ git add hello.py

    Commit along with the message added print text:

    suhas@test:~/code$ git commit -m added print text

    [master af7cfeb] added print text

    1 file changed, 1 insertion(+)

    View the logs using the git log command:

    suhas@test:~/code$ git log

    commit af7cfeb53ff6dc49126d24aedb20a065c54ef4a0 (HEAD -> master)

    Author: Suhas Pote <suhasp.ds@gmail.com>

    Date: Sun Jul 5 21:34:15 2020 +0530

    added print text

    commit bd49a5e5280de35811848a80299d900e6e6509ce

    Author: Suhas Pote <suhasp.ds@gmail.com>

    Date: Sun Jul 5 21:26:12 2020 +0530

    New

    Enjoying the preview?
    Page 1 of 1