Behavior Analysis With Machine Learning and R A Sensors and Data Driven Approach Enrique Garcia Ceja PDF Download
Behavior Analysis With Machine Learning and R A Sensors and Data Driven Approach Enrique Garcia Ceja PDF Download
https://ebookbell.com/product/behavior-analysis-with-machine-
learning-and-r-a-sensors-and-data-driven-approach-enrique-garcia-
ceja-33377446
https://ebookbell.com/product/behavior-analysis-with-machine-learning-
using-r-1st-edition-ceja-36103552
https://ebookbell.com/product/applied-behavior-analysis-for-children-
with-autism-spectrum-disorders-1st-edition-johnny-l-matson-4391242
Applied Behavior Analysis For Teachers With Etext Access Code Ninth
Edition Paul A Alberto Anne C Troutman
https://ebookbell.com/product/applied-behavior-analysis-for-teachers-
with-etext-access-code-ninth-edition-paul-a-alberto-anne-c-
troutman-10008822
https://ebookbell.com/product/handbook-of-applied-behavior-analysis-
for-children-with-autism-clinical-guide-to-assessment-and-
treatment-1st-edition-johnny-l-matson-50440210
Applied Behavior Analysis Treatment Of Violence And Aggression In
Persons With Neurodevelopmental Disabilities James K Luiselli
https://ebookbell.com/product/applied-behavior-analysis-treatment-of-
violence-and-aggression-in-persons-with-neurodevelopmental-
disabilities-james-k-luiselli-46099444
https://ebookbell.com/product/applied-behavior-analysis-treatment-of-
violence-and-aggression-in-persons-with-neurodevelopmental-
disabilities-james-k-luiselli-43644832
https://ebookbell.com/product/applied-behavior-analysis-treatment-of-
violence-and-aggression-in-persons-with-neurodevelopmental-
disabilities-james-k-luiselli-43644830
https://ebookbell.com/product/the-verbal-behavior-analysis-inducing-
and-expanding-new-verbal-capabilities-in-children-with-language-
delays-r-douglas-greer-denise-f-ross-10466066
https://ebookbell.com/product/autism-spectrum-disorders-triumph-over-
with-ayurveda-and-applied-behavior-analysis-aba-ramachandran-
sk-46334784
Behavior Analysis with Machine Learning and R
A Sensors and Data Driven Approach
Enrique Garcia Ceja
To Darlene.
Contents
Welcome ix
Preface xi
Supplemental Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii
1 Introduction 1
1.1 What is Machine Learning? . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Types of Machine Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3.1 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3.2 Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3.3 Predictive Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.4 Data Analysis Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.5 Evaluating Predictive Models . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.6 Simple Classification Example . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.6.1 K-fold Cross-Validation Example . . . . . . . . . . . . . . . . . . . . 23
1.7 Simple Regression Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.8 Underfitting and Overfitting . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
1.9 Bias and Variance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
1.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
iii
CONTENTS iv
B Datasets 350
B.1 COMPLEX ACTIVITIES . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
B.2 DEPRESJON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
B.3 ELECTROMYOGRAPHY . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
B.4 FISH TRAJECTORIES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
B.5 HAND GESTURES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
B.6 HOME TASKS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
B.7 HOMICIDE REPORTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
B.8 INDOOR LOCATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
B.9 SHEEP GOATS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
B.10 SKELETON ACTIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
B.11 SMARTPHONE ACTIVITIES . . . . . . . . . . . . . . . . . . . . . . . . . 354
B.12 SMILES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
B.13 STUDENTS’ MENTAL HEALTH . . . . . . . . . . . . . . . . . . . . . . . . 354
This book aims to provide an introduction to machine learning concepts and algorithms
applied to a diverse set of behavior analysis problems. It focuses on the practical aspects of
solving such problems based on data collected from sensors or stored in electronic records.
The included examples demonstrate how to perform several of the tasks involved during a
data analysis pipeline such as: data exploration, visualization, preprocessing, representation,
model training/validation, and so on. All of this, using the R programming language and
real-life datasets.
Some of the content that you will find here includes, How To:
ix
CONTENTS x
The front cover depicts two brothers (Biås and Biranz) in what seems to be a typical
weekend. They are exploring and enjoying nature as usual. What they don’t know is that
their lives are about to change and there is no turning back. Suddenly, Biranz spots a strange
object approaching them. As it makes its way out of the rocks, its entire figure is revealed.
The brothers have never seen anything like that before. The closest similar image they have
in their brains is a hand-sized carnivorous plant they saw at the botanical garden during a
school visit several years ago. Without any warning, the creature releases a load of spores
into the air. Today, the breeze is not on the brothers’ side and the spores quickly enclose
them. After seconds of being exposed, their bodies start to paralyze. Moments later, they
can barely move their pupils. The creature’s multiple stems begin to inflate and all of a
sudden, multiple thorns are shot. Horrified and incapable, the brothers can only witness
how the thorns approach their bodies and they can even hear how the air is being cut by
the sharp thorns. At this point, they are only waiting for the worst. …they haven’t felt
any impact. Has time just stopped? No—the thorns were repelled by what appears to be
a bionic dolphin emitting some type of ultrasonic waves. One of the projectiles managed
to dodge the sound defense and is heading directly to Biås. While flying almost one mile
above sea level, an eagle aims for the elusive thorn and destroys it with surgical precision.
However, the creature is being persistent with its attacks. Will the brothers escape from this
crossfire battle?
About me. My name is Enrique and I am a researcher at SINTEF4 . Please feel free to
e-mail me with any questions/comments/feedback, etc.
e-mail:
twitter: e_g_mx
website: http://www.enriquegc.com
4
https://www.sintef.no/en/
Preface
Automatic behavior monitoring technologies are becoming part of our everyday lives thanks
to advances in sensors and machine learning. The automatic analysis and understanding
of behavior are being applied to solve problems in several fields, including health care,
sports, marketing, ecology, security, and psychology, to name a few. This book provides
a practical introduction to machine learning methods applied to behavior analysis with the
R programming language. The book does not assume any previous knowledge in machine
learning. You should be familiar with the basics of R and some knowledge in basic statistics
and high school-level mathematics would be beneficial.
Supplemental Material
Supplemental material consists of the examples’ code and datasets. The source code for the
examples can be downloaded from https://github.com/enriquegit/behavior-code. Instruc-
tions on how to set up the code and get the datasets are in Appendix A. A reference for all
the utilized datasets is in Appendix B.
Conventions
DATASET names are written in uppercase italics. Functions are referred to by their name
followed by parenthesis and omitting their arguments, for example: myFunction(). Class
labels are written in italics and between single quotes: ‘label1’. The following icons are used
to provide additional contextual information:
xi
CONTENTS xii
Interactive shiny app available. Please see Appendix A for instructions on how to run
shiny apps.
The folder icon will appear at the beginning of a section (if applicable) to indicate which
scripts were used for the corresponding examples.
Acknowledgments
I want to thank Michael Riegler, Jaime Mondragon y Ariana, Viviana M., Linda Sicilia,
Anton Aguilar, Aleksander Karlsen, my former master’s and PhD. advisor Ramon F. Brena,
and my colleagues at SINTEF.
The examples in this book rely heavily on datasets. I want to thank all the people that made
all their datasets used here publicly available. I want to thank Vance Capley who brought
to life the front cover and comic illustrations.
Chapter 1
Introduction
Living organisms are constantly sensing and analyzing their surrounding environment. This
includes inanimate objects but also other living entities. All of this is with the objective of
making decisions and taking actions, either consciously or unconsciously. If we see someone
running, we will react differently depending on whether we are at a stadium or in a bank.
At the same time, we may also analyze other cues such as the runner’s facial expressions,
clothes, items, and the reactions of the other people around us. Based on this aggregated
information, we can decide how to react and behave. All this is supported by the organisms’
sensing capabilities and decision-making processes (the brain and/or chemical reactions).
Understanding our environment and how others behave is crucial for conducting our everyday
life activities and provides support for other tasks. But, what is behavior? The Cambridge
dictionary defines behavior as:
Both definitions are very similar and include humans and animals. Following those defini-
tions, this book will focus on the automatic analysis of human and animal behaviors. There
are three main reasons why one may want to analyze behaviors in an automatic manner:
1. React. A biological or an artificial agent (or a combination of both) can take actions
based on what is happening in the surrounding environment. For example, if suspicious
behavior is detected in an airport, preventive actions can be triggered by security
1
CHAPTER 1. INTRODUCTION 2
systems and the corresponding authorities. Without the possibility to automate such
a detection system, it would be infeasible to implement it in practice. Just imagine
trying to analyze airport traffic by hand.
Some behaviors can be used as a proxy to understand other behaviors, states, and/or pro-
cesses. For example, detecting body movement behaviors during a job interview could serve
as the basis to understand stress levels. Behaviors can also be modeled as a composition
of lower-level behaviors. In chapter 7, a method called Bag of Words that can be used to
decompose complex behaviors into a set of simpler ones will be presented.
In order to analyze and monitor behaviors, we need a way to observe them. Living organ-
isms use their available senses such as eyesight, hearing, smell, echolocation (bats, dolphins),
thermal senses (snakes, mosquitoes), etc. In the case of machines, they need sensors to ac-
complish or approximate those tasks. For example, RGB and thermal cameras, microphones,
temperature sensors, and so on.
The reduction in the size of sensors has allowed the development of more powerful wearable
devices. Wearable devices are electronic devices that are worn by a user, usually as acces-
sories or embedded in clothes. Examples of wearable devices are smartphones, smartwatches,
fitness bracelets, actigraphy watches, etc. These devices have embedded sensors that allow
them to monitor different aspects of a user such as activity levels, blood pressure, temper-
ature, and location, to name a few. Examples of sensors that can be found in those devices
are accelerometers, magnetometers, gyroscopes, heart rate, microphones, Wi-Fi, Bluetooth,
Galvanic Skin Response (GSR), etc.
Several of those sensors were initially used for some specific purposes. For example, ac-
celerometers in smartphones were intended to be used for gaming or detecting the device’s
orientation. Later, some people started to propose and implement new use cases such as
activity recognition (Shoaib et al., 2015) and fall detection. The magnetometer, which mea-
sures the earth’s magnetic field, was mainly used with map applications to determine the
orientation of the device, but later, it was found that it can also be used for indoor location
purposes (Brena et al., 2017).
In general, wearable devices have proven success in tracking different types of behaviors such
as physical activity, sports activities, location, and even mental health states (Garcia-Ceja
et al., 2018c). These sensors generate a lot of raw data, but it will be our task to process
and analyze it. Doing it by hand becomes impossible given the large amounts and available
formats in which data is available. Thus, in this book, several machine learning methods will
be introduced to extract and analyze different types of behaviors from data. The next section
begins with an introduction to machine learning. The rest of this chapter will introduce the
required machine learning concepts before we start analyzing behaviors in chapter 2.
CHAPTER 1. INTRODUCTION 4
This simple rule should work well and will do the job. Imagine that now your boss tells you
that the system needs to recognize green apples as well. Our previous rule will no longer
work, and we will need to include additional rules and thresholds. On the other hand, a
machine learning algorithm will automatically learn such rules based on the updated data.
So, you only need to update your data with examples of green apples and “click” the re-train
button!
The result of learning is knowledge that the system can use to solve new instances of a
problem. In this case, when you show a new image to the system it should be able to
recognize the type of fruit. Figure 1.2 shows this general idea.
For more formal definitions of machine learning, I recommend you check (Kononenko
and Kukar, 2007).
• Data
• Algorithms
• Models
Every machine learning method needs data to learn from. For the example of the fruits, we
need examples of images for each type of fruit we want to recognize. Additionally, we need
their corresponding output types (labels) so the algorithm can learn how to associate each
image with its corresponding label.
Not every machine learning method needs the expected output or labels (more on this
in the Taxonomy section 1.2).
Typically, an algorithm will use the data to learn a model. This is called the learning or
training phase. The learned model can then be used to generate predictions when presented
with new data. The data used to train the models is called the train set. Since we need a
way to test how the model will perform once it is deployed in a real setting (in production),
we also need what is known as the test set. The test set is used to estimate the model’s
performance on data it has never seen before (more on this will be presented in section 1.5).
Machine learning methods can be grouped into different types. Figure 1.3 depicts a cate-
gorization of machine learning ‘types’. This figure is based on (Biecek et al., 2012). The x
axis represents the certainty of the labels and the y axis the percent of training data that
is labeled. In our previous example, the labels are the fruits’ names associated with each
image.
From the figure, four main types of machine learning methods can be observed:
• Supervised learning. In this case, 100% of the training data is labeled and the
certainty of those labels is 100%. This is like the fruits example. For every image
used to train the system, the respective label is also known and there is no uncertainty
about the label. When the expected output is a category (the type of fruit), this is
called classification. Examples of classification models (a.k.a classifiers) are decision
trees, k-nearest neighbors, random forest, neural networks, etc. When the output is
a real number (e.g., predict temperature) it is called regression. Examples of re-
gression models are linear regression, regression trees, neural networks, random forest,
CHAPTER 1. INTRODUCTION 6
k-nearest neighbors, etc. Note that some models can be used for both classification
and regression. A supervised learning problem can be formalized as follows:
f (x) = y (1.1)
where f is a function that maps some input data x (for example images) to an output y
(types of fruits). Usually, an algorithm will try to learn the best model f given some data
consisting of n pairs (x, y) of examples. During learning, the algorithm has access to the
expected output/label y for each input x. At inference time, that is, when we want to make
predictions for new examples, we can use the learned model f and feed it with a new input
x to obtain the corresponding predicted value y.
• Semi-supervised learning. This is the case when the certainty of the labels is 100%
but not all training data are labeled. Usually, this scenario considers the case when
only a very small proportion of the data is labeled. That is, the dataset contains pairs
of examples of the form (x, y) but also examples where y is missing (x, ?). In supervised
learning, both x and y must be present. On the other hand, semi-supervised algorithms
can learn even if some examples are missing the expected output y. This is a common
situation in real life since labeling data can be expensive and time-consuming. In the
fruits example, someone needs to tag every training image manually before training
a model. Semi-supervised learning methods try to extract information also from the
CHAPTER 1. INTRODUCTION 7
• Unsupervised learning. This is the extreme case when none of the training examples
have a label. That is, the dataset only has pairs (x, ?). Now, you may be wondering: If
there are no labels, is it possible to extract information from these data? The answer
is yes. Imagine you have fruit images with no labels. What you could try to do is to
automatically group them into meaningful categories/groups. The categories could be
the types of fruits themselves, i.e., trying to form groups in which images within the
same category belong to the same type. In the fruits example, we could infer the true
types by visually inspecting the images, but in many cases, visual inspection is difficult
and the formed groups may not have an easy interpretation, but still, they can be very
useful and can be used as a preprocessing step (like in vector quantization). These
types of algorithms that find groups (hierarchical groups in some cases) are called
clustering methods. Examples of clustering methods are k-means, k-medoids, and
hierarchical clustering. Clustering algorithms are not the only unsupervised learning
methods. Association rules, word embeddings, and autoencoders are examples of other
unsupervised learning methods. Some people may disagree that word embeddings
and autoencoders are not fully unsupervised methods but for practical purposes, this
categorization is not relevant.
contextual information about a given user such as location, current activity, mood, and so
on. Unsupervised learning methods (clustering and association rules) will be covered as well
in chapter 6.
1.3 Terminology
This section introduces some basic terminology that will be helpful for the rest of the book.
1.3.1 Tables
Since data is the most important ingredient in machine learning, let’s start with some related
terms. First, data needs to be stored/structured so it can be easily manipulated and pro-
cessed. Most of the time, datasets will be stored as tables or in R terminology, data frames.
Figure 1.4 shows the mtcars dataset stored in a data frame.
Columns represent variables and rows represent examples also known as instances or data
points. In this table, there are 5 variables mpg, cyl, disp, hp and the model (the first column).
In this example, the first column does not have a name, but it is still a variable. Each row
represents a specific car model with its values per variable. In machine learning terminology,
rows are more commonly called instances whereas in statistics they are often called data
points or observations. Here, those terms will be used interchangeably.
Figure 1.5 shows a data frame for the iris dataset which consists of different kinds of plants.
Suppose that we are interested in predicting the Species based on the other variables. In
machine learning terminology, the variable of interest (the one that depends on the others)
is called the class or label for classification problems. For regression, it is often referred to
as y. In statistics, it is more commonly known as the response, dependent, or y variable, for
both classification and regression.
In machine learning terminology, the rest of the variables are called features or attributes. In
statistics, they are called predictors, independent variables, or just X. From the context, most
of the time it should be easy to identify dependent from independent variables regardless of
the used terminology.
When working with machine learning algorithms, the following are the most commonly used
variable types. Here, when I talk about variable types, I do not refer to programming-
language-specific data types (int, boolean, string, etc.) but to more general types regardless
of the underlying implementation for each specific programming language.
values (categories). For example, the categorical variable color can take the values
“red”, “green”, “black”, and so on. Categorical variables do not have an ordering.
• Integer: Integer values such as number of rooms, age, number of floors, etc.
• Ordinal: Similar to categorical variables, these take their values from a set of discrete
values, but they do have an ordering. For example, low < medium < high.
In machine learning terminology, a predictive model is a model that takes some input and
produces an output. Classifiers and Regressors are predictive models. I will use the terms
classifier/model and regressor/model interchangeably.
• Data exploration. This step aims to familiarize yourself and understand the data so
you can make informed decisions during the following steps. Some of the tasks involved
in this phase include summarizing your data, generating plots, validating assumptions,
and so on. During this phase you can, for example, identify outliers, missing values,
or noisy data points that can be cleaned in the next phase. Chapter 4 will introduce
some data exploration techniques. Throughout the book, we will also use some other
data exploratory methods but if you are interested in diving deeper into this topic,
I recommend you check out the “Exploratory Data Analysis with R” book by Peng
(2016).
• Data cleaning. After the data exploration phase here, we can remove the identified
outliers, remove noisy data points, remove variables that are not needed for further
computation, and so on.
• Training and evaluation. Once the data is preprocessed, we can then proceed to
train the models. Furthermore, we also need ways to evaluate their generalization
performance on new unseen instances. The purpose of this phase is to try, and fine-
tune different models to find the one that performs the best. Later in this chapter,
some model evaluation techniques will be introduced.
pipeline. This applies to both classification and regression problems. I’m starting with this
topic because it will be a recurring one every time you work with machine learning. You will
also be training a lot of models, but you will need ways to validate them as well.
Once you have trained a model (with a training set), that is, finding the best function f
that maps inputs to their corresponding outputs, you may want to estimate how good the
model is at solving a particular problem when presented with examples it has never seen
before (that were not part of the training set). This estimate of how good the model is at
predicting the output of new examples is called the generalization performance.
To estimate the generalization performance of a model, a dataset is usually divided into a
train set and a test set. As the name implies, the train set is used to train the model (learn its
parameters) and the test set is used to evaluate/test its generalization performance. We need
independent sets because when deploying models in the wild, they will be presented with
new instances never seen before. By dividing the dataset into two subsets, we are simulating
this scenario where the test set instances were never seen by the model at training time so
the performance estimate will be more accurate rather than if we used the same set to train
and evaluate the performance. There are two main validation methods that differ in the way
the dataset is divided: hold-out validation and k-fold cross validation.
1) Hold-out validation. This method randomly splits the dataset into train and test sets
based on some predefined percentages. For example, randomly select 70% of the instances
and use them as the train set and use the remaining 30% of the examples for the test set. This
will vary depending on the application and the amount of data, but typical splits are 50/50
and 70/30 percent for the train and test sets, respectively. Figure 1.7 shows an example of
a dataset divided into 70/30.
Then, the train set is used to train (fit) a model, and the test set to evaluate how well that
CHAPTER 1. INTRODUCTION 13
model performs on new data. The performance can be measured using performance metrics
such as the accuracy for classification problems. The accuracy is the percent of correctly
classified instances.
It is a good practice to estimate the performance on both, the train and test sets.
Usually, the performance on the train set will be higher since the model was trained
with that very same data. It is also common to measure the performance computing the
error instead of accuracy. For example, the percent of misclassified instances. These
are called the train error and test error (also known as the generalization error), for
both the train and test sets respectively. Estimating these two errors will allow you
to ‘debug’ your models and understand if they are underfitting or overfitting (more on
this in the following sections).
and features. All those changes affect the final performance. If all those hyperparameter
changes are evaluated using the test set, there is a risk of overfitting the model. That is,
making the model very specific to this particular data. Instead of using the test set to fine-
tune parameters, a validation set needs to be used instead. Thus, the dataset is randomly
partitioned into three subsets: train/validation/test sets. The train set is used to train
the model. The validation set is used to estimate the model’s performance while trying
different hyperparameters and preprocessing methods. Once you are happy with your final
model, you use the test set to assess the final generalization performance and this is what you
report. The test set is used only once. Remember that we want to assess performance
on unseen instances. When using k-fold cross validation, first, an independent test set needs
to be put aside. Hyperparameters are tuned using cross-validation and the test set is used
at the very end and just once to estimate the final performance.
When working with multi-user systems, we need to additionally take into account
between-user differences. In those situations, it is advised to perform extra validations.
Those multi-user validation techniques will be covered in chapter 9.
CHAPTER 1. INTRODUCTION 15
simple_model.R
So far, a lot of terminology and concepts have been introduced. In this section, we will
work through a practical example that will demonstrate how most of those concepts fit
together. Here you will build (from scratch) your first classification and regression models!
Furthermore, you will learn how to evaluate their generalization performance.
Suppose you have a dataset that contains information about felines including their maximum
speed in km/hr and their specific type. For the sake of the example, suppose that these two
variables are the only ones that we can observe. As for the types, consider that there are
two possibilities: ‘tiger’ and ‘leopard’. Figure 1.9 shows the first 10 instances (rows) of the
dataset.
This table has 2 variables: speed and class. The first one is a numeric variable. The second
one is a categorical variable. In this case, it can take two possible values: ‘tiger’ or ‘leopard’.
This dataset was synthetically created for illustration purposes, but I promise that after this,
we will mostly use real datasets.
The code to reproduce this example is contained in the Introduction folder in the script file
simple_model.R. The script contains the code used to generate the dataset. The dataset is
stored in a data frame named dataset. Let’s start by doing a simple exploratory analysis
of the dataset. More detailed exploratory analysis methods will be presented in chapter 4.
First, we can print the data frame dimensions with the dim() function.
CHAPTER 1. INTRODUCTION 16
The output tells us that the data frame has 100 rows and 2 columns. Now we may be
interested to know from those 100 instances, how many correspond to tigers. We can use
the table() function to get that information.
Here we can see that 50 instances are of type ‘leopard’ and also 50 instances are of type
‘tiger’. In fact, this is how the dataset was intentionally generated. The next thing we
can do is compute some summary statistics for each column. R already provides a very
convenient function for that purpose. Yes, it is the summary() function.
Since speed is a numeric variable, summary() computes some statistics like the mean, min,
max, etc. The class variable is a factor. Thus, it returns row counts instead. In R, categorical
variables are usually encoded as factors. It is similar to a string, but R treats factors in
a special way. We can already appreciate that with the previous code snippet when the
summary function returned class counts.
There are many other ways in which you can explore a dataset, but for now, let’s assume
we already feel comfortable and that we have a good understanding of the data. Since this
dataset is very simple, we won’t need to do any further data cleaning or preprocessing.
CHAPTER 1. INTRODUCTION 17
Figure 1.10: Feline speeds with vertical dashed lines at the means.
Now, imagine that you are asked to build a model that is able to predict the type of feline
based on observed attributes. In this case, the only thing we can observe is the speed. Our
task is to build a function that maps speed measurements to classes. That is, we want to be
able to predict what type of feline it is based on how fast it runs. Based on the terminology
presented in section 1.3, speed would be a feature variable and class would be the class
variable.
Based on the types of machine learning presented in section 1.2, this one corresponds to
a supervised learning problem because, for each instance, we have its respective label
or class which we can use to train a model. And, specifically, since we want to predict a
category, this is a classification problem.
Before building our classification model, it would be worth plotting the data. Let’s plot the
speeds for both tigers and leopards.
Here, I omitted the code for building the plot, but it is included in the script. I have also
added vertical dashed lines at the mean speeds for the two classes. From this plot, it seems
that leopards are faster than tigers (with some exceptions). One thing we can note is that
the data points are grouped around the mean values of their corresponding classes. That is,
most tiger data points are closer to the mean speed for tigers and the same can be observed
for leopards. Of course, there are some exceptions in which an instance is closer to the mean
of the opposite class. This could be because some tigers may be as fast as leopards. Some
leopards may also be slower than the average, maybe because they are newborns or they are
old. Unfortunately, we do not have more information so the best we can do is use our single
feature speed. We can use these insights to come up with a simple model that discriminates
between the two classes based on this single feature variable.
CHAPTER 1. INTRODUCTION 18
One thing we can do for any new instance we want to classify is to compute its distance to
the ‘center’ of each class and predict the class that is the closest one. In this case, the center
is the mean value. We can formally define our model as the set of n centrality measures
where n is the number of classes (2 in our example).
M = {µ1 , . . . , µn } (1.2)
Those centrality measures (the class means in this particular case) are called the parameters
of the model. Training a model consists of finding those optimal parameters that will allow
us to achieve the best performance on new instances that were not part of the training data.
In most cases, we will need an algorithm to find those parameters. In our example, the
algorithm consists of simply computing the mean speed for each class. That is, for each
class, sum all the speeds belonging to that class and divide them by the number of data
points in that class.
Once those parameters are found, we can start making predictions on new data points. This
is called inference or prediction time. In this case, when a new data point arrives, we can
predict its class by computing its distance to each of the n centrality measures in M and
returning the class of the closest one.
The following function implements the training part of our model.
return(params)
The first argument is the training data and the second argument is the centrality function we
want to use (the mean, by default). This function iterates each class, computes the centrality
measure based on the speed, and stores the results in a named array called params which is
then returned at the end.
Most of the time, training a model involves passing the training data and any additional
hyperparameters specific to each model. In this case, the centrality measure is a hyper-
parameter and here we want to use the mean.
The difference between parameters and hyperparameters is that the former are
learned during training. The hyperparameters are settings specific to each model
that we can define before the actual training starts.
Now that we have a function that performs the training, we need another one that
performs the actual inference or prediction on new data points. Let’s call this one
simple.classifier.predict(). Its first argument is a data frame with the instances
we want to get predictions for. The second argument is the named vector of parameters
learned during training. This function will return an array with the predicted class for each
instance in newdata.
return(predictions)
}
This function iterates through each row and computes the distance to each centrality measure
and returns the name of the class that was the closest one. The distance computation is
done with the following line of code:
First, it computes the absolute difference between the speed and each centrality measure
stored in params and then, it returns the name of the one that was the minimum. Now that
we have defined the training and prediction procedures, we are ready to test our classifier!
In section 1.5, two evaluation methods were presented. Hold-out and k-fold cross-
validation. These methods allow you to estimate how your model will perform on new
data. Let’s first start with hold-out validation.
First, we need to split the data into two independent sets. We will use 70% of the data to
train our classifier and the remaining 30% to test it. The following code splits dataset into
a trainset and testset.
CHAPTER 1. INTRODUCTION 21
The sample() function was used to select integer numbers at random from 1 to n, where
n is the total number of data points in dataset. These randomly selected data points are
the ones that will go to the train set. Thus, with the size argument we tell the function to
return 70 numbers which correspond to 70% of the total since dataset has 100 instances.
The last argument replace is set to FALSE because we do not want repeated numbers.
This ensures that any instance only belongs to either the train or the test set. We
don’t want an instance to be copied into both sets.
Now it’s time to test our functions. We can train our model using the trainset by calling our
previously defined function simple.model.train().
After training the model, we print the learned parameters. In this case, the mean for tiger
is 48.88 and for leopard, it is 54.58. With these parameters, we can start making predictions
CHAPTER 1. INTRODUCTION 22
on our test set! We pass the test set and the newly-learned parameters to our function
simple.classifier.predict().
Our predict function returns predictions for each instance in the test set. We can use the
head() function to print the first predictions. The first two instances were classified as tigers,
the third one as leopard, and so on.
But how good are those predictions? Since we know what the true classes are (also known
as ground truth) in our test set, we can compute the performance. In this case, we will
compute the accuracy, which is the percentage of correct classifications. Note that we did not
use the class information when making predictions, we only used the speed. We pretended
that we didn’t have the true class. We will use the true class only to evaluate the model’s
performance.
We can compute the accuracy by counting how many predictions were equal to the true
classes and divide them by the total number of points in the test set. In this case, the test
accuracy was 83.0%. Congratulations! you have trained and evaluated your first
classifier.
It is also a good idea to compute the performance on the same train set that was used to
train the model.
nrow(trainset)
#> [1] 0.8571429
The train accuracy was 85.7%. As expected, this was higher than the test accuracy. Typi-
cally, what you report is the performance on the test set, but we can use the performance
on the train set to look for signs of over/under-fitting which will be covered in the following
sections.
Now, let’s see how k-fold cross-validation can be implemented to test our classifier. I will
choose a k = 5. This means that 5 independent sets are going to be generated and 5 iterations
will be run.
# Number of folds.
k <- 5
set.seed(123)
Again, we can use the sample() function. This time we want to select random integers
between 1 and k. The total number of integers will be equal to the total number of instances
n in the entire dataset. Note that this time we set replace = TRUE since k < n we need to
pick repeated numbers. Each number will represent the fold to which each instance belongs
to. As before, we need to make sure that each instance belongs only to one of the sets. Here,
we are guaranteeing that by assigning each instance a single fold number. We can use the
table() function to print how many instances ended up in each fold. Here, we see that the
folds will contain between 17 and 23 instances.
CHAPTER 1. INTRODUCTION 24
K-fold cross-validation consists of iterating k times. In each iteration, we select one of the
folds to function as the test set and the remaining folds are used as the train set. We can
then train a model with the train set and evaluate it with the test set. In the end, we report
the average accuracy across folds.
for(i in 1:k){
testset <- dataset[which(folds == i),]
trainset <- dataset[which(folds != i),]
The test mean accuracy across the 5 folds was ≈ 83% which is very similar to the accuracy
estimated by hold-out validation.
Note that in section 1.5 a validation set was also mentioned. This one is useful when
you want to fine-tune a model and/or try different preprocessing methods on your
data. In case you are using hold-out validation, you may want to split your data into
three sets: train/validation/test sets. So, you train your model using the train set and
estimate its performance using the validation set. Then you can fine-tune your model.
For example, here, instead of the mean as centrality measure, you can try to use the
median and measure the performance again with the validation set. When you are
pleased with your settings, you estimate the final performance of the model with the
test set only once.
In the case of k-fold cross-validation, you can set aside a test set at the beginning.
Then you use the remaining data to perform cross-validation and fine-tune your model.
Within each iteration, you test the performance with the validation data. Once you
are sure you are not going to do any parameter tuning, you can train a model with the
train and validation sets and test the generalization performance using the test set.
One of the benefits of machine learning is that it allows us to find patterns based on
data freeing us from having to program hard-coded rules. This means a more scalable
and flexible code. If for some reason, now, instead of 2 classes we needed to add another
class, for example, a jaguar, the only thing we need to do is update our database and
retrain our model. We don’t need to modify the internals of the algorithms. They will
update themselves based on the data.
We can try this by adding a third class to the dataset. The simple_model.R script
shows how to add a new class, ‘jaguar’, to the dataset. It then trains the model as
usual and performs predictions.
CHAPTER 1. INTRODUCTION 26
simple_model.R
return(predictions)
}
CHAPTER 1. INTRODUCTION 27
print(params)
#> tiger leopard
#> 48.88246 54.5836
Here, we reused our previous function simple.model.train() to learn the parameters and
then print them. Then we can use those parameters to infer the speed. If a test instance
belongs to the class ‘tiger’ then return 48.88. If it is of class ‘leopard’ then return 54.58.
Since these are numeric predictions, we cannot use accuracy as with classification to evaluate
the performance. One way to evaluate how well these predictions are is by computing
the mean absolute error (MAE). This measure tells you, on average, how much each
CHAPTER 1. INTRODUCTION 28
prediction deviates from its true value. It is computed by subtracting each prediction from
its real value and taking the absolute value: |predicted − realV alue|. This can be visualized
in Figure 1.11. The distances between the true and predicted values are the errors and the
MAE is the average of all those errors.
We can use the following code to compute the MAE:
The MAE on the test set was 2.56. That is, on average, our simple model had a deviation
of 2.56 km/hr. with respect to the true values, which is not bad. We can also compute the
MAE on the train set.
The MAE on the train set was 2.16, which is better than the test set MAE (small MAE
values are preferred). Now, you have built, trained, and evaluated a regression
model!
CHAPTER 1. INTRODUCTION 29
This was a simple example, but it illustrates the basic idea of regression and how it differs
from classification. It also shows how the performance of regression models is typically
evaluated with the MAE as opposed to the accuracy used in classification. In chapter 8,
more advanced methods such as neural networks will be introduced, which can be used to
solve regression problems.
In this section, we have gone through several of the data analysis pipeline phases. We did a
simple exploratory analysis of the data and then we built, trained, and validated the models
to perform both classification and regression. Finally, we estimated the overall performance
of the models and presented the results. Here, we coded our models from scratch, but in
practice, you typically use models that have already been implemented and tested. All in all,
I hope these examples have given you the feeling of how it is to work with machine learning.
From the felines classification example, we saw how we can separate two classes by computing
the mean for each class. For the two-class problem, this is equivalent to having a decision
line between the two means (Figure 1.12). Everything to the right of this decision line will
be closer to the mean that corresponds to ‘leopard’ and everything to the left to ‘tiger’. In
this case, the classification function is a vertical line, and during learning the position of the
line that reduces the classification error is searched for. We implicitly estimated the position
of that line by finding the mean values for each of the classes.
Now, imagine that we do not only have access to the speed but also to the felines’ age. This
extra information could help us reduce the prediction error since age plays an important role
in how fast a feline is. Figure 1.13 (left) shows how it will look like if we plot age in the
x-axis and speed in the y-axis. Here, we can see that for both, tigers and leopards, the speed
seems to increase as age increases. Then, at some point, as age increases the speed begins to
decrease.
Constructing a classifier with a single vertical line as we did before will not work in this 2-
dimensional case where we have 2 predictors. We will need a more complex decision boundary
(function) to separate the two classes. One approach would be to use a line as before but
this time we allow the line to have an angle. Everything below the line is classified as ‘tiger’
and everything else as ‘leopard’. Thus, the learning phase involves finding the line’s position
and its angle that achieves the smallest error.
Figure 1.13 (left) shows a possible decision line. Even though this function is more complex
than a vertical line, it will still produce a lot of misclassifications (it does not clearly separate
both classes). This is called underfitting, that is, the model is so simple that it is not able
to capture the underlying data patterns.
Let’s try a more complex function, for example, a curve. Figure 1.13 (middle) shows that a
curve does a better job at separating the two classes with fewer misclassifications but still,
3 leopards were misclassified as tigers and 1 tiger was misclassified as a leopard. Can we do
better than that? Yes, just keep increasing the complexity of the decision function.
Figure 1.13 (right) shows a more complex function that was able to separate the two classes
with 100% accuracy or equivalently, with a 0% error. However, there is a problem. This
function learned how to accurately separate the training data, but it is likely that it will
not do as well with a new test set. This function became so specialized in this data that
it failed to capture the overall pattern. This is called overfitting. In this case, the model
CHAPTER 1. INTRODUCTION 31
starts to ‘memorize’ the train set instead of finding general patterns applicable to new unseen
instances. If we were to choose a model, the best one would be the one in the middle. Even if
it was not perfect on the train data, it will do better than the other models when evaluating
it on new test data.
Overfitting is a common problem in machine learning. One way to know if a model is
overfitting is if the error in the train set is low, but it is high on a new set (can be a test or
validation set). Figure 1.14 illustrates this idea. Too-simple models will have a high error in
both, the train and validation sets. As the complexity of the model increases, the error on
both sets is reduced. Then, at some point, the complexity of a model is too high so that it
gets too specific on the train set and fails to perform well on a new independent set.
In this example, we saw how underfitting and overfitting can affect the generalization perfor-
mance of a model in a classification setting but the same can occur in regression problems.
There are several methods that aim to reduce overfitting but many of them are specific to
each type of model. For example, with decision trees (covered in chapter 2), one way to
reduce overfitting is to limit their depth or build ensembles of trees (chapter 3). Neural
networks are also highly prone to overfitting since they can be very complex with millions of
parameters and there are also several techniques to reduce the effect of overfitting (chapter
8).
So far, we have seen how we can train predictive models and evaluate how well they do
on new data (test/validation sets). The main goal is to have predictive models that have
a low error rate with new data. Understanding the source of the error can help us make
CHAPTER 1. INTRODUCTION 32
more informed decisions when building predictive models. The test error, also known as the
generalization error of a predictive model can be decomposed into three components: a bias,
a variance, and noise.
Noise. This component is related to the data itself and there is nothing we can do about
it. For example, two instances having the same values in their features but with a different
label.
Bias. The bias is how much the average prediction differs from the true value. Note the
average keyword. This means that we make the assumption that an infinite (or very large)
number of train sets can be generated, and for each a predictive model is trained. Then we
average the predictions of all those models and see how much that average differs from the
true value.
Variance. The variance relates to how much the predictions change for a given data point
when training a model using a different train set each time.
The following picture graphically illustrates different scenarios for high/low bias and variance.
The center of the target represents the true value and the small red dots the predictions on
a hypothetical test set.
Bias and variance are closely related to underfitting and overfitting. High variance is a sign
CHAPTER 1. INTRODUCTION 33
of overfitting. That is, a model is so complex that it will fit a particular train set very well.
Every time it is trained with a different train set, the train error will be low, but it will likely
generate very different predictions for the same test points and a much higher test error.
Figure 1.16 illustrates the relation between overfitting and high variance with a regression
problem. Given a feature x, two models are trained to predict y: i) a complex model (top
row), and ii) a simpler model (bottom row). Both models are fitted with two training sets (a
and b) sampled from the same distribution. The complex model fits the train data perfectly
but makes very different predictions (big ∆) for the same test point when using a different
train set. The simpler model does not fit the train data so well but has a smaller ∆ and
a lower error on the test point as well. Visually, the function (red curve) of the complex
model also varies a lot across train sets whereas the shapes of the simpler model functions
look very similar.
On the other hand, if a model is too simple, it will underfit causing highly biased results and
not being able to capture the input-output relationships. This results in a high train error
and in consequence a high test error as well.
CHAPTER 1. INTRODUCTION 34
A formal formulation of the error decomposition can be consulted in the book “The
elements of statistical learning: data mining, inference, and prediction” (Hastie et al.,
2009).
CHAPTER 1. INTRODUCTION 35
1.10 Summary
In this chapter, several introductory machine learning concepts and terminology were intro-
duced. These concepts are the basis for the methods that will be covered in the following
chapters.
In the previous chapter, the concept of classification was introduced along with a simple
example (feline type classification). This chapter will cover more in depth concepts on
classification methods and their application to behavior analysis tasks. Moreover, additional
performance metrics will be introduced. This chapter begins with an introduction to k-
nearest neighbors (k-NN) which is one of the simplest classification algorithms. Then, an
example of k-NN applied to indoor location using Wi-Fi signals is presented. This chapter
also covers Decision Trees and Naive Bayes classifiers and how they can be used for
activity recognition based on smartphone accelerometer data. After that, Dynamic Time
Warping (DTW) (a method for aligning time series) is introduced, and an example of how
it can be used for hand gesture recognition is presented.
k-nearest neighbors (k-NN) is one of the simplest classification algorithms. The predicted
class for a given query instance is the most common class of its k nearest neighbors. A query
instance is just the instance we want to make predictions on. In its most basic form, the
algorithm consists of two steps:
1. Compute the distance between the query instance and all training instances.
2. Return the most common class among the k nearest training instances.
This is a type of lazy-learning algorithm because all the computations take place at predic-
tion time. There are no parameters to learn at training time! The training phase consists
37
Discovering Diverse Content Through
Random Scribd Documents
Dumouriez replied, asking for an audience, and requested his
successor to be sought for. It was clear that the anti-revolutionist
party felt strong.
Indeed, they were reckoning on the following forces:
The Constitutional Guards, six thousand strong, disbanded, but
ready to fly to arms at the first call; seven or eight thousand Knights
of the Order of St. Louis, whose red ribbon was the rallying token;
three battalions of Switzers, sixteen hundred men, picked soldiers,
unshaken as the old Helvetic rocks.
Better than all, Lafayette had written: "Persist, sire; fortified with the
authority the National Assembly has delegated to you, you will find
all good citizens on your side!"
The plan was to gather all the forces at a given signal, seize the
cannon of each section of Paris, shut up the Jacobin's Club-house
and the Assembly, add all the Royalists in the National Guard, say, a
contingent of fifteen thousand men, and wait for Lafayette, who
might march up in three days.
The misfortune was that the queen would not hear of Lafayette.
Lafayette was merely the Revolution moderated, and might prolong
it and lead to a republic like that he had brought round in America;
while the Jacobins' outrageous rule would sicken the people and
could not endure.
Oh, had Charny been at hand! But it was not even known where he
was; and were it known, it would be too low an abasement for the
woman, if not the queen, to have recourse to him.
The night passed tumultuously at the palace, where they had the
means of defense and attack, but not a hand strong enough to
grasp and hurl them.
Dumouriez and his colleagues came to resign. They affirmed they
were willing to die for the king, but to do this for the clergy would
only precipitate the downfall of the monarchy.
"Sire," pleaded Dumouriez, "your conscience is misled; you are
beguiled into civil war. Without strength, you must succumb, and
history, while sorrowing for you, will blame you for causing the woes
of France."
"Heaven be my witness that I wished but her happiness!"
"I do not doubt that; but one must account to the King of kings not
only for purity of intentions, but the enlightened use of intentions.
You suppose you are saving religion, but you will destroy it; your
priests will be massacred; your broken crown will roll in your blood,
the queen's, your children's, perhaps—oh, my king, my king!"
Choking, he applied his lips to the royal hand. With perfect serenity,
and a majesty of which he might not be believed capable, Louis
replied.
"You are right, general. I expect death, and forgive my murderers
beforehand. You have well served me; I esteem you, and am
affected by your sympathy. Farewell, sir!"
With Dumouriez going, royalty had parted with its last stay. The king
threw off the mask, and stood with uncovered face before the
people.
Let us see what the people were doing on their side.
CHAPTER V.
THE UNINVITED VISITORS.
All day long a man in general's uniform was riding about the St.
Antoine suburb, on a large Flanders horse, shaking hands right and
left, kissing the girls and treating the men to drink. This was one of
Lafayette's half dozen heirs, the small-change of the commander of
the National Guard—Battalion Commander Santerre.
Beside him rode, on a fiery charger, like an aid next his general, a
stout man who might by his dress be taken to be a well-to-do
farmer. A scar tracked his brow, and he had as gloomy an eye and
scowling a face as the battalion commander had an open
countenance and frank smile.
"Get ready, my good friends; watch over the nation, against which
traitors are plotting. But we are on guard," Santerre kept saying.
"What are we to do, friend Santerre?" asked the working-men. "You
know that we are all your own. Where are the traitors? Lead us at
them!"
"Wait; the proper time has not come."
"When will it strike?"
Santerre did not know a word about it; so he replied at a hazard,
"Keep ready; we'll let you know."
But the man who rode by his knee, bending down over the horse's
neck, would make signs to some men, and whisper:
"June twenty."
Whereupon these men would call groups of twenty or so around
each, and repeat the date to them, so that it would be circulated.
Nobody knew what would be done on the twentieth of June, but all
felt sure that something would happen on that day.
By whom was this mob moved, stirred, and excited? By a man of
powerful build, leonine mane, and roaring voice, whom Santerre was
to find waiting in his brewery office—Danton.
None better than this terrible wizard of the Revolution could evoke
terror from the slums and hurl it into the old palace of Catherine di
Medicis. Danton was the gong of riots; the blow he received he
imparted vibratingly to all the multitude around him. Through Hebert
he was linked to the populace, as by the Duke of Orleans he was
affixed to the throne.
Whence came his power, doomed to be so fatal to royalty? To the
queen, the spiteful Austrian who had not liked Lafayette to be mayor
of Paris, but preferred Petion, the Republican, who had no sooner
brought back the fugitive king to the Tuileries than he set to watch
him closely.
Petion had made his two friends, Manuel and Danton, the Public
Prosecutor and the Vice, respectively.
On the twentieth of June, under the pretext of presenting a petition
to the king and raising a liberty pole, the palace was to be stormed.
The adepts alone knew that France was to be saved from the
Lafayettes and the Moderates, and a warning to be given to the
incorrigible monarch that there are some political tempests in which
a vessel may be swamped with all hands aboard; that is, a king be
overwhelmed with throne and family as in the oceanic abysses.
Billet knew more than Santerre when he accompanied him on his
tour, after presenting himself as from the committee.
Danton called on the brewer to arrange for the meeting of the
popular leaders that night at Charenton for the march on the
morrow, presumably to the House, but really to the Tuileries.
The watchword was, "Have done with the palace!" but the way
remained vague.
On the evening of the nineteenth, the queen saw a woman clad in
scarlet, with a belt full of pistols, gallop, bold and terrible, along the
main streets. It was Theroigne Mericourt, the beauty of Liege, who
had gone back to her native country to help its rebellion; but the
Austrians had caught her and kept her imprisoned for eighteen
months.
She returned mysteriously to be at the bloody feast of the coming
day. The courtesan of opulence, she was now the beloved of the
people; from her noble lovers had come the funds for her costly
weapons, which were not all for show. Hence the mob hailed her
with cheers.
From the Tuileries garret, where the queen had climbed on hearing
the uproar, she saw tables set out in the public squares and wine
broached; patriotic songs were sung and at every toast fists were
shaken at the palace.
Who were the guests? The Federals of Marseilles, led by Barbaroux,
who brought with them the song worth an army—"the Marseillaise
Hymn of Liberty."
Day breaks early in June. At five o'clock the battalions were
marshaled, for the insurrection was regularized by this time and had
a military aspect. The mob had chiefs, submitted to discipline, and
fell into assigned places under flags.
Santerre was on horseback, with his staff of men from the working
district. Billet did not leave him, for the occult power of the Invisibles
charged him to watch over him.
Of the three corps into which the forces were divided, Santerre
commanded the first, St. Huruge the second, and Theroigne the last.
About eleven, on an order brought by an unknown man, the
immense mass started out. It numbered some twenty thousand
when it left the Bastile Square.
It had a wild, odd, and horrible look.
Santerre's battalion was the most regular, having many in uniform,
and muskets and bayonets among the weapons. But the other two
were armed mobs, haggard, thin, and in rags from three years of
revolutions and four of famine.
Neither had uniforms nor muskets, but tattered coats and smocks;
quaint arms snatched up in the first impulse of self-defense and
anger: pikes, cooking-spits, jagged spears, hiltless swords, knives
lashed to long poles, broad-axes, stone-masons' hammers and
curriers' knives.
For standards, a gallows with a dangling doll, meant for the queen;
a bull's head, with an obscene card stuck on the horns; a calf's heart
on a spit, with the motto: "An Aristocrat's;" while flags showed the
legends: "Sanction the decrees, or death!"—"Recall the patriotic
ministers!"—"Tremble, tyrant; your hour has come!"
At every crossing and from each by-way the army was swollen.
The mass was silent, save now and then when a cheer burst from
the midst, or a snatch of the "It shall go on" was sung, or cries went
up of "The nation forever!"—"Long live the Breechless!"—"Down
with Old Veto and Madame Veto!"
They came out for sport—to frighten the king and queen, and did
not mean murdering. They demanded to march past the Assembly
through the Hall, and for three hours they defiled under the eyes of
their representatives.
It was three o'clock. The mob had obtained half their programme,
the placing of their petition before the Assembly. The next thing was
to call on the king for his sanction to the decree.
As the Assembly had received them, how could the king refuse?
Surely he was not a greater potentate than the Speaker of the
House, whose chair was like his and in the grander place?
In fact, the king assented to receiving their deputation of twenty.
As the common people had never entered the palace, they merely
expected their representatives would be received while they
marched by under the windows. They would show the king their
banners with the odd devices and the gory standards.
All the palace garden gates were closed; in the yards and gardens
were soldiers with four field-pieces. Seeing this apparently ample
protection, the royal family might be tranquil.
Still without any evil idea, the crowd asked for the gates to be
opened which allowed entrance on the Feuillants Terrace.
Three municipal officers went in and got leave from the king for
passage to be given over the terrace and out by the stable doors.
Everybody wanted to go in as soon as the gates were open, and the
throng spread over the lawn; it was forgotten to open the outlet by
the stables, and the crush began to be severe. They streamed
before the National Guards in a row along the palace wall to the
Carrousel gates, by which they might have resumed the homeward
route. They were locked and guarded.
Sweltering, crushed, and turned about, the mob began to be
irritated. Before its growls the gates were opened and the men
spread over the capacious square.
There they remembered what the main affair was—to petition the
king to revoke his veto. Instead of continuing the road, they waited
in the square for an hour, when they grew impatient.
They might have gone away, but that was not the aim of the
agitators, who went from group to group, saying:
"Stay; what do you want to sneak away for? The king is going to
give his sanction; if we were to go home without that, we should
have all our work to do over again."
The level-headed thought this sensible advice, but at the same time
that the sanction was a long time coming. They were getting hungry,
and that was the general cry.
Bread was not so dear as it had been, but there was no work going
on, and however cheap bread may be, it is not made for nothing.
Everybody had risen at five, workmen and their wives, with their
children, and come to the palace with the idea that they had but to
get the royal sanction to have hard times end. But the king did not
seem to be at all eager to give his sanction.
It was hot, and thirst began to be felt. Hunger, thirst, and heat drive
dogs mad; yet the poor people waited and kept patient. But those
next to the railings set to shaking them. A municipal officer made a
speech to them:
"Citizens, this is the king's residence, and to enter with arms is to
violate it. The king is quite ready to receive your petition, but only
from twenty deputies bearing it."
What! had not their deputation, sent in an hour ago, been attended
to yet?
Suddenly loud shouts were heard on the streets. It was Santerre,
Billet, and Huruge on their horses, and Theroigne riding on her
cannon.
"What are you fellows hanging round this gate for?" queried Huruge.
"Why do you not go right in?"
"Just so; why haven't we?" said the thousands.
"Can't you see it is fast?" cried several voices.
Theroigne jumped off her cannon, saying:
"The barker is full to the muzzle; let's blow the old gate open."
"Wait! wait!" shouted two municipal officers; "no roughness. It shall
be opened to you."
Indeed, by pressing on the spring-catch they released the two gates,
which drew aside, and the mass rushed through.
Along with them came the cannon, which crossed the yard with
them, mounted the steps, and reached the head of the stairs in their
company. Here stood the city officials in their scarfs of office.
"What do you intend doing with a piece of artillery?" they
challenged. "Great guns in the royal apartments! Do you believe
anything is to be gained by such violence?"
"Quite right," said the ringleaders, astonished themselves to see the
gun there; and they turned it round to get it down-stairs. The hub
caught on the jamb, and the muzzle gaped on the crowd.
"Why, hang them all, they have got cannon all over the palace!"
commented the new-comers, not knowing their own artillery.
Police-Magistrate Mouchet, a deformed dwarf, ordered the men to
chop the wheel clear, and they managed to hack the door-jamb
away so as to free the piece, which was taken down to the yard.
This led to the report that the mob were smashing all the doors in.
Some two hundred noblemen ran to the palace, not with the hope of
defending it, but to die with the king, whose life they deemed
menaced. Prominent among these was a man in black, who had
previously offered his breast to the assassin's bullet, and who always
leaped like a last Life-Guard between danger and the king, from
whom he had tried to conjure it. This was Gilbert.
After being excited by the frightful tumult, the king and queen
became used to it.
It was half past three, and it was hoped that the day would close
with no more harm done.
Suddenly, the sound of the ax blows was heard above the noise of
clamor, like the howling of a coming tempest. A man darted into the
king's sleeping-room and called out:
"Sire, let me stand by you, and I will answer for all."
It was Dr. Gilbert, seen at almost periodical intervals, and in all the
"striking situations" of the tragedy in play.
"Oh, doctor, is this you? What is it?" King and queen spoke together.
"The palace is surrounded, and the people are making this uproar in
wanting to see you."
"We shall not leave you, sire," said the queen and Princess Elizabeth.
"Will the king kindly allow me for an hour such power as a captain
has over his ship?" asked Gilbert.
"I grant it," replied the monarch. "Madame, hearken to Doctor
Gilbert's advice, and obey his orders, if needs must." He turned to
the doctor: "Will you answer to me for the queen and the dauphin?"
"I do, or I shall die with them; it is all a pilot can say in the
tempest!"
The queen wished to make a last effort, but Gilbert barred the way
with his arms.
"Madame," he said, "it is you and not the king who run the real
danger. Rightly or wrongly, they accuse you of the king's resistance,
so that your presence will expose him without defending him. Be the
lightning-conductor—divert the bolt, if you can!"
"Then let it fall on me, but save my children!"
"I have answered for you and them to the king. Follow me."
He said the same to Princess Lamballe, who had returned lately from
London, and the other ladies, and guided them to the Council Hall,
where he placed them in a window recess, with the heavy table
before them.
The queen stood behind her children—Innocence protecting
Unpopularity, although she wished it to be the other way.
"All is well thus," said Gilbert, in the tone of a general commanding a
decisive operation; "do not stir."
There came a pounding at the door, which he threw open with both
folds, and as he knew there were many women in the crowd, he
cried:
"Walk in, citizenesses; the queen and her children await you."
The crowd burst in as through a broken dam.
"Where is the Austrian? where is the Lady Veto?" demanded five
hundred voices.
It was the critical moment.
"Be calm," said Gilbert to the queen, knowing that all was in
Heaven's hand, and man was as nothing. "I need not recommend
you to be kind."
Preceding the others was a woman with her hair down, who
brandished a saber; she was flushed with rage—perhaps from
hunger.
"Where is the Austrian cat? She shall die by no hand but mine!" she
screamed.
"This is she," said Gilbert, taking her by the hand and leading her up
to the queen.
"Have I ever done you a personal wrong?" demanded the latter, in
her sweetest voice.
"I can not say you have," faltered the woman of the people, amazed
at the majesty and gentleness of Marie Antoinette.
"Then why should you wish to kill me?"
"Folks told me that you were the ruin of the nation," faltered the
abashed young woman, lowering the point of her saber to the floor.
"Then you were told wrong. I married your King of France, and am
mother of the prince whom you see here. I am a French woman,
one who will nevermore see the land where she was born; in France
alone I must dwell, happy or unhappy. Alas! I was happy when you
loved me." And she sighed.
The girl dropped the sword, and wept.
"Beg your pardon, madame, but I did not know what you were like.
I see you are a good sort, after all."
"Keep on like that," prompted Gilbert, "and not only will you be
saved, but all these people will be at your feet in an hour."
Intrusting her to some National Guardsmen and the War Minister,
who came in with the mob, he ran to the king.
Louis had gone through a similar experience. On hastening toward
the crowd, as he opened the Bull's-eye Room, the door panels were
dashed in, and pikes, bayonets, and axes showed their points and
edges.
"Open the doors!" cried the king.
Servants heaped up chairs before him, and four grenadiers stood in
front, but he made them put up their swords, as the flash of steel
might seem a provocation.
A ragged fellow, with a knife-blade set in a pole, darted at the king,
yelling:
"Take that for your veto!"
One grenadier, who had not yet sheathed his sword, struck down the
stick with the blade. But it was the king who, entirely recovering self-
command, put the soldier aside with his hand, and said:
"Let me stand forward, sir. What have I to fear amid my people?"
Taking a forward step, Louis XVI., with a majesty not expected in
him, and a courage strange heretofore in him, offered his breast to
the weapons of all sorts directed against him.
"Hold your noise!" thundered a stentorian voice in the midst of the
awful din. "I want a word in here."
A cannon might have vainly sought to be heard in this clamor, but at
this voice all the vociferation ceased. This was the butcher Legendre.
He went up almost to touching the king, while they formed a ring
round the two.
Just then, on the outer edge of the circle, a man made his
appearance, and behind the dread double of Danton, the king
recognized Gilbert, pale and serene of face. The questioning glance
implying: "What have you done with the queen?" was answered by
the doctor's smile to the effect that she was in safety. He thanked
him with a nod.
"Sirrah," began Legendre.
This expression, which seemed to indicate that the sovereign was
already deposed, made the latter turn as if a snake had stung him.
"Yes, sir, I am talking to you, Veto," went on Legendre. "Just listen
to us, for it is our turn to have you hear us. You are a double-dealer,
who have always cheated us, and would try it again, so look out for
yourself. The measure is full, and the people are tired of being your
plaything and victim."
"Well, I am listening to you, sir," rejoined the king.
"And a good thing, too. Do you know what we have come here for?
To ask the sanction of the decrees and the recall of the ministers.
Here is our petition—see!"
Taking a paper from his pocket, he unfolded it, and read the same
menacing lines which had been heard in the House. With his eyes
fixed on the speaker, the king listened, and said, when it was ended,
without the least apparent emotion:
"Sir, I shall do what the laws and the Constitution order me to do!"
"Gammon!" broke in a voice; "the Constitution is your high horse,
which lets you block the road of the whole country, to keep France
in-doors, for fear of being trampled on, and wait till the Austrians
come up to cut her throat."
The king turned toward this fresh voice, comprehending that it was
a worse danger. Gilbert also made a movement and laid his hand on
the speaker's shoulder.
"I have seen you somewhere before, friend," remarked the king.
"Who are you?"
He looked with more curiosity than fear, though this man wore a
front of terrible resolution.
"Ay, you have seen me before, sire. Three times: once, when you
were brought back from Versailles; next at Varennes; and the last
time, here. Sire, bear my name in mind, for it is of ill omen. It is
Billet."
At this the shouting was renewed, and a man with a lance tried to
stab the king; but Billet seized the weapon, tore it from the wielder's
grip, and snapped it across his knee.
"No foul play," he said; "only one kind of steel has the right to touch
this man: the ax of the executioner! I hear that a King of England
had his head cut off by the people whom he betrayed—you ought to
know his name, Louis. Don't you forget it."
"'Sh, Billet!" muttered Gilbert.
"Oh, you may say what you like," returned Billet, shaking his head;
"this man is going to be tried and doomed as a traitor."
"Yes, a traitor!" yelled a hundred voices; "traitor, traitor!"
Gilbert threw himself in between.
"Fear nothing, sire, and try by some material token to give
satisfaction to these mad men."
Taking the physician's hand, the king laid it on his heart.
"You see that I fear nothing," he said; "I received the sacraments
this morning. Let them do what they like with me. As for the
material sign which you suggest I should display—are you satisfied?"
Taking the red cap from a by-stander, he set it on his own head. The
multitude burst into applause.
"Hurrah for the king!" shouted all the voices.
A fellow broke through the crowd and held up a bottle.
"If fat old Veto loves the people as much as he says, prove it by
drinking our health."
"Do not drink," whispered a voice. "It may be poisoned."
"Drink, sire, I answer for the honesty," said Gilbert.
The king took the bottle, and saying, "To the health of the people,"
he drank. Fresh cheers for the king resounded.
"Sire, you have nothing to fear," said Gilbert; "allow me to return to
the queen."
"Go," said the other, gripping his hand.
More tranquil, the doctor hastened to the Council Hall, where he
breathed still easier after one glance. The queen stood in the same
spot; the little prince, like his father, was wearing the red cap.
In the next room was a great hubbub; it was the reception of
Santerre, who rolled into the hall.
"Where is this Austrian wench?" demanded he.
Gilbert cut slanting across the hall to intercept him.
"Halloo, Doctor Gilbert!" said he, quite joyfully.
"Who has not forgotten that you were one of those who opened the
Bastile doors to me," replied the doctor. "Let me present you to the
queen."
"Present me to the queen?" growled the brewer.
"You will not refuse, will you?"
"Faith, I'll not. I was going to introduce myself; but as you are in the
way—"
"Monsieur Santerre needs no introduction," interposed the queen. "I
know how at the famine time he fed at his sole expense half the St.
Antoine suburb."
Santerre stopped, astonished; then, his glance happening to fall,
embarrassed, on the dauphin, whose perspiration was running down
his cheeks, he roared:
"Here, take that sweater off the boy—don't you see he is
smothering?"
The queen thanked him with a look. He leaned on the table, and
bending toward her, he said in an under-tone:
"You have a lot of clumsy friends, madame. I could tell you of some
who would serve you better."
An hour afterward all the mob had flowed away, and the king,
accompanied by his sister, entered the room where the queen and
his children awaited him.
She ran to him and threw herself at his feet, while the children
seized his hands, and all acted as though they had been saved from
a shipwreck. It was only then that the king noticed that he was
wearing the red cap.
"Faugh!" he said; "I had forgotten!"
Snatching it off with both hands, he flung it far from him with
disgust.
The evacuation of the palace was as dull and dumb as the taking
had been gleeful and noisy. Astonished at the little result, the mob
said:
"We have not made anything; we shall have to come again."
In fact, it was too much for a threat, and not enough for an attempt
on the king's life.
Louis had been judged on his reputation, and recalling his flight to
Varennes, disguised as a serving-man, they had thought that he
would hide under a table at the first noise, and might be done to
death in the scuffle, like Polonius behind the arras.
Things had happened otherwise; never had the monarch been
calmer, never so grand. In the height of the threats and the insults
he had not ceased to say: "Behold your king!"
The Royalists were delighted, for, to tell the truth, they had carried
the day.
CHAPTER VI.
"THE COUNTRY IS IN DANGER!"
ebookbell.com