100% found this document useful (5 votes)
64 views

Instant download Deploy Machine Learning Models to Production: With Flask, Streamlit, Docker, and Kubernetes on Google Cloud Platform Pramod Singh pdf all chapter

Deploy

Uploaded by

frumanayel84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (5 votes)
64 views

Instant download Deploy Machine Learning Models to Production: With Flask, Streamlit, Docker, and Kubernetes on Google Cloud Platform Pramod Singh pdf all chapter

Deploy

Uploaded by

frumanayel84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Download the Full Version of textbook for Fast Typing at textbookfull.

com

Deploy Machine Learning Models to Production: With


Flask, Streamlit, Docker, and Kubernetes on Google
Cloud Platform Pramod Singh

https://textbookfull.com/product/deploy-machine-learning-
models-to-production-with-flask-streamlit-docker-and-
kubernetes-on-google-cloud-platform-pramod-singh/

OR CLICK BUTTON

DOWNLOAD NOW

Download More textbook Instantly Today - Get Yours Now at textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Deploy Machine Learning Models to Production: With Flask,


Streamlit, Docker, and Kubernetes on Google Cloud Platform
1st Edition Pramod Singh
https://textbookfull.com/product/deploy-machine-learning-models-to-
production-with-flask-streamlit-docker-and-kubernetes-on-google-cloud-
platform-1st-edition-pramod-singh/
textboxfull.com

Biota Grow 2C gather 2C cook Loucas

https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-loucas/

textboxfull.com

Cloud Native Apps on Google Cloud Platform Use Serverless


Microservices and Containers to Rapidly Build and Deploy
Apps on Google Cloud English Edition Gilchrist
https://textbookfull.com/product/cloud-native-apps-on-google-cloud-
platform-use-serverless-microservices-and-containers-to-rapidly-build-
and-deploy-apps-on-google-cloud-english-edition-gilchrist/
textboxfull.com

Learn PySpark: Build python-based machine learning and


deep learning models 1st Edition Pramod Singh

https://textbookfull.com/product/learn-pyspark-build-python-based-
machine-learning-and-deep-learning-models-1st-edition-pramod-singh/

textboxfull.com
Practical Machine Learning with AWS : Process, Build,
Deploy, and Productionize Your Models Using AWS Himanshu
Singh
https://textbookfull.com/product/practical-machine-learning-with-aws-
process-build-deploy-and-productionize-your-models-using-aws-himanshu-
singh/
textboxfull.com

Getting Started with Containers in Google Cloud Platform :


Deploy, Manage, and Secure Containerized Applications
Shimon Ifrah
https://textbookfull.com/product/getting-started-with-containers-in-
google-cloud-platform-deploy-manage-and-secure-containerized-
applications-shimon-ifrah/
textboxfull.com

Machine Learning with PySpark: With Natural Language


Processing and Recommender Systems 1st Edition Pramod
Singh
https://textbookfull.com/product/machine-learning-with-pyspark-with-
natural-language-processing-and-recommender-systems-1st-edition-
pramod-singh/
textboxfull.com

Practical Machine Learning with AWS Process Build Deploy


and Productionize Your Models Using AWS 1st Edition
Himanshu Singh
https://textbookfull.com/product/practical-machine-learning-with-aws-
process-build-deploy-and-productionize-your-models-using-aws-1st-
edition-himanshu-singh/
textboxfull.com

Beginning MLOps with MLFlow: Deploy Models in AWS


SageMaker, Google Cloud, and Microsoft Azure Sridhar Alla

https://textbookfull.com/product/beginning-mlops-with-mlflow-deploy-
models-in-aws-sagemaker-google-cloud-and-microsoft-azure-sridhar-alla/

textboxfull.com
Deploy Machine
Learning Models
to Production
With Flask, Streamlit, Docker, and
Kubernetes on Google Cloud Platform

Pramod Singh
Deploy Machine
Learning Models to
Production
With Flask, Streamlit, Docker,
and Kubernetes on Google
Cloud Platform

Pramod Singh
Deploy Machine Learning Models to Production
Pramod Singh
Bangalore, Karnataka, India

ISBN-13 (pbk): 978-1-4842-6545-1 ISBN-13 (electronic): 978-1-4842-6546-8


https://doi.org/10.1007/978-1-4842-6546-8

Copyright © 2021 by Pramod Singh


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Celestin Suresh John
Development Editor: Laura Berendson
Coordinating Editor: Aditee Mirashi
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 1
New York Plaza, Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201)
348-4505, email [email protected], or visit www.springeronline.com. Apress Media,
LLC is a California LLC and the sole member (owner) is Springer Science + Business Media
Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for
reprint, paperback, or audio rights, please e-mail [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our
Print and eBook Bulk Sales web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on GitHub via the book’s product page, located at www.apress.com/
978-1-4842-6545-1. For more detailed information, please visit www.apress.com/source-code.
Printed on acid-free paper
Table of Contents
About the Author��������������������������������������������������������������������������������vii

About the Technical Reviewer�������������������������������������������������������������ix


Acknowledgments�������������������������������������������������������������������������������xi
Introduction���������������������������������������������������������������������������������������xiii

Chapter 1: Introduction to Machine Learning���������������������������������������1


History�������������������������������������������������������������������������������������������������������������������2
The Last Decade����������������������������������������������������������������������������������������������3
Rise in Data�����������������������������������������������������������������������������������������������������3
Increased Computational Efficiency����������������������������������������������������������������4
Improved ML Algorithms���������������������������������������������������������������������������������5
Availability of Data Scientists��������������������������������������������������������������������������6
Machine Learning�������������������������������������������������������������������������������������������������6
Supervised Machine Learning�������������������������������������������������������������������������7
Unsupervised Learning����������������������������������������������������������������������������������10
Semi-supervised Learning����������������������������������������������������������������������������11
Reinforcement Learning��������������������������������������������������������������������������������12
Gradient Descent�������������������������������������������������������������������������������������������13
Bias vs. Variance�������������������������������������������������������������������������������������������15
Cross Validation and Hyperparameters���������������������������������������������������������16
Performance Metrics�������������������������������������������������������������������������������������17

iii
Table of Contents

Deep Learning�����������������������������������������������������������������������������������������������������22
Human Brain Neuron vs. Artificial Neuron�����������������������������������������������������23
Activation Functions��������������������������������������������������������������������������������������26
Neuron Computation Example�����������������������������������������������������������������������28
Neural Network���������������������������������������������������������������������������������������������30
Training Process��������������������������������������������������������������������������������������������32
Role of Bias in Neural Networks��������������������������������������������������������������������35
CNN���������������������������������������������������������������������������������������������������������������37
RNN���������������������������������������������������������������������������������������������������������������39
Industrial Applications and Challenges���������������������������������������������������������������48
Retail�������������������������������������������������������������������������������������������������������������48
Healthcare�����������������������������������������������������������������������������������������������������49
Finance����������������������������������������������������������������������������������������������������������50
Travel and Hospitality������������������������������������������������������������������������������������50
Media and Marketing������������������������������������������������������������������������������������51
Manufacturing and Automobile���������������������������������������������������������������������51
Social Media��������������������������������������������������������������������������������������������������52
Others������������������������������������������������������������������������������������������������������������52
Challenges�����������������������������������������������������������������������������������������������������52
Requirements������������������������������������������������������������������������������������������������������53
Conclusion����������������������������������������������������������������������������������������������������������54

Chapter 2: Model Deployment and Challenges�����������������������������������55


Model Deployment����������������������������������������������������������������������������������������������56
Why Do We Need Machine Learning Deployment?���������������������������������������������58
Challenges����������������������������������������������������������������������������������������������������������59
Challenge 1: Coordination Between Stakeholders�����������������������������������������61
Challenge 2: Programming Language Discrepancy���������������������������������������62

iv
Table of Contents

Challenge 3: Model Drift��������������������������������������������������������������������������������62


Challenge 4: On-Prem vs. Cloud-Based Deployment�������������������������������������64
Challenge 5: Clear Ownership�����������������������������������������������������������������������64
Challenge 6: Model Performance Monitoring������������������������������������������������64
Challenge 7: Release/Version Management��������������������������������������������������65
Challenge 8: Privacy Preserving and Secure Model��������������������������������������65
Conclusion����������������������������������������������������������������������������������������������������������66

Chapter 3: Machine Learning Deployment as a Web Service�������������67


Introduction to Flask�������������������������������������������������������������������������������������������68
route Function�����������������������������������������������������������������������������������������������68
run Method����������������������������������������������������������������������������������������������������69
Deploying a Machine Learning Model as a REST Service�����������������������������������69
Templates������������������������������������������������������������������������������������������������������73
Deploying a Machine Learning Model Using Streamlit���������������������������������������76
Deploying a Deep Learning Model����������������������������������������������������������������������81
Training the LSTM Model������������������������������������������������������������������������������������82
Conclusion����������������������������������������������������������������������������������������������������������90

Chapter 4: Machine Learning Deployment Using Docker�������������������91


What Is Docker, and Why Do We Need It?�����������������������������������������������������������92
Introduction to Docker�����������������������������������������������������������������������������������93
Docker vs. Virtual Machines��������������������������������������������������������������������������94
Docker Components and Useful Commands�������������������������������������������������������96
Docker Image������������������������������������������������������������������������������������������������96
Docker Hub��������������������������������������������������������������������������������������������������100
Docker Client and Docker Server����������������������������������������������������������������100
Docker Container�����������������������������������������������������������������������������������������105

v
Table of Contents

Machine Learning Using Docker�����������������������������������������������������������������������110


Step 1: Training the Machine Learning Model���������������������������������������������110
Step 2: Exporting the Trained Model������������������������������������������������������������114
Step 3: Creating a Flask App Including UI����������������������������������������������������115
Step 4: Building the Docker Image��������������������������������������������������������������118
Step 5: Running the Docker Container��������������������������������������������������������119
Step 6: Stopping/Killing the Running Container������������������������������������������126
Conclusion��������������������������������������������������������������������������������������������������������126

Chapter 5: Machine Learning Deployment Using Kubernetes����������127


Kubernetes Architecture�����������������������������������������������������������������������������������128
Kubernetes Master��������������������������������������������������������������������������������������������129
Worker Nodes���������������������������������������������������������������������������������������������������130
ML App Using Kubernetes���������������������������������������������������������������������������������131
Google Cloud Platform��������������������������������������������������������������������������������������132
Conclusion��������������������������������������������������������������������������������������������������������146

Index�������������������������������������������������������������������������������������������������147

vi
About the Author
Pramod Singh is a manager of data science at
Bain & Company. He has more than 11 years
of rich experience in the data science field
working with multiple product- and service-
based organizations. He has been part of
numerous large-scale ML and AI projects. He
has published three books on large-scale data
processing and machine learning. He is also a
regular speaker at major AI conferences such
as O’Reilly AI and Strata.

vii
About the Technical Reviewer
Manohar Swamynathan is a data science
practitioner and an avid programmer, with
14+ years of experience in various data science
areas that include data warehousing, business
intelligence (BI), analytical tool development,
ad hoc analysis, predictive modeling, data
science product development, consulting,
formulating strategy, and executing analytics
programs. He’s had a career covering the
life cycle of data across different domains
such as US mortgage banking, retail/e-commerce, insurance, and
industrial IoT. He has a bachelor’s degree with a specialization in
physics, mathematics, and computers, and a master’s degree in project
management. He’s currently living in Bengaluru, the Silicon Valley of India.
He has also been the technical reviewer of books such as Data Science
Using Python and R.

ix
Acknowledgments
I want to take a moment to thank the most important person in my life: my
wife, Neha. Without her support, this book wouldn’t have seen the light of
day. She is the source of my energy, motivation, and happiness and keeps
me going despite challenges and hardships. I dedicate this book to her.
I also want to thank a few other people who helped a great deal
during these months and provided a lot of support. Let me start with
Aditee, who was very patient and kind to understand the situation and
help to reorganize the schedule. Thanks to Celestian John as well to offer
me another opportunity to write for Apress. Last but not the least, my
mentors: Barron Beranjan, Janani Sriram, Sebastian Keupers, Sreenivas
Venkatraman, Dr. Vijay Agneeswaran, Shoaib Ahmed, and Abhishek
Kumar. Thank you for your continuous guidance and support.

xi
Introduction
This book helps upcoming data scientists who have never deployed any
machine learning model. Most data scientists spend a lot of time analyzing
data and building models in Jupyter Notebooks but have never gotten an
opportunity to take them to the next level where those ML models are
exposed as APIs. This book helps those people in particular who want to
deploy these ML models in production and use the power of these models
in the background of a running application.
The term ML productionization covers lots of components and
platforms. The core idea of this book is not to look at each of the options
available but rather provide a holistic view on the frameworks for
productionizing models, from basic ML-based apps to complex ones.
Once you know how to take an ML model and put it in production, you
will become more confident to work on complicated applications and big
deployments. This book covers different options to expose the ML model
as a web service using frameworks such as Flask and Streamlit. It also
helps readers to understand the usage of Docker in machine learning apps
and the end-to-end process of deployment on Google Cloud Platform
using Kubernetes.
I hope there is some useful information for every reader, and
potentially they can apply it in their workstreams to go beyond Jupyter
Notebooks and productionalize some of their ML models.

xiii
CHAPTER 1

Introduction to
Machine Learning
In this first chapter, we are going to discuss some of the fundamentals
of machine learning and deep learning. We are also going to look at
different business verticals that are being transformed by using machine
learning. Finally, we are going to go over the traditional steps of training
and building a rather simple machine learning model and deep learning
model on a cloud platform (Databricks) before moving on to the next set
of chapters on productionization. If you are aware of these concepts and
feel comfortable with your level of expertise on machine learning already,
I encourage you to skip the next two sections and move on to the last
section, where I mention the development environment and give pointers
to the book’s accompanying codebase and data download information so
that you are able to set up the environment appropriately. This chapter
is divided into three sections. The first section covers the introduction
to the fundamentals of machine learning. The second section dives into
the basics of deep learning and the details of widely used deep neural
networks. Each of the previous sections is followed up by the code to build
a model on the cloud platform. The final section is about the requirements
and environment setup for the remainder of the chapters in the book.

© Pramod Singh 2021 1


P. Singh, Deploy Machine Learning Models to Production,
https://doi.org/10.1007/978-1-4842-6546-8_1
Chapter 1 Introduction to Machine Learning

History
Machine learning/deep learning is not new; in fact, it goes back to 1940s
when for the first time an attempt was made to build something that had
some amount of built-in intelligence. The great Alan Turing worked on
building this unique machine that could decrypt German code during
World War II. That was the beginning of machine intelligence era, and
within a few years, researchers started exploring this field in great detail
across many countries. ML/DL was considered to be significantly powerful
in terms of transforming the world at that time, and an enormous number
of funds were granted to bring it to life. Nearly everybody was very
optimistic. By late 1960s, people were already working on machine vision
learning and developing robots with machine intelligence.
While it all looked good on the surface level, there were some serious
challenges that were impeding the progress in this field. Researchers
were finding it extremely difficult to create intelligence in the machines.
Primarily it was due to a couple of reasons. One of them was that the
processing power of computers in those days was not enough to handle
and process large amounts of data, and the reason was the availability of
relevant data itself. Despite the support of government and the availability
of sufficient funds, the ML/AI research hit a roadblock from the period of
the late 1960s to the early 1990s. This block of time period is also known as
the “AI winters” among the community members.
In the late 1990s, corporations once again became interested in AI.
The Japanese government unveiled plans to develop a fifth-generation
computer to advance machine learning. AI enthusiasts believed that soon
computers would be able to carry on conversations, translate languages,
interpret pictures, and reason like people. In 1997, IBM’s Deep Blue
became the first computer to beat a reigning world chess champion, Garry
Kasparov. Some AI funding dried up when the dot-com bubble burst in the
early 2000s. Yet machine learning continued its march, largely thanks to
improvements in computer hardware.

2
Chapter 1 Introduction to Machine Learning

The Last Decade


There is no denying the fact that the world has seen significant progress
in terms of machine learning and AI applications in the last decade or
so. In fact, if it were to be compared with any other technology, ML/AI
has been path-breaking in multiple ways. Businesses such as Amazon,
Google, and Facebook are thriving on these advancements in AI and are
partly responsible for it as well. The research and development wings
of organizations like these are pushing the limits and making incredible
progress in bringing AI to everyone. Not only big names like these but
thousands of startups have emerged on the landscape specializing in AI-­
based products and services. The numbers only continue to grow as I write
this chapter. As mentioned earlier, the adoption of ML and AI by various
businesses has exponentially grown over the last decade or so, and the
prime reason for this behavior has been multifold.

• Rise in data

• Increased computational efficiency

• Improved ML algorithms

• Availability of data scientists

Rise in Data
The first most prominent reason for this trend is the massive rise in data
generation in the past couple of decades. Data was always present, but
it’s imperative to understand the exact reason behind this abundance of
data. In the early days, the data was generated by employees or workers
of particular organizations as they would save the data into systems, but
there were limited data points holding only a few variables. Then came
the revolutionary Internet, and generic information was made accessible
to virtually everyone using the Internet. With the Internet, the users got

3
Chapter 1 Introduction to Machine Learning

the control to enter and generate their own data. This was a colossal shift
as the total number of Internet users in the world grew at an exploding
rate, and the amount of data created by these users grew at an even
higher rate. All of this data—login/sign-up forms capturing user details,
photos and videos uploads on various social platforms, and other online
activities—led to the coining of the term Big Data. As a result, the challenges
that ML and AI researchers faced in earlier times due to a lack of data points
were completely eliminated, and this proved to be a major enabler for the
adoption of in ML and AI.
Finally, from a data perspective, we have already reached the next level
as machines are generating and accumulating data. Every device around
us is capturing data such as cars, buildings, mobiles, watches, and flight
engines. They are embedded with multiple monitoring sensors and are
recording data every second. This data is even higher in magnitude than the
user-generated data and commonly referred as Internet of Things (IoT) data.

Increased Computational Efficiency


We have to understand the fact that ML and AI at the end of the day
are simply dealing with a huge set of numbers being put together and
made sense out of. To apply ML or AI, there is a heavy need for powerful
processing systems, and we have witnessed significant improvements
in computation power at a breakneck pace. Just to observe the changes
that we have seen in the last decade or so, the size of mobile devices has
reduced drastically, and the speed has increased to a great extent. This
is not just in terms of physical changes in the microprocessor chips for
faster processing using GPUs and TPUs but also in the presence of data
processing frameworks such as Spark. The combination of advancement in
processing capabilities and in-memory computations using Spark made it
possible for lots of ML algorithms to be able to run successfully in the past
decade.

4
Chapter 1 Introduction to Machine Learning

Improved ML Algorithms
Over the last few years, there has been tremendous progress in terms
of the availability of new and upgraded algorithms that have not only
improved the predictions accuracy but also solved multiple challenges that
traditional ML faced. In the first phase, which was a rule-based system,
one had to define all the rules first and then design the system within
those set of rules. It became increasingly difficult to control and update the
number of rules as the environment was too dynamic. Hence, traditional
ML came into the picture to replace rule-based systems. The challenge
with this approach was that the data scientist had to spent a lot of time
to hand design the features for building the model (known as feature
engineering), and there was an upper threshold in terms of predictions
accuracy that these models could never go above no matter if the input
data size increased. The third phase was the introduction of deep neural
networks where the network would figure out the most important features
on its own and also outperform other ML algorithms. In addition, some
other approaches that have been creating a lot of buzz over the last few
years are as follows:

• Meta learning

• Transfer learning (nano nets)

• Capsule networks

• Deep reinforcement learning

• Generative adversarial networks (GANs)

5
Chapter 1 Introduction to Machine Learning

Availability of Data Scientists


ML/AI is a specialized field as the skills required to be able to do this is
indeed a combination of multiple disciplines. To be able to build and apply
ML models, one needs to have a sound knowledge of math and statistics
fundamentals. Along with that, a deep understanding of machine learning
algorithms and various optimization techniques is critical to taking the
right approach to solve a business problem using ML and AI. The next
important skill is to be extremely comfortable at coding, and the last one is
to be an expert of particular domain (finance, retail, auto, healthcare, etc.)
or carry deep knowledge of multiple domains. There is a huge excitement
in the job markets with respect to data scientist roles, and there are a
huge number of requirements for data scientists everywhere, especially in
countries such as the United States, United Kingdom, and India.

M
 achine Learning
Now that we know a little bit of history around machine learning, we can
go over the fundamentals of machine learning. We can break down ML
into four parts, as shown in Figure 1-1.

• Supervised machine learning

• Unsupervised machine learning

• Semi-supervised machine learning

• Reinforcement machine learning

6
Chapter 1 Introduction to Machine Learning

Figure 1-1. Machine learning categories (source: en.proft.me)

Supervised Machine Learning


Supervised machine learning is the major category of machine learning
that drives a lot of applications and value for businesses. In this type of
learning, the model is trained on the data for which we already have the
correct labels or output. In short, we try to map the relationship between
input data and output data in such a way that it can generalize well on
unseen data as well, as shown in Figure 1-2. The training of the model
takes place by comparing the actual output with the predicted output and
then optimizing the function to reduce the total error between the actual
and predicted.

7
Chapter 1 Introduction to Machine Learning

Figure 1-2. Generalization

This type of learning is predominantly used in cases where historical


data is available and predictions need to be made on future data. The
further categorization of supervised learning is based on types of labels
being used for prediction, as shown in Figure 1-3. If the nature of the
output variable is numerical, it falls under regression, whereas if it is
categorical, it is in the classification category.

Figure 1-3. Regression versus classification

8
Chapter 1 Introduction to Machine Learning

Classification refers to the case when the output variable is a discrete


value or categorical in nature. Classification comes in two types.

• Binary classification

• Multiclassification

When the target class is of two categories, it is referred to as binary,


and when it is more than two classes, it is known as multiclassifications, as
shown in Figure 1-4.

Figure 1-4. Binary versus multiclass

Another property of supervised learning is that the model’s


performance can be evaluated. Based on the type of model (classification
or regression), the evaluation metric can be applied, and performance
results can be measured. This happens mainly by splitting the training data
into two sets (the train set and the validation set) and training the model
on the train set and testing its performance on the validation set since we
already know the right label/outcome for the validation set.

9
Chapter 1 Introduction to Machine Learning

U
 nsupervised Learning
Unsupervised learning is another category of machine learning that is used
heavily in business applications. It is different from supervised learning in terms
of the output labels. In unsupervised learning, we build the models on similar
sort of data as of supervised learning except for the fact that this dataset does
not contain any label or outcomes column. Essentially, we apply the model
on the data without any right answers. In unsupervised learning, the machine
tries to find hidden patterns and useful signals in the data that can be later used
for other applications. The main objective is to probe the data and come up
with hidden patterns and a similarity structure within the dataset, as shown in
Figure 1-5. One of the use cases is to find patterns within the customer data and
group the customers into different clusters. It can also identify those attributes
that distinguish between any two groups. From a validation perspective, there
is no measure of accuracy for unsupervised learning. The clustering done by
person A can be totally different from that of person B based on the parameters
used to build the model. There are different types of unsupervised learning.

• K-means clustering

• Mapping of nearest neighbor

Figure 1-5. Clustering


10
Chapter 1 Introduction to Machine Learning

Semi-supervised Learning
As the name suggests, semi-supervised learning lies somewhere in between
supervised and unsupervised learning. In fact, it uses both of the techniques.
This type of learning is mainly relevant in scenarios when we are dealing
with a mixed sort of dataset, which contains both labeled and unlabeled
data. Sometimes it’s just unlabeled data completely, but we label some part
of it manually. The whole idea of semi-supervised learning is to use this
small portion of labeled data to train the model and then use it for labeling
the other remaining part of data, which can then be used for other purposes.
This is also known as pseudo-labeling as it labels the unlabeled data using
the predictions made by the supervised model. To quote a simple example,
say we have lots of images of different brands from social media and most
of it is unlabeled. Now using semi-supervised learning, we can label some
of these images manually and then train our model on the labeled images.
We then use the model predictions to label the remaining images to
transform the unlabeled data to labeled data completely.
The next step in semi-supervised learning is to re-train the model
on entire labeled dataset. The advantage that it offers is that the model
gets trained on a bigger dataset, which was not the case earlier and is
now more robust and better at predictions. The other advantage is that
semi-supervised learning saves a lot of effort and time that could go in to
manually label the data. The flipside of doing all this is that it’s difficult to
get the high performance of the pseudo-labeling as it uses a small part of
the labeled data to make the predictions. However, it is still a better option
rather than manually labeling the data, which can be expensive and time-­
consuming at the same time. This is how semi-supervised learning uses
both the supervised and unsupervised learning to generate the labeled
data. Businesses that face challenges regarding costs associated with the
labeled training process usually go for semi-supervised learning.

11
Chapter 1 Introduction to Machine Learning

Reinforcement Learning
Reinforcement learning is the fourth kind of learning and is little different
in terms of the data usage and its predictions. Reinforcement learning
is a big research area in itself, and an entire book could be written just
on it. The main difference between the other kinds of learning and
reinforcement learning is that we need data, mainly historical data, to train
the models, whereas reinforcement learning works on a reward system,
as shown in Figure 1-6. It is primarily decision-making based on certain
actions that the agent takes to change its state while trying to maximize the
rewards. Let’s break this down to individual elements using a visualization.

Figure 1-6. Reinforcement learning

• Autonomous agent: This is the main character in this


whole learning who is responsible for taking action. If it is
a game, the agent makes the moves to finish or reach the
end goal.

• Actions: These are set of possible steps that the agent


can take to move forward in the task. Each action will
have some effect on the state of the agent and can result
in either reward or penalty. For example, in a game of
tennis, the actions might be to serve, return, move left
or right, etc.

12
Chapter 1 Introduction to Machine Learning

• Reward: This is the key to making progress in


reinforcement learning. Rewards enable the agents
to take actions based on if they’re positive rewards
or penalties. It is an instant feedback mechanism
that differentiates it from traditional supervised and
unsupervised learning techniques.

• Environment: This is the territory in which the agent gets


to play in. The environment decides whether the actions
that the agent takes results in rewards or penalties.

• State: The position the agent is in at any given point of


time defines the state of the agent. To move forward
or reach the end goal, the agent has to keep changing
states in the positive direction to maximize the rewards.

The unique thing about reinforcement learning is that there is an


immediate feedback mechanism that drives the next behavior of the agent
based on a reward system. Most of the applications that use reinforcement
learning are in navigation, robotics, and gaming. However, it can be also
used to build recommender systems.
Now let’s go over some of the important concepts in machine learning
as its critical to have a good understanding of these aspects before moving
on to the machine learning in production.

G
 radient Descent
At the end of the day, the machine learning model is as good as the loss
it’s able to minimize in its predictions. There are different types of loss
functions pertaining to a specific category of problems, and most often in
the typical classification or regression tasks, we try to minimize the mean
squared error and log loss during training and cross validation. If we think
of the loss as a curve, as shown in Figure 1-7, gradient descent helps us to

13
Chapter 1 Introduction to Machine Learning

reach the point where the loss value is at its minimum. We start a random
point based on the initial weights or parameters in the model and move in
the direction where it starts reducing. One thing worth remembering here
is that gradient descent takes big steps when it’s far away from the actual
minima, whereas once it reaches a nearby value, the step sizes become
very small to not miss the minima.
To move toward the minimum value point, it starts with taking the
derivative of the error with respect to the parameters/coefficients (weights
in case of neural networks) and tries to find the point where the slope
of this error curve is equal to zero. One of the important components
in gradient descent is the learning rate as it decides how quickly or
how slowly it descends toward the lowest error value. If learning rate
parameters are set to be higher value, then chances are that it might
skip the lowest value, and on the contrary, if learning rate is too small, it
would take a long time to converge. Hence, the learning rate becomes an
important part in the overall gradient descent process.
The overall aim of gradient descent is to reach to a corresponding
combination of input coefficients that reflect the minimum errors based
on the training data. So, in a way we try to change these coefficient values
from earlier values to have minimum loss. This is achieved by the process
of subtracting the product of the learning rate and the slope (derivative
of error with regard to the coefficient) from the old coefficient value. This
alteration in coefficient values keeps happening until there is no more
change in the coefficient/weights of the model as it signifies that the
gradient descent has reached the minimum value point in the loss curve.

Figure 1-7. Gradient descent

14
Chapter 1 Introduction to Machine Learning

Another type of gradient descent technique is stochastic gradient


descent (SGD), which deals with a similar approach for minimizing the
error toward zero but with sets of data points instead of considering all
data in one go. It takes sample data from input data and applies gradient
descent to find the point of lowest error.

Bias vs. Variance


Bias variance trade-off is the most common problem that gets attention from
data scientists. High bias refers to the situation where the machine learning
model is not learning enough of the signal from the input data and leads to
poor performance in terms of final predictions. In such a case, the model
is too simple to approximate the output based on the given inputs. On the
other hand, high variance refers to overfitting (learning too much on training
data). In the case of high variance, the learning of the model on the training
data affects the generalization performance on the unseen or test data due
to an overcomplex model. One needs to balance the bias versus variance
as both are opposite of each other. In other words, if we increase bias, the
variance goes down, and vice versa, as shown in Figure 1-8.

Figure 1-8. Bias versus variance

15
Chapter 1 Introduction to Machine Learning

Cross Validation and Hyperparameters


For most of the machine learning algorithms out there, there is a set
of hyperparameters that can be adjusted accordingly to have the best
performance coming out of the model. The famous analogy of the
hyperparameters is that of tuning knobs in a radio/transistor to match the
exact frequency of the radio station to hear the sound properly. Likewise,
hyperparameters provide the best possible combination for a model’s
performance for a given training data. The following are a few examples of
hyperparameters in the case of a machine learning model such as random
forest:

• Number of trees

• Maximum number of features

• Maximum depth of trees

For the different values of the previous hyperparameters, the model


would learn the different parameters for the given input data, and the
prediction performance would vary accordingly. Most libraries provide
the default value of these parameters for the vanilla version of the
model, and it’s the responsibility of the data scientist to find out the best
hyperparameters that work in that particular situation. We also have to
be careful that we don’t overfit the data. Now, hyperparameters and cross
validations go hand in hand. Cross validation is a technique where we split
the training data in such a way that the majority of records in the training
set are used to train the model and the remaining set (smaller set) is used
to test the performance of the model. Depending on the type of cross
validation (with repetition or without repetition), the training data is split
accordingly, as shown in Figure 1-9.

16
Chapter 1 Introduction to Machine Learning

Figure 1-9. Cross validation

Performance Metrics
There are different ways in which the performance of a machine learning
model can be evaluated depending on the nature of algorithm used.
As mentioned previously, there are broadly two categories of models:
regression and classification. For the models that predict a continuous
target, such as R-square, root mean squared error (RMSE) can be used,
whereas for the latter, an accuracy measure is the standard metric.
However, the cases where there is class imbalance and the business needs
to focus on only one out of the positive or negative class, measures such as
precision and recall can be used.
Now that we have gone over the fundamentals and important concepts
in machine learning, it’s time for us to build a simple machine learning
model on a cloud platform, namely, Databricks.

17
Chapter 1 Introduction to Machine Learning

Databricks is an easy and convenient way to get started with cloud


infrastructure to build and run machine learning models (single-threaded
as well as distributed). I have given a deep introduction of the Databricks
platform in a couple of my earlier books (Machine Learning Using PySpark
and Learn PySpark). The objective of this section in this chapter is to give
you a flavor of how to get up and running with ML on the cloud by just
signing up for any of the major cloud services providers (Google, Amazon,
Microsoft, Databricks). Most of these platforms allows users to simply sign
up and use the ML services (in some cases with limited capabilities) for a
predefined period or up to the extent of exhausting the free credit points.
Databricks allows you to use the community edition of its platform that
offers up to 6 GB of cluster size. We are going to use the community edition
to build and understand a decision tree model on a fake currency dataset.
The dataset contains four attributes of the currency notes that can be used
to detect whether a currency note is genuine or fake. Since we are using
the community edition, there is a limitation on the size of the dataset, and
hence it’s been kept relatively small for demo purpose.

Note Sign up for the Databricks community edition to run this code.

The first step is to start a new cluster with the default settings as we
are not building a complicated model here. Once the cluster is up and
running, we need to simply upload the data to Databricks from the local
system. The next step is to create a new notebook and attach it to the
cluster we created earlier. The next step is to import all required libraries
and confirm that the data was uploaded successfully.

[In]: import pandas as pd


[In]: import numpy as np
[In]: from sklearn.model_selection import train_test_split
[In]: from sklearn.tree import DecisionTreeClassifier
[In]: from sklearn.metrics import classification_report

18
Another Random Scribd Document
with Unrelated Content
contents either because they found they could get along without
them, or were killed or died, or grew disheartened and made their
way back to the river towns of the Yukon. In only a couple of them
did they find fresh stores and in one of these, curiously enough,
there was a poke5 of gold nuggets. Its owner, in all probability, had
laid it down when he was stocking the cache and forgot to take it
with him when he went.
5 A poke is a small bag usually of deerskin.
Neither did the boys take it, nor disturb the stores in any of the
caches they found, for it is an unwritten law in the barren north that
no man shall touch anything cached which belongs to another.
On the fifth trip out they drove east, or more accurately east by
south, crossed the International boundary line and headed straight
for Mount Burgess forty miles away. As Jack had said, they cared
not whether they found the gold in Alaska, in the Yukon Territory or
on top of the North Pole, as long as they found it. After they had
covered about thirty miles they ran into a scrub forest and the first
thing Jack spied was a pair of moose antlers lashed to a tree.
Both he and Bill thought this a very strange circumstance but they
presently concluded that it had been put there by some hunter
though for what purpose they could not guess. After going half-a-
mile farther into the woods they came to another pair of moose
antlers likewise lashed to a tree; this interested them in dead earnest
and they began to investigate accordingly. Ordinarily when a trail is
blazed through the woods a bit of the bark of the trees is chipped off
at short intervals so that those who go or come cannot go astray but
must find their way there and back, let come whatever may.
But here was a trail blazed differently from any they had ever seen
or heard of, in that at considerable distances apart the antlers of a
moose lashed to a tree pointed the way, but what that way led to
neither Jack nor Bill had the remotest idea. Sometimes the antlers
were so far apart, or led off at such angles, that they had to hunt for
an hour or more for the next one.
“What, I’m askin’ you as man to man, does it mean? Are we gettin’
near it?” questioned Bill, blinking his blue eyes.
“I don’t know,” replied Jack soberly, though hoping against hope
that it was the sign they sought; “but it is queer, isn’t it?”
“Let’s keep right on,” was Bill’s solemn advice.
“Mush on there, you huskies!” yelled Jack; “double rations of fish
for you if we find it.”
“Ten rations of fish, three times a day fer life if we finds it, says I,”
came from Bill.
It is not known positively whether Sate could count up to ten or not
but he gave Bill an awful look which in husky language meant “cut
out that loose talk and maybe each of us will get a piece of fish for
supper anyway,” and with that he and his mates mushed on as fast
as their masters could pick out the trail.
They kept this up the best part of the day when their quest ended
at a log cabin not unlike their own, and over whose door was the
largest pair of bull-moose antlers the boys had ever seen. The boys,
who had been building high their hopes on something far less
tangible than a clew, were disappointed to the quick but they had the
right kind of stuff in them and so never batted an eye.
They were greeted by the barking and howling of many dogs and
what with the noise their own teams made it sounded as if
pandemonium had broken loose. Then Joseph Cook, hunter, trapper,
Indian Agent and sometime gold seeker, otherwise familiarly known
as Bull Moose Joe, for he had brought down more moose than any
other living man, appeared at the door and gave them a warm
welcome.
“But why all the antlers lashed to the trees?” Jack queried after
they had established comrade-like relations.
“I have blazed the trail to my cabin with antlers so that he who
chances this way with his eyes open can find me.”
Bull Moose Joe was a man who stood six foot in his moccasins,
was of medium build and as straight as an Indian. He looked as if he
might have stepped out of the great West in the days of the fifties for
he wore his hair long, had a mustache and a goatee. As usual with
white men up there he must needs have the news from down under,
no matter how stale it was, and then, also as usual, the conversation
just naturally drifted over to the channel of gold. It was then that Bull
Moose Joe gave the boys the greatest jolt they had had in all their
varied but brief career in the gold fields.
“I take it you boys are looking for the same thing I came up to look
for ten years ago,” he said in an off-hand way.
“Yes, it’s gold we’re after,” replied Jack.
“Gold in moosehide sacks piled up like cordwood!” he added,
watching the effect of his words on the boys.
And the effect was truly electrical for their faces became rigid,
their eyes glassed over and they felt the very blood in their arteries
congeal into water-ice.
“And—and—did you find it?” asked Jack when he had recovered
his powers of speech a little.
“Yes, that’s what we want to know,” Bill gurgled as if his gullet was
choked up.
Bull Moose Joe pulled a couple of times on his pipe, watched the
hot smoke ascend and dissolve away just as had his dreams of gold.
He laughed softly. He was in no hurry to answer but to the boys the
moments seemed like an age.
“No,” he said finally, “I never found it though I searched diligently
for it winter and summer for the first five years I was here. I speak
the Hupa tongue which is the tongue of the Athapascans and I
learned to talk it so that I could find out what the Indians knew about
it.
“There was once a tribe of Indians, who lived hereabouts and they
were different from any of the Indians that are living in the Yukon or
Alaska to-day, for they were as fierce and bloodthirsty as the
Apaches down under. Among our natives here there is a legend
about a pocket of gold that was found by these Indians long before
the gold seekers came on to it.
“Then hunters and trappers from the Hudson Bay Company
pushed their way across the desolate wastes of upper Canada and
coming upon this tribe they killed them and took the gold from them.
Before they could get the metal out of the country they were attacked
by the Yeehats, another band of Indians, and, in turn, lost their lives.
These latter Indians cached the gold in a pile of stones but how long
it remained there it is hard to say for the Indians now living seem not
to know.
“Many years after, when men swarmed over Chilcoot Pass and
White Pass like so many black flies, floated down the Yukon River
and on to the Klondike, a miner named John Thornton and a couple
of pards, left the others and pushed farther north. And then, like the
fools for luck they were, they discovered the cache and in it the pile
of nuggets that is worth millions.
“How to get it over to the Yukon River and down under in safety
were their only worries but they were big ones. They were rich
beyond the dreams of the wildest stampeder and so to lessen the
chances of loss by any means they took their time and laid the most
painstaking plans.
“First they hunted the moose and made sacks of the hides; into
these they packed the gold nuggets fifty pounds to the sack, and
there were five hundred sacks which were worth millions. No sooner
had they started than the Yeehats swooped down on them and
although Thornton and his men put up a desperate fight they fell
before the larger number of Indians and the moosehide sacks of gold
stayed right where they found them.
“In a few years the Yeehats as a tribe were practically
exterminated by starvation and disease and so the gold is still here,
but exactly where, no one knows. But sometime it will be found again
and if those who strike it are luckier than the others they will get it
out; but that time has not yet come. To keep me going I began to trap
and hunt and a year or so ago the Minister of the Interior made me
Indian Agent for this part of the Yukon.”
“‘THESE INDIANS CACHED THE GOLD IN A PILE OF STONES.’”

“How did you come to take up moose-hunting?” Jack asked him.


“I calculated that when I found the gold I wouldn’t want to wait until
I killed the moose needed to make the new sacks I should need, so I
began to hunt them long ago and there they are,” and he pointed to
a pile of finished sacks over in the corner. “You see I took time by the
forelock.
“There’s only one other man up here that has any kind of a
reputation as a moose-hunter other than myself and that’s
Moosehide Mike who lives somewhere over in the Klondike River
district. I met him a few years ago at a potlatch but as soon as we
found out that each was looking for the same pot of gold we didn’t hit
it up very well together.”
When the boys left Bull Moose Joe’s cabin they were on pins and
needles, for their thoughts were of the most conflicting nature. Their
belief that the gold was there was now for the first time fixed to a
certainty; on the other hand what ghost of a chance had they of
finding it when an old timer like Bull Moose Joe who had lived there
for years and covered the ground in winter and summer had not
unearthed it?
“We won’t be quitters anyway,” announced Jack, “we’ll keep right
on as per schedule.”
“You said it,” affirmed his partner.
As they had met with quite a few Indians during their sojourn at
Circle and had since run into several Indian villages, the boys had
acquired a fair vocabulary of the Chinook jargon; which is a simple
universal language formed of a lot of heterogeneous words which
every Indian and white man understands and by which they are able
to hold intelligible though limited conversation.
For instance, in the Chinook jargon the word English is called
Boston; to go toward the shore is called Friday; a big lot of anything
is expressed by saying hi-ya; a vile native alcoholic drink is known as
hootchenoo, and from this latter word comes the word hootch which
is used by the frontiersmen everywhere. Do you understand, or you
do understand, is kum-tux; anything to eat is muck-a-muck; a strong
person or animal is skookum; a friend, tillacum, and so on.
With a vocabulary of a couple of dozen words of Chinook the boys
were able to get along fairly well with any of the Indian tribes they
happened to meet. In all of the Indian villages they came to
everything was quiet and peaceful excepting the fiendish howling
and barking of the half-starved dogs. There was nothing to indicate
the cruelty and ferociousness that marked the Yeehats and the
Indians who lived in these parts before them.
Jack and Bill easily made friends with the Indians they came in
contact with for they bought dried fish of them for their teams, gave
them a few provisions where the need was great and Jack always
carried his medicine case and treated the sick for such ailments as
were not beyond his poor ability. These latter he had to leave for the
medicine man, or Shamen, as he is called, to kill or cure.
One afternoon as they neared an Indian village of considerable
size near the head waters of the Tatonduk River they met with whole
families of Indians and on scraping up an acquaintance with some of
them the boys gathered the information that they were going to a
potlatch.
Now about all that the Indians of this region of Alaska do, outside
of trapping and hunting, is to eat, drink and be merry, provided of
course, they have the food and hootchenoo to do it with, for lacking
these integers the resultant product, that is, unalloyed joy, could not
be had. Among the Indians who were going to the potlatch was a
half-breed boy who spoke English a little having learned it from Bull
Moose Joe and other white hunters and trappers, and Jack promptly
annexed him with the gift of a knife.
When Jack asked the lad his name he said that the white men
called him Kloshsky, but that his right name was Montegnard. Now
Klosh in Chinook means good but where the sky came from was not
so easy to guess, unless he was nicknamed by some one of Semitic
persuasion.
Kloshsky told the boys that the potlatch was a hi-yu feast with
hyas fun, and that it was going to be given by a big man of the
Yikyak tribe who wanted to be chief. The word potlatch, he
explained, really means gift and that after much feasting, drinking,
dancing and wrestling the man-who-would-be-chief and whose name
was Montegnais, would give away everything he owned to his
guests.
“Let’s declare ourselves in on this potlatch thing,” said Bill.
“Not a bad idea at all,” admitted Jack. And so they followed the
crowd.
Friends and relatives of the man-who-would-be-chief came from
miles and miles around and the journey finally ended at an Indian
village in the center of which was a big log house nearly as large as
that of the Grand Palace Hotel back at Circle. Into it the visitors
made their way and Jack and Bill went with them.
Talk about the decorations for a Halloween party! why, boy,
nothing a white mind ever conceived of could begin to come up to
the embellishments of this great hall. In the middle there was a
wonderful bird that reached from the floor to the ceiling, nearly, and
the like of which nature had never made in all her seven million
years of experience. From the ceiling there hung curiously shapen
birds, beasts and human beings that for fearsomeness outdid
anything the boys had ever seen. As Bill said, “it was enough to
scare a fellow half-to-death.”
On poles, which were arranged in a circle around the giant bird,
the finest blankets, the costliest furs and other articles prized by the
Indians were displayed and these, Kloshsky told the boys, were the
presents which the man-who-would-be-chief was to give away.
When all had assembled the potlatch came to order. The big man
was gorgeously dressed in ceremonial clothes and carried a long
wand. Around him gathered his lieutenants (they would be so called
down under) and they were also outfitted in ceremonial clothes.
Then came the orchestra which consisted of half-a-dozen men
with their tom-toms. Finally followed the guests who moved about
talking among themselves like society folks at a church fair. From the
man-who-would-be-chief on down to the poorest Indian, all wore the
richest kind of furs, some of them made of the silver fox, and they
were ornamented with various decorations and natural jewelry. Many
of the men and women wore necklaces and belts formed of gold
nuggets as large as hickory nuts and these at once caught the eyes
of the boys. Lo! the poor Indian!
Of all those present there were only two poorly dressed ones and
these were a couple of rank outsiders who had come from down
under and now saw for the first time what Indian high-life really
meant. Jack and Bill felt like a couple of hobos who had tumbled out
of a box-car and landed in the midst of a fancy dress hall in progress
on Fifth Avenue.
When all were assembled the man-who-would-be-chief opened
the potlatch with a recital of the wonderful deeds his ancestors had
done, that his family had done, and especially those that he had
done.
“It’s the same old stuff the politician who wants to be mayor, or
governor, or president pulls in the States,” Bill pointed out.
Then the players began to beat their tom-toms and when the
rhythm of this bombastic music had stirred the souls of the guests to
their very depths, it got them going and they danced for all they were
worth. Most of them carried huge wooden masks that were a
nightmare to look at. Different from our dances their movements
were not regulated by art but by the simple history of their lives and
of those of their ancestors; in other words they were folk-dances.
“I could do that dance as good as any of them if I only had a false-
face,” spoke up Bill, who could see nothing whatever in the energetic
but solemn performance.
“What do you want a false-face for? What’s the matter with the
one you have on?” said Jack, laughing heartily.
“I knew it was purtty bad but I didn’t know it was as bad as all
that,” retorted his partner.
The dance over, the man-who-would-be-chief began to talk to the
spirits of his ancestors. Getting no immediate response he called
upon his guests to wake them up that they might hear what he had
to say to them. He started them off with a large assortment of
terrifying yells and this was augmented by cries, shrieks and
screams of the others until it sounded like a band of renegade
savages rushing to the first onslaught of battle.
Bill wasn’t the least bit afraid of anything happening, because
Jack had told him all of the people in Alaska and the Yukon country,
whatever the color of their exteriors might be, were white at heart.
But his excess of caution just naturally led him to fold his arms so
that his hand wouldn’t be more than half-a-second away from his six-
gun should he need it.
The yelling kept up at a pitch so that a white man could not have
heard himself think and it lasted for fifteen or twenty minutes. Neither
Jack nor Bill took very much stock in what they were yelling for but (it
is sad to relate and hard to believe) the primitive instinct in these
boys overpowered the civilizing influences to which they had been
subjected and time and again they both let loose the awful and
heartrending yi-yi, yi-yi, of the cowboy.
“Oh, Harlem flat, where is thy sting?” said Jack when the yelling
was over.
“You’d think they was a lot o’ cliff-dwellers in Noo York tellin’ the
janitor in soothin’ tones down the dumb-waiter to put on a little more
coal,” commented Bill.
Then came the wrestling matches between those who had been
enemies and, without regard to which one won, when the bout was
over they were good friends again.
“I could throw the two o’ them with me right hand tied back o’ me,
see?” Bill sneered with evident disgust. “Let’s you and me show
these Injuns what a real wrestling bout is, what say, Jack?”
“Don’t get peeved, Bill. This is their game. If you saw a bout in the
New York Athletic Club, or back of the gas-house, you wouldn’t want
to jump in and show the onlookers how it ought to be done, would
you? Just remember that we are only innocent bystanders.”
Next came the big feast and although there were caribou and
rabbit, geese and ptarmigan, still that old standby without which no
Indian feast would be complete had the place of honor.
There was a team of ten roast dogs all hitched up and going to fill
the great void in the principal organ of digestion which existed under
the belt of each redskin. They were hot-dogs in very truth.
“I think I’d better go an’ find out if all our dogs says ‘here’ when I
calls the roll,” said Bill, and not withstanding Jack’s assurances that
these edible dogs were not their sled dogs, Bill went out and counted
up the members of their teams just the same.
After every one had gorged himself, or herself, the man-who-
would-be-chief began to distribute the presents. One of his
lieutenants would call out a name, another would hold the gift before
the person who answered to it, Montegnais would strike the floor
with his wand indicating his pleasure and the gift would be made.
The boys came last and the man-who-would-be-chief asked them
their names. Kloshsky interpreted his wishes to the boys and through
the linguistic ability of this half-breed lad they made known that they
answered to the cognomens of Jack and Bill, the latter from “Noo”
York. Then it was they knew the man-who-would-be-chief for a
gentleman, even if he was a red-skin, for he gave them each a most
wonderful blanket.
When he had given away all of his possessions the potlatch was
over; it was then very near morning but as the boys were tired they
stayed over at the village until the following day.
“Old hatchet face can have my vote, anytime,” proclaimed Bill, as
he admired his trophy.
“You’re a nice American, you are,” said Jack; “selling your vote for
a blanket, eh!”
“There’s a big difference,” proclaimed Bill; “this man-who-wants-
to-be-chief is a heathen savage politician while down in the States
the politicians are civilized Christians. An’ besides they’ve got jails
down there. Get me?”
Just as they were ready to start back Kloshsky, the half-breed boy,
told them it is the custom to return all gifts to the man-who-would-be-
chief within a month and that they must bring his blankets back by
the next moon.
Jack and Bill reluctantly handed over their presents to Kloshsky
and told him to give them back to the man-who-would-be-chief with
their best wishes and kindest personal regards and other nice
felicitations that are usually found on the ends of business letters.
“Mush, you huskies!” yelled Jack and Bill simultaneously while the
Indians, less cheerful than on the night of the potlatch, waved them
their adieus.
“Indian giver,” said Jack when they were beyond earshot.
“I wouldn’t vote for that stingy guy now if he gave me all the
blankets he owns,” groused Bill.
But while they soon forgot the blankets they could not forget the
necklaces and belts of nuggets the Indians wore and they had more
reason than ever to believe they were at the rainbow’s end where it
dipped into pots of pure gold.
CHAPTER X
ON THE TRAIL OF GOLD
“Well, how is old Potlatch this nice, bright, beautiful morning,” Jack
jocularly inquired of his partner after they had started and their
grouches had somewhat subsided.
“No more o’ them things for me,” replied Bill almost amiably.
“We’ve wasted a whole day and we haven’t even got a blanket
between us to show for it. What I was thinkin’ about, though, was the
sacks Bull Moose Joe has made pertainin’ to an’ anticipatin’ the
findin’ of the gold. My one best bet is that we gets the gold first off
and the sacks arterward.”
“Now you’re talking sense, Bill. It just goes to show how all-fired
over confident a fellow can be. Confidence is a good thing but some
people have so much of it they fool themselves. Of course I’ll admit
that it would take a long time to kill enough moose to make twenty or
thirty sacks but a few months more or less wouldn’t make much
difference after we’ve got the metal. Of course if we accidentally
stumbled onto a moose-yard that would be different.”
The boys had hunted the caribou for their fresh meat supplies, in
fact caribou were so plentiful in some districts of the country through
which they passed they seldom had to use their stock provisions,
such as bacon and Alaska strawberries, and as for the dogs, they
waxed fat on the excess of meat they were given and grew sluggish.
There was no need for them to die to get to the happy hunting
grounds—they had attained all that their canine souls could wish for
under these youngsters of great hearts and high courage who were
their masters.
It is no trick at all to shoot a caribou and it is no sport either for if it
is wounded it will not put up a fight. Sport in hunting big game comes
in only when the hunter is exposed to danger and takes a chance of
fighting for his life along with the beast he is trying to kill. And Bill
was right when he said that any man who calls himself a sportsman
and goes after caribou for the mere sake of killing them ought to be
given a spanking and sent back home to his mother.
While Jack was something of a naturalist and knew all about
caribou and their habits Bill was the expert when it came to dressing
them. Bill shot the first caribou and when he brought it into camp he
examined it closely for it was the first one he had ever seen at close
range.
“It looks like a reindeer to me, pard,” he said after eyeing it closely.
“It is a reindeer, for caribou and reindeer are one and the same
animal; the only difference is that reindeer are domesticated and
caribou are wild. Then again there are two kinds of caribou; the one
you’ve brought in is the kind that lives north of sixty-four and this is
called barren ground caribou, while the kind that lives farther south is
called woodland caribou.
“You see the winter coat of this caribou is thick and almost white,
but in summer it takes on a reddish-brown color except underneath
and that stays white. As summer comes on the caribou goes north
and in winter he comes down here to the woodlands. While he is
quite shy yet his curiosity is so great it often gets the best of him and
he will stand and give a fellow the once over until it is sometimes too
late for him to retreat.
“As to speed, why he can beat a dog or a horse all hollow and so
when he is running nothing but a target shot will bring him down.”
“We must get some moose afore we start back for little ole Noo
York. I want to take back the head and antlers of a big un to me goil,
see,” reflected Bill, who was evidently beginning to think of home.
Jack allowed that it might not be a bad scheme to bring down a
moose or two, not merely for trophies of their prowess as big game
hunters, but for the purpose of using their flesh for food, as well as
their hides, in the possible event of their having need for them. Now,
know you, that while in summer the moose usually travels alone, in
winter a number of them will band together and trample down the
snow in a space with their hoofs, and this is called a moose-yard.
Finally, one day, the boys came across tracks leading to a moose-
yard, then quickly made a temporary camp, and struck out to stalk it.
They came upon it just as the moose, of which there were about a
dozen, had reached a small lake. In the yard were two old bull
moose, half-a-dozen cows and the rest calves. The boys crept up on
them until they were within bullet range. The bull moose were
magnificent specimens of wild animal life and must have weighed
more than a thousand pounds apiece.
The boys chose their quarry and then two bullets speeded forth
though the cracks of their Winchesters sounded like a single shot.
They ran toward the moose but the bullets which had crashed into
their great bodies did not kill them or even drop them to the ground.
Instead, the wounded beasts bellowed with rage and as the boys
came up they charged them with mighty fury, their great antlers
cutting the air like so many sabers.
As fast as they were able to get out of the way of one of the bulls,
the other was upon them and they were kept busy dodging, side-
stepping and in devious other ways eluding them. In the skirmish
between the boys and the bulls, the cows and the calves stood off at
some little distance looking on but without the slightest show of any
intention of joining in, for their belief in the power of the bulls to look
after themselves was absolute.
Just as the larger of the bulls was making a final desperate charge
on Jack, he pulled the trigger of his rifle three times with lightning-like
rapidity; the monster moose came to a dead-stop and toppled over,
when a fourth bullet ended him and Jack had his first and only
moose to his credit.
In the meantime Bill was having a hard time of it, for the other bull
pressed him so close he not only could not use his gun but he had to
drop it to save himself. Bill had seen bullfights in Mexico, but a
toreador dodging a bull of the bovine species was as mere child’s
play, he opined, as he afterward said in telling me about it, when
compared with getting away from this mighty animal of the genus
Cervus.
He had also seen, yes, had even performed, that seemingly
superhuman feat known in the cattle country as bulldogging a steer,
which means that a cowboy throws a steer to the ground by grasping
its horns and twisting its neck until the animal falls, but he knew that
this trick would not succeed with the monster he was now pitted
against.
The struggle was going on away from where it started as far as
powder will send a bullet and the moment Jack had killed his moose
he ran to help his partner. Before he got within firing range he saw a
sight that he would not be likely to forget, no, not if he lived to the
century mark. The bull moose had made a terrific lunge at Bill but
instead of pinning him on his horns, or catching and tossing him a
dozen yards or so as is the way of these enraged beasts, the New
York boy had grasped his antlers as he lowered his head and with
the agility of an acrobat, plus the desire to aid and abet the first law
of nature, when the bull’s head went up Bill went with it with his feet
straight up in the air.
In another instant he turned completely over and landed on the
moose’s neck and there he gripped the coarse thatch of hair and
held on with a tenacity of purpose that all of the bull’s cavorting
around could not shake off. Then it was that Bill drew his six-gun and
emptied the contents of it into the head of the great beast, while a
bullet from Jack’s rifle brought him down. Finding their leaders were
no more, the cows and calves turned and fled.
The next thing on the list was to skin the moose, and this was a
very arduous job. Both of the boys, but especially Bill, could almost
out-Indian an Indian when it came to skinning a caribou but out here
where the icy wind was cutting across the lake it was a very
disagreeable task. Before they were through with the work the day
had slipped into night and they had to make their temporary camp
their quarters. After a supper of moose-cutlet they felt much
“sorensified” as Bill expressed it, and he was not so badly off but that
he could play a few chunes, as he called them, on his mouth organ.
They piled the hides, both of which were as large as the largest
buffalo hides, on their sleds, together with as much of the meat of
the carcass of one of the moose as they could carry; this they took
back with them to their permanent camp, and it solved the meat
problem for a very considerable time to come.
While Jack could clean the skins quite as well as his partner, still
the job didn’t agree with his finer sensibilities and he balked on doing
it in true Indian style. Bill was not so particular and he would squat
squaw-like on the floor, lay the skin on his lap, hair-side down, grip
the edge of it with his teeth, and with his left hand under it he easily
and quickly cut and scraped away all the flesh and fat from it with his
knife in the right and never once make a miscue and cut the skin.
“BILL DREW HIS SIX-GUN AND EMPTIED IT INTO THE HEAD OF THE GIANT
BEAST.”

Not satisfied with their experience as big game hunters in bringing


down the moose, the boys pined for a bear. Now while bears are
quite plentiful in many parts of Alaska they seemed to be mighty
scarce in the Yeehat district, though every once in a while the boys
would see the tracks of one. And so it was that Jack and Bill left their
work of seeking gold ever and anon and sought to track, instead, the
bear to his lair.
But their hunt for a bear was very like their hunt for gold in that
they hunted both with vim and determination but neither the bear nor
the gold was anywhere to be found. Yet the boys knew that both
were there if they could only catchee ’em, as Sing Nook would say.
When they came upon the fresh tracks of a bear, as they did once in
a while in crossing lakes or going through the woods, they forewent
their main quest in the hopes of getting a shot at Bruin, but instead
they never even got a look at one.
But bear was not on their minds all of the time. They had been
busy around their permanent camp for several days getting the
moosehides into shape and bear was as remote from their minds as
the prehistoric dinosaur.
One evening Jack was getting supper and Bill had gone over to
the wood-pile, which was a stone’s throw from the cabin, for some
firewood. After he had been gone for a quarter of an hour, or so,
Jack began to wonder what had become of him, inasmuch as he
was waiting for the wood to broil a moose-steak. Another five
minutes elapsed and Jack, who had become impatient, went to the
door to hurry Bill up.
“Going to stay at that wood-pile all day,” he yelled very loud and
not very gently.
No answer from Bill, so Jack went over to see if anything could
have happened. When he got close to the wood-pile he heard
groans and when he came upon his partner he found enough had
happened, and to spare. There was Bill keeled over in the snow
covered with frozen blood while lying up as close to him as two
mortal enemies could get was a big brown bear breathing his last.
Jack lifted his partner to his shoulder and carried him to the cabin
where he gave him first aid and washed him up. Bill was clawed,
chewed, torn and bruised from head to foot and back again. Only for
his fur clothing he must certainly have been killed.
After Jack had attended his partner and made him as comfortable
as possible he went out to the wood-pile and took a look at the bear.
Mr. Bruin had been slashed up quite a bit himself for Jack counted
fifty-six knife wounds in his head and body. He was assuredly a
whopper for he must have weighed in the neighborhood of six
hundred pounds.
Bill lay in his bunk for two days and nights and when he got up he
was still feeling pretty groggy. The first thing he did was to ask for his
“lookin’ glass,” which was a bit of burnished steel of the kind used by
dough-boys in the army. Bill screwed up his face and Jack thought
he was going to cry.
“’Tain’t no use, pard,” he moaned looking at himself.
“No use of what, Bill,” Jack asked sympathetically.
“No use in havin’ a goil. Look at me map now and tells me, as
man to man, could any goil love a guy what’s got one like it. I says
no.”
“A fellow’s face hasn’t anything to do with it. It’s the kind of a
fellow he is down deep in his heart, and the stuff he’s made of, that
counts, not only with his girl, but with the world at large,” urged Jack.
“But look at it. Nobody but a mother could love a face like that,”
proclaimed Bill, and Jack came very near thinking his partner had
spoken rightly.
“Now tell me how it all happened.”
“Well,” began Bill, putting his hand to his forehead, “I remember I
went to the wood-pile and as I was pickin’ up an armful o’ wood I
heard something back of me go woof! woof! I said ’woof, woof
yourself’ and lookin’ ’round I saw this here ornery bear standin’ back
o’ me with his dooks up and ready for a fight. I drops the wood and
lets out an orful holler for you to bring a gun but you musta gone to
sleep on the stove for you didn’t show up.
“Then this here ornery bear makes a reach for me jaw and me
and him had a sprintin’ match ’round the wood-pile. Finally he
catches up with me and lands a gentle little tap on me jaw with his
tremendous right hand and it sent me sprawling. Afore I could get up
he was on top o’ me and I thought I was goin’ to be like the hero o’
that rime for little kids which runs:
‘Algy met a bear;
The bear was bulgy
And the bulge was Algy.’
“I had left me six-gun here in the cabin and I had just sense
enough left to grabs me huntin’ knife when I stabbed him every
chanst I got.
“We rolls over and over until after a while he and me couldn’t roll
over any more and then you comes.”
“Yes, you drove that knife into him fifty-six times by actual count,”
said Jack admiringly.
“One more stab and there’d have been enough for an
advertisement for a pickle factory,” replied Bill.
“You certainly did put him out of commission all right. It must have
been a great fight. I tell you I’d like to have seen it,” allowed Jack
with enthusiasm.
Bill looked up and blinked his eyes at his partner.
“Yes, it was a great fight all right. I’m sorry you missed it and I
wish you could have seen it from the place I did. I allus did prefer
broilin’ moose-steaks as against killin’ a b’ar, and hereafter youse
gets the wood. See?”
So ended their hunt for big game.

Now if you will look at a map of Alaska you will see that the
Porcupine River is like the letter U laid over on its side; that is to say,
its head waters are in Alaska and the stream then flows east over
the International boundary into the Yukon Territory, thence north by
northeast across the Arctic Circle and when it reaches latitude 137
degrees and longitude about 67-1/2 degrees, it makes a sharp bend
and flows back west by southwest for a couple of hundred miles,
when it empties into the Yukon River, between the towns of Beaver
and Fort Yukon.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like