Data Mining with Python Theory Application and Case Studies 1st Edition Di Wu download
Data Mining with Python Theory Application and Case Studies 1st Edition Di Wu download
https://ebookgate.com/product/data-mining-with-python-theory-
application-and-case-studies-1st-edition-di-wu/
https://ebookgate.com/product/data-science-fundamentals-with-r-python-
and-open-data-1st-edition-marco-cremonini/
ebookgate.com
https://ebookgate.com/product/data-mining-using-sas-enterprise-miner-
a-case-study-approach-2nd-edition/
ebookgate.com
https://ebookgate.com/product/pattern-discovery-using-sequence-data-
mining-applications-and-studies-1st-edition-pradeep-kumar/
ebookgate.com
Applying Theory to Educational Research An Introductory
Approach with Case Studies 1st Edition Jeff Adams
https://ebookgate.com/product/applying-theory-to-educational-research-
an-introductory-approach-with-case-studies-1st-edition-jeff-adams/
ebookgate.com
https://ebookgate.com/product/traditional-neutrality-revisited-law-
theory-and-case-studies-1st-edition-elizabeth-chadwick/
ebookgate.com
https://ebookgate.com/product/data-mining-and-management-1st-edition-
lawrence-i-spendler/
ebookgate.com
https://ebookgate.com/product/handbook-of-statistics-24-data-mining-
and-data-visualization-c-r-rao/
ebookgate.com
Data Mining with Python
Data is everywhere and it’s growing at an unprecedented rate. But making sense of all that data
is a challenge. Data Mining is the process of discovering patterns and knowledge from large data
sets, and Data Mining with Python focuses on the hands-on approach to learning Data Mining.
It showcases how to use Python Packages to fulfil the Data Mining pipeline, which is to collect,
integrate, manipulate, clean, process, organize, and analyze data for knowledge.
The contents are organized based on the Data Mining pipeline, so readers can naturally prog-
ress step by step through the process. Topics, methods, and tools are explained in three aspects:
“What it is” as a theoretical background, “why we need it” as an application orientation, and
“how we do it” as a case study.
This book is designed to give students, data scientists, and business analysts an understanding of
Data Mining concepts in an applicable way. Through interactive tutorials that can be run, modi-
fied, and used for a more comprehensive learning experience, this book will help its readers gain
practical skills to implement Data Mining techniques in their work.
Python has been ranked as the most popular programming language, and it is widely used in
education and industry. This book series will offer a wide range of books on Python for students
and professionals. Titles in the series will help users learn the language at an introductory and
advanced level, and explore its many applications in data science, AI, and machine learning.
Series titles can also be supplemented with Jupyter notebooks.
Di Wu
First edition published 2024
by CRC Press
2385 Executive Center Drive, Suite 320, Boca Raton, FL 33431
© 2024 Di Wu
Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot as-
sume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have
attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders
if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please
write and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or
utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including pho-
tocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission
from the publishers.
For permission to photocopy or use material electronically from this work, access www.copyright.com or contact the
Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. For works that are not
available on CCC please contact [email protected]
Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used only for iden-
tification and explanation without intent to infringe.
DOI: 10.1201/9781003462781
Publisher’s note: This book has been prepared from camera-ready copy provided by the authors.
List of Figures xi
Foreword xix
Preface xxi
vii
viii ■ Contents
Index 389
List of Figures
xi
xii ■ LIST OF FIGURES
xix
Preface
xxi
Author Bios
xxiii
I
Data Wrangling
1
CHAPTER 1
Data Collection
DOI: 10.1201/9781003462781-1 3
4 ■ Data Mining with Python
Storing data in different file formats allows for versatility and compatibility with
various applications and tools.
• CSV (Comma-Separated Values): CSV files store tabular data in plain text
format, where each line represents a row, and values are separated by commas (or
other delimiters). CSV files are simple, human-readable, and widely supported.
They can be easily opened and edited using spreadsheet software or text editors.
However, CSV files may not support complex data structures, and there is no
standardized format for metadata or data types. Pandas provides the read_csv()
function, allowing you to read CSV files into a DataFrame object effortlessly.
It automatically detects the delimiter, handles missing values, and provides
convenient methods for data manipulation and analysis.
• TXT (Plain Text): TXT files contain unformatted text with no specific structure
or metadata. TXT files are lightweight, widely supported, and can be easily
opened with any text editor. However, TXT files lack a standardized structure or
format, making it challenging to handle data that requires specific organization
or metadata. Pandas offers the read_csv() function with customizable delimiters
to read text files with structured data. By specifying the appropriate delimiter,
you can read text files into a DataFrame for further analysis.
• XLSX (Microsoft Excel): XLSX is a file format used by Microsoft Excel to
store spreadsheet data with multiple sheets, formatting, formulas, and metadata.
XLSX files support complex spreadsheets with multiple tabs, cell formatting,
and formulas. They are widely used in business and data analysis scenarios.
However, XLSX files can be large, and manipulating them directly can be
memory-intensive. Additionally, XLSX files require software like Microsoft Excel
to view and edit. Pandas provides the read_excel() function, enabling the
reading of XLSX files into DataFrames. It allows you to specify the sheet name,
range of cells, and other parameters to extract data easily.
• JSON (JavaScript Object Notation): JSON is a lightweight, human-readable
data interchange format that represents structured data as key-value pairs, lists,
and nested objects. JSON is easy to read and write, supports complex nested
structures, and is widely used for data interchange between systems. However,
JSON files can be larger than their equivalent CSV representations, and handling
Data Collection ■ 5
1.1.1.1 CSV
We have done this when we learned pandas. You can get the path of your csv file,
and feed the path to the function read_csv.
df.head()
2 2 2020 SE FT
3 3 2020 MI FT
4 4 2020 SE FT
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 607 entries, 0 to 606
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Unnamed: 0 607 non-null int64
1 work_year 607 non-null int64
2 experience_level 607 non-null object
3 employment_type 607 non-null object
4 job_title 607 non-null object
5 salary 607 non-null int64
6 salary_currency 607 non-null object
7 salary_in_usd 607 non-null int64
8 employee_residence 607 non-null object
9 remote_ratio 607 non-null int64
10 company_location 607 non-null object
11 company_size 607 non-null object
dtypes: int64(5), object(7)
memory usage: 57.0+ KB
Customize setting You can manipulate arguments for your specific csv file
df = pd.read_csv('/content/ds_salaries.csv', header = None)
df.head()
0 1 2 3 \
0 NaN work_year experience_level employment_type
1 0.0 2020 MI FT
2 1.0 2020 SE FT
3 2.0 2020 SE FT
4 3.0 2020 MI FT
4 5 6 7 \
Data Collection ■ 7
8 9 10 11
0 employee_residence remote_ratio company_location company_size
1 DE 0 DE L
2 JP 0 JP S
3 GB 50 GB M
4 HN 0 HN S
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 608 entries, 0 to 607
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 0 607 non-null float64
1 1 608 non-null object
2 2 608 non-null object
3 3 608 non-null object
4 4 608 non-null object
5 5 608 non-null object
6 6 608 non-null object
7 7 608 non-null object
8 8 608 non-null object
9 9 608 non-null object
10 10 608 non-null object
11 11 608 non-null object
dtypes: float64(1), object(11)
memory usage: 57.1+ KB
0 1 2 3 4 5 6 7 8 9 \
0 0 2020 MI FT Data Scientist 70000 EUR 79833 DE 0
1 1 2020 SE FT Machine Learning Scientist 260000 USD 260000 JP 0
2 2 2020 SE FT Big Data Engineer 85000 GBP 109024 GB 50
3 3 2020 MI FT Product Data Analyst 20000 USD 20000 HN 0
4 4 2020 SE FT Machine Learning Engineer 150000 USD 150000 US 50
10 11
0 DE L
1 JP S
2 GB M
3 HN S
4 US L
8 ■ Data Mining with Python
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 607 entries, 0 to 606
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 0 607 non-null int64
1 1 607 non-null int64
2 2 607 non-null object
3 3 607 non-null object
4 4 607 non-null object
5 5 607 non-null int64
6 6 607 non-null object
7 7 607 non-null int64
8 8 607 non-null object
9 9 607 non-null int64
10 10 607 non-null object
11 11 607 non-null object
dtypes: int64(5), object(7)
memory usage: 57.0+ KB
0 1 2 3 4 5 6 7 8 9 \
0 0 2020 MI FT Data Scientist 70000 EUR 79833 DE 0
1 1 2020 SE FT Machine Learning Scientist 260000 USD 260000 JP 0
2 2 2020 SE FT Big Data Engineer 85000 GBP 109024 GB 50
3 3 2020 MI FT Product Data Analyst 20000 USD 20000 HN 0
4 4 2020 SE FT Machine Learning Engineer 150000 USD 150000 US 50
10 11
0 DE L
1 JP S
2 GB M
3 HN S
4 US L
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 307 entries, 0 to 306
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 0 307 non-null int64
1 1 307 non-null int64
2 2 307 non-null object
3 3 307 non-null object
4 4 307 non-null object
5 5 307 non-null int64
6 6 307 non-null object
Data Collection ■ 9
1.1.1.2 TXT
If the txt follows csv format, then it can be read as a csv file
df = pd.read_csv('/content/ds_salaries.txt')
df
1.1.1.3 Excel
df = pd.read_excel('/content/ds_salaries.xlsx')
df.head()
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 607 entries, 0 to 606
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Unnamed: 0 607 non-null int64
1 work_year 607 non-null int64
2 experience_level 607 non-null object
3 employment_type 607 non-null object
4 job_title 607 non-null object
5 salary 607 non-null int64
6 salary_currency 607 non-null object
7 salary_in_usd 607 non-null int64
8 employee_residence 607 non-null object
9 remote_ratio 607 non-null int64
10 company_location 607 non-null object
11 company_size 607 non-null object
dtypes: int64(5), object(7)
memory usage: 57.0+ KB
Data Collection ■ 11
1.1.1.4 json
df = pd.read_json('/content/ds_salaries.json')
df.head()
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 607 entries, 0 to 606
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 FIELD1 607 non-null int64
1 work_year 607 non-null int64
2 experience_level 607 non-null object
3 employment_type 607 non-null object
4 job_title 607 non-null object
5 salary 607 non-null int64
6 salary_currency 607 non-null object
7 salary_in_usd 607 non-null int64
8 employee_residence 607 non-null object
9 remote_ratio 607 non-null int64
10 company_location 607 non-null object
11 company_size 607 non-null object
dtypes: int64(5), object(7)
memory usage: 57.0+ KB
12 ■ Data Mining with Python
1.1.1.5 XML
df = pd.read_xml('/content/ds_salaries.xml')
df.head()
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 607 entries, 0 to 606
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 FIELD1 607 non-null int64
1 work_year 607 non-null int64
2 experience_level 607 non-null object
3 employment_type 607 non-null object
4 job_title 607 non-null object
5 salary 607 non-null int64
6 salary_currency 607 non-null object
7 salary_in_usd 607 non-null int64
8 employee_residence 607 non-null object
9 remote_ratio 607 non-null int64
10 company_location 607 non-null object
11 company_size 607 non-null object
dtypes: int64(5), object(7)
memory usage: 57.0+ KB
Data Collection ■ 13
1.1.1.6 HTM
df = pd.read_html('/content/ds_salaries.htm')[0]
df.head()
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 607 entries, 0 to 606
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 FIELD1 607 non-null int64
1 work_year 607 non-null int64
2 experience_level 607 non-null object
3 employment_type 607 non-null object
4 job_title 607 non-null object
5 salary 607 non-null int64
6 salary_currency 607 non-null object
7 salary_in_usd 607 non-null int64
8 employee_residence 607 non-null object
9 remote_ratio 607 non-null int64
10 company_location 607 non-null object
11 company_size 607 non-null object
dtypes: int64(5), object(7)
memory usage: 57.0+ KB
1.1.2 Documentation
It is always good to have a reference of the read files functions in pandas. You can
find it via https://pandas.pydata.org/docs/reference/io.html
14 ■ Data Mining with Python
1.2.1.1 Wiki
Some websites maintains structured data, which is easy to read
table = pd.read_html('https://en.wikipedia.org/wiki/
List_of_countries_by_GDP_(nominal)#Table')
for i in table:
print(type(i))
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
for i in table:
print(i.columns)
Int64Index([0], dtype='int64')
Int64Index([0, 1, 2], dtype='int64')
MultiIndex([( 'Country/Territory', 'Country/Territory'),
( 'UN Region', 'UN Region'),
16 ■ Data Mining with Python
( 'IMF[1][13]', 'Estimate'),
( 'IMF[1][13]', 'Year'),
( 'World Bank[14]', 'Estimate'),
( 'World Bank[14]', 'Year'),
('United Nations[15]', 'Estimate'),
('United Nations[15]', 'Year')],
)
...
Int64Index([0, 1], dtype='int64')
df = table[2]
df.head()
United Nations[15]
Estimate Year
0 85328323 2020
1 20893746 2020
2 14722801 [n 1]2020
3 5057759 2020
4 3846414 2020
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 217 entries, 0 to 216
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 (Country/Territory, Country/Territory) 217 non-null object
1 (UN Region, UN Region) 217 non-null object
2 (IMF[1][13], Estimate) 217 non-null object
3 (IMF[1][13], Year) 217 non-null object
4 (World Bank[14], Estimate) 217 non-null object
5 (World Bank[14], Year) 217 non-null object
6 (United Nations[15], Estimate) 217 non-null object
7 (United Nations[15], Year) 217 non-null object
dtypes: object(8)
memory usage: 13.7+ KB
Download by requests We’ll need to first import the requests library, and then
download the page using the requests.get method:
import requests
page = requests.get("https://dataquestio.github.io/
web-scraping-pages/simple.html")
page
<Response [200]>
After running our request, we get a Response object. This object has a status_code
property, which indicates if the page was downloaded successfully:
page.status_code
200
A status_code of 200 means that the page downloaded successfully. We won’t fully
dive into status codes here, but a status code starting with a 2 generally indicates
success, and a code starting with a 4 or a 5 indicates an error.
We can print out the HTML content of the page using the content property:
page.content
Parsing by BeautifulSoup As you can see above, we now have downloaded an HTML
document.
We can use the BeautifulSoup library to parse this document, and extract the text
from the p tag.
18 ■ Data Mining with Python
We can now print out the HTML content of the page, formatted nicely, using the
prettify method on the BeautifulSoup object.
print(soup.prettify())
<!DOCTYPE html>
<html>
<head>
<title>
A simple example page
</title>
</head>
<body>
<p>
Here is some simple content for this page.
</p>
</body>
</html>
This step isn’t strictly necessary, and we won’t always bother with it, but it can be
helpful to look at prettified HTML to make the structure of the and where tags are
nested easier to see.
Finding Tags Finding all instances of a tag at once What we did above was useful for
figuring out how to navigate a page, but it took a lot of commands to do something
fairly simple. If we want to extract a single tag, we can instead use the find_all
method, which will find all the instances of a tag on a page.
if we are looking for the title, we can look for <title> tag
soup.find_all('title')
for t in soup.find_all('title'):
print(t.get_text())
If you instead only want to find the first instance of a tag, you can use the find method,
which will return a single BeautifulSoup object:
soup.find('p').get_text()
{"type":"string"}
<html>
<head>
<title>A simple example page</title>
</head>
<body>
<div>
<p class="inner-text first-item" id="first">
First paragraph.
</p>
<p class="inner-text">
Second paragraph.
</p>
</div>
<p class="outer-text first-item" id="second">
<b>
First outer paragraph.
</b>
</p>
<p class="outer-text">
<b>
Second outer paragraph.
</b>
</p>
</body>
</html>
Now, we can use the find_all method to search for items by class or by id. In the
below example, we’ll search for any p tag that has the class outer-text:
soup.find_all('p', class_='outer-text')
In the below example, we’ll look for any tag that has the class outer-text:
soup.find_all(class_="outer-text")
A
few days after we had fairly got out to sea, the thrilling cry of “A
man overboard!” ran through the ship with electrical effect; it
was followed by another cry of, “Heave out a rope!” then by still
another, of “Cut away the life buoy!” Then came the order, “Lower a
boat!” Notwithstanding the rapidity of these commands, and the
confusion occasioned by the anticipated loss of a man, they were
rapidly obeyed. The ship was then hove to. But that time, however,
the cause of all this excitement was at a considerable distance from
the ship. It was a poor Swede, named Logholm, who, while engaged
in lashing the larboard anchor stock, lost his hold and fell into the
sea. He could not swim; but, somehow, he managed to keep afloat
until the boat reached him, when he began to sink. The man at the
bow ran his boat hook down, and caught the drowning man by his
clothes: his clothes tearing, the man lost his hold, and the Swede
once more sunk. Again the active bowsman ran the hook down,
leaning far over the side; fortunately, he got hold of his shirt collar:
dripping, and apparently lifeless, they drew him into the boat. He was
soon under the surgeon’s care, whose skill restored him to animation
and to life. It was a narrow escape!
Rising one morning, I heard the men talking about having been
called to quarters during the night. They said a strange vessel having
appeared, the drums beat to quarters, the guns were got ready,
those great lanterns, which are placed on the main deck, called battle
lanterns, were got out, and the officers began to muster the men at
each division; when they discovered the supposed vessel of war to be
nothing more than a large merchant ship. Upon this the hands were
sent below. All this was news to me; I had slept through all the noise,
confusion and bustle of the night, utterly ignorant of the whole
matter. It was fortunate for me that the real character of the strange
ship was discovered before my name was called, otherwise the
morning would have found me at the gratings under punishment.
Never was boy happier than myself, when made acquainted with my
hair-breadth escape from the lash.
We had now reached the island of Madeira, occupied by the
Portuguese, and producing fine oranges, grapes and wine. It is some
sixty miles in length, about forty in breadth; the climate is hot, but
salubrious; its harbor, or rather roadstead, is by no means
commodious or safe—so that our stay was short. Here, the
Portuguese lad who had supplied my place as servant to the surgeon,
was sent on shore, for attempting a crime unfit to be mentioned in
these pages, but quite common among the Spaniards and
Portuguese. My old master made an effort to obtain me again, but
did not succeed.
Sailing from Madeira, we next made St. Michael’s. At this pace we
had an increase to our crew, in the person of a fine, plump boy—born
to the wife of one of our men. The captain christened the new comer,
Michael, naming him after the island. This birth was followed by
another. Whether the captain did not like the idea of such interesting
episodes in sea life, or whether any other motive inspired him, I
cannot tell; but when, shortly after, we returned to Lisbon, he
ordered all the women home to England, by a ship just returning
thither. Before this, however, one of our little Tritons had died, and
found a grave under the billows, leaving its disconsolate mother in a
state little short of distraction. A man of war is no place for a woman.
Short cruises are very popular with man-of-war’s men. On many
accounts they love being in harbor; on others they prefer being at
sea. In harbor they have to work all day, but in return for this they
have the whole night for sleep. At sea, the whole time is divided into
five watches of four hours each, and two shorter ones, called dog
watches, of two hours each, or from four to six and from six to eight,
P. M. The design of these dog watches is to alternate the time, so
that each watch may have a fair proportion of every night below.
While at our station this time, our old friend, Bob Hammond, met
with some little difficulty, which we will here make matter of record.
He was below, and one of his messmates did something that vexed
him exceedingly. Now Bob was not a man to bear vexations tamely,
where he had the power to resist them; so, lifting his huge fist, he
struck at the offender; missing his real opponent, the blow fell upon
another who stood near him. Bob was too much of a bully to offer
any apology; he merely laughed, and remarked that he had “killed
two birds with one stone.”
Whether the bird, who, in Bob’s figurative language, was killed, did
not like being called a bird, or whether he conceived a strong dislike
to being a mark for Bob to shoot at, is not for me to say; but he
certainly disliked the one or the other, for the next morning he
reported the matter to the officers, which complaint was considered a
most unsailor-like act by the whole crew.
Fighting was a punishable offence, so Bob was called up the next
morning. The captain mentioned what was reported concerning him.
He acknowledged it was all true, and without any signs of contrition
said, “I only killed two birds with one stone.” The angry captain
ordered two dozen lashes to be laid on; it was done without extorting
a sigh or a groan. He was then loosed from the grating, and
questioned; but he merely replied, in a gruff tone, that “the man who
reported him was a blackguard!” For this, he was seized up again and
another dozen lashes inflicted; he bore them with the same dogged
and imperturbable air. Finding it impossible to extort any
acknowledgment from the stubborn tar, the captain ordered him
below.
About the same time one of our crew, named Jack Sadler, a fine,
noble-hearted seaman, growing weary of the service, determined to
desert. Dropping into the water, he began swimming towards the
shore. It was not very dark, and he was discovered; the sentry was
ordered to fire at him, which he did, but missed his prey. A boat was
next lowered, which soon overtook and dragged him on board. The
officer commanding the boat said, “Well, Mr. Sadler, you thought you
had got away, did you?” “You are not so sure that you have me now,”
replied Sadler, as he sprung over the side of the boat. Nor would they
have captured him, had not another ship’s boat arrived to their
assistance.
The next day, he was seized up and received three dozen lashes,
which, considering his offence, was a very light punishment. I
suppose that his noble bearing, his lion-hearted courage, and his
undaunted manner, produced a favorable feeling in the captain’s
mind; especially as he afterwards became his favorite—a fancy man—
as those men are called who win the favor of their superior officer.
One of Sadler’s failings was that too prevalent evil among seamen,
drunkenness. Soon after the above affair, he got drunk. Being seen
by the captain, he was ordered to be put in irons. Sadler was Bob
Hammond’s messmate; this worthy, finding his comrade in trouble,
made himself drunk, and purposely placed himself in the way of the
officers, that he might be put in irons also, to keep his friend Sadler
company. The plan succeeded. Bob had his wish, and the two
fearless tars were soon ironed together. Nothing daunted, they began
to sing, and through the whole night they kept up such a hallooing,
shouting and singing as might have served for a whole company of
idle roysterers. Being near the ward-room, they prevented the
officers from sleeping nearly all night.
As usual, after being in irons, they were brought up for punishment
the next morning. “Well, Mr. Sadler,” said the captain, “you were
drunk, were you, last night?”
“I was, sir,” replied the offender.
Had he been any other man, he would have been ordered to strip:
as it was, the captain proceeded,—
“Do you feel sorry for it, sir?”
“I do, sir.”
“Will you try to keep sober if I forgive you?” continued Captain
Carden.
“I will try, sir.”
“Then, sir, I forgive you:” and no doubt he was glad to witness that
contrition in his favorite which made it consistent to forgive him.
Having dismissed Sadler, he turned to Hammond: assuming a sterner
look and a harsher voice, he said, in a tone of irony, “Well, Mr.
Hammond, you got drunk last night, did you, sir?”
Bob shrugged up his shoulders, and removed his enormous quid
into a convenient position for speaking, and then replied, “I can’t say
but that I had a horn of malt.”
The captain looked thunder at the stalwart man, as he answered,
“A horn of malt, you rascal! what do you call a horn of malt?”
“When I was in Bengal, Madras, and Batavia,” said he, “I used to
get some stuff called arrack—we used to call it a horn of malt; but
this was some good rum.”
Bob’s manner was so exquisitely ridiculous while delivering this
harangue, that both officers and men broke out into an involuntary
laugh. The captain looked confounded, but recovering himself, he
said to Mr. Hope, the first lieutenant, “Put that rascal in irons; it is of
no use to flog him.”
One of the peculiarities of Captain Carden was an ardent desire to
have a crew of picked, first-rate men. The shiftless, slovenly seaman
was his abhorrence. Had he dared, he would gladly have given all
such their discharge; as it was, he never attempted their recovery, by
offering a reward for their detection, if they ran away; while he
spared no pains to catch an able, active, valuable man like Sadler. He
even gave these drones opportunity to escape, by sending them on
shore at Lisbon, to cut stuff to make brooms for sweeping the deck.
The men sent out on these expeditions were nicknamed “broomers.”
Now, although Bob Hammond was as expert a sailor as any man in
the ship, yet his unconquerable audacity made the captain fear his
influence, and wish to get rid of him; hence, a few days after this
drunken spree, Bob was called on deck to go with the broomers. “You
may go, Mr. Hammond,” said the captain, eyeing him in a very
expressive manner, “with these fellows to cut broom.”
Bob understood the hint perfectly, and replied, “Aye, aye, sir, and I
will cut a long handle to it.” I scarcely need remark that the broomers
returned without Bob. Whether he remained on shore to cut the long
handle, or for some other purpose, he never informed us: certain it
is, however, that the presence of Bob Hammond never darkened the
decks of the Macedonian again.
About this time the prevailing topic of conversation among our men
and officers was the probability of a war with America. The prevailing
feeling through the whole fleet was that of confidence in our own
success, and of contempt for the inferior naval force of our
anticipated enemies. Every man, and especially the officers,
predicted, as his eye glanced proudly on the fine fleet which was
anchored off Lisbon, a speedy and successful issue to the
approaching conflict.
We now received orders to sail to Norfolk, Virginia, with
despatches. The voyage was accomplished without any occurrence of
note. We found ourselves on the American coast, with no very
pleasant impressions. It was late in the fall, and the transition from
the mild, soft climate of Spain and Portugal, to the bleak, sharp
atmosphere of the coast of Virginia, was anything but delightful.
The most disagreeable duty in the ship was that of holy-stoning the
decks on cold, frosty mornings. Our movements were never more
elastic than when at this really severe task. As usual, it gave occasion
to a variety of forecastle yarns about cold stations. Among these was
one which was attested by many witnesses, and there can be no
doubt of its truth:
A British frigate was once stationed in a cold climate. The first
lieutenant was a complete tyrant, delighting in everything that caused
the crew to suffer. Among other things, he took especial care to make
the work of holy-stoning as painful as possible, by forcing them to
continue at it much longer than was necessary. Although he had no
watch on deck, he would contrive to be up in season to annoy the
men with his hated presence. One morning, the weather being
unusually severe, the men sprang to their task with unwonted agility,
and contrived to finish it before the appearance of their persecutor.
To their vexation, however, just as they had completed their work, he
bounced on deck, with a peremptory order to wash the decks all over
a second time.
The men dropped on their knees with the holy-stones, and prayed,
as the tyrant went below, that he might never come on deck again
alive. Whether God heard the cry of the oppressed crew, or whether
it was the action of the ordinary natural laws, the reader must
determine for himself; but when the lieutenant again appeared on
deck, he was brought up “feet foremost,” to be buried. He was taken
sick that morning: his disease baffled the skill of the surgeon, and in
a few days he was a corpse. The opinion that he died a monument of
the divine displeasure against cruel, hard-hearted men of power, and
of disregard for the miseries and tears of the oppressed poor, is at
least worthy of serious consideration.
Soon after we had descried land, an American pilot came on board
to pilot us into Hampton Roads. The sound of our own familiar
tongue from a stranger, was very agreeable to men who had been
accustomed to hear the semi-barbarous lingo of the Portuguese, and
a thrill of home remembrances shot through our hearts, as, stepping
on deck, the pilot exclaimed, “It is very cold!”
While at anchor in Hampton Roads, we fared well. Boats were
alongside every day with plenty of beef and pork, which was
declared, by universal consent, to be infinitely superior to what we
obtained from Portugal. Our men said that the Yankee pork would
swell in the pot, which they very sagely accounted for on the
supposition that the pigs were killed at the full of the moon. But I
suppose that Virginia corn had more to do in this matter than lunar
influences; though our men most doggedly maintained the contrary
and more mystical opinion.
The principal draw-back on the enjoyment of our stay at Norfolk,
was the denial of liberty to go on shore. The strictest care was taken
to prevent all communication with the shore, either personally or by
letter. The reason of this prohibition was a fear lest we should desert.
Many of our crew were Americans: some of these were pressed men;
others were much dissatisfied with the severity, not to say cruelty, of
our discipline; so that a multitude of the crew were ready to give “leg
bail,” as they termed it, could they have planted their feet on
American soil. Hence our liberty was restrained.
Our officers never enjoyed better cheer than during our stay at this
port. Besides feasting among themselves on the fine fat beef, geese
and turkeys, which came alongside in abundance, they exchanged
visits with Commodore Decatur and his officers, of the frigate United
States, then lying at Norfolk. These visits were seasons of much
wassail and feasting. I remember overhearing Commodore Decatur
and the captain of the Macedonian joking about taking each other’s
ship, in case of a war; and some of the crew said that a bet of a
beaver hat passed between them on the issue of such a conflict.
They probably little thought that this joking over a wine-cup, would
afterwards be cracked in earnest, in a scene of blood and carnage.
It was at this port that the difficulty between the British ship
Leopard and the American frigate Chesapeake took place. Several
American seamen, having escaped from the former, took refuge on
board the latter. The captain of the Leopard demanded their
restoration; the captain of the Chesapeake refused submission to the
demand. The Leopard fired into the frigate, which, being of inferior
force, struck to her opponent. As it was a time of peace, the
Chesapeake was not kept as a prize; the claimed men were taken
from her, and she was restored. This was among the circumstances
which led to the war of 1812.
The despatches delivered, and the object of the voyage
accomplished, we once more put to sea; having first laid in a liberal
store of our favorite beef, together with a quantity of Virginia beans,
called Calavances, which were in high favor with our men. To those of
our crew who were Americans, this was rather an unpleasant event.
Like the fabled Tantalus, they had the cup at their mouths, but it
receded before they could taste its contents. They had been at the
threshold of “home, sweet home,” but had not been permitted to step
within its doors. Some of them felt this very keenly, especially a boy,
who belonged to New York, named Jesse Lloyd. In truth, it was a
hard lot.
A quick winter passage brought us to Lisbon, where the arrival of
the English mail-bag, and orders to proceed to England with a convoy
of merchantmen, put us all into a tolerably good humor.
The arrival of the mail-bag is a season of peculiar interest on board
a man of war. It calls the finer feelings of human nature into exercise.
It awakens conjugal, fraternal, and filial affection in almost every
breast. The men crowd around, as the letters are distributed, and he
was pronounced a happy fellow whose name was read off by the
distributor; while those who had none, to hide their disappointment,
would jocularly offer to buy those belonging to their more fortunate
messmates.
During the two years of our absence I had received several letters
from my mother, which afforded me much satisfaction. To these I had
faithfully replied. I now experienced the advantage of the primary
education I had received when a boy. Many of my shipmates could
neither read nor write, and were, in consequence either altogether
deprived of the privilege of intercourse with their friends, or were
dependent on the kindness of others, to read and write for them. For
these I acted as a sort of scribe. I also solaced many weary hours by
reading such works as could be obtained from the officers; and
sometimes I perused the Bible and prayer book which my mother so
wisely placed in my chest, on the eve of my departure. The pack of
cards, which so inappropriately accompanied them, I had loaned to
one of the officers, who took the liberty to keep them. This was,
perhaps, more fortunate than otherwise, since their possession might
have led to their use, and their use might have excited a propensity
to gambling, which would have ended in my ruin.
After remaining a very short time at Lisbon, we one morning fired a
gun to give notice to our convoy to get under weigh. Immediately the
harbor was alive with noise and activity. The song of the sailors
weighing anchor, the creaking of pulleys, the flapping of the sails, the
loud, gruff voices of the officers, and the splashing of the waters,
created what was to us, now that we were “homeward bound,” a
sweet harmony of sounds. Amid all this animation, our own stately
frigate spread her bellying sails to a light but favoring breeze; with
colors flying, our band playing lively airs, and the captain with his
speaking trumpet urging the lagging merchant-ships to more activity,
we passed gaily through the large fleet consigned to our care. In this
gallant style we scudded past the straggling ruins of old Lisbon,
which still bore marks of the earthquake that destroyed it. Very soon
the merry fishermen, who abound in the Tagus, were far at our stern.
Next, we glided past the tall granite pinnacles of towering mount
Cintra; the high-lands passed from our vision like the scenes in a
panorama, and in a few hours, instead of the companionship of the
large flocks of gulls, which abound in this river, we were attended by
only here and there one of these restless wanderers of the deep. We
were fairly at sea, and, what was the more inspiring, we were
enjoying the luxury of fond anticipation. Visions of many an old fire-
side, of many a humble hearth-stone, poor, but precious, flitted
across the visions of our crew that night. Hardships, severe discipline,
were for the time forgotten in the dreams of hope. Would that I could
say that everything in every mind was thus absorbed in pleasure!
There were minds that writhed under what is never forgotten. Like
the scar, that time may heal, but not remove, the flogged man
forgets not that he has been degraded; the whip, when it scarred the
flesh, went farther; it wounded the spirit; it struck the man; it begat
a sense of degradation he must carry with him to his grave. We had
many such on board our frigate; their laugh sounded empty, and
sometimes their look became suddenly vacant in the midst of hilarity.
It was the whip entering the soul anew. But the most of our crew were,
for the time, happy. They were homeward bound!
CHAPTER V
A
fter running a few days before a fair wind, the delightful cry of
“Land ho!” was heard from the mast-head; a cry always
pleasant to the inhabitant of a ship, but most especially so
when the distant hills are those of his native land. Soon after the cry
of the man aloft, the land became dimly visible from the deck, and
our eyes glistened, as the bright, emerald fields of old England, in all
the glory of their summer beauty, lay spread out before us. Ascending
the British Channel, we soon made the spacious harbor of Plymouth,
where we came to an anchor. One of our convoy, however, by some
unskilful management, ran ashore at the mouth of the harbor, where
she went to pieces.
We found Plymouth to be a naval station of considerable
importance, well fortified, possessing extensive barracks for the
accommodation of the military, and having a magnificent dock-yard,
abundantly supplied with the means of building and refitting the
wooden walls.
Nothing would have afforded me a higher gratification, than a trip
to the pleasant fields and quiet hearth-sides of dear old Bladen. I
longed to pour out my pent-up griefs into the bosom of my mother,
and to find that sympathy which is sought in vain in the cold,
unfeeling world. This privilege was, however, denied to all. No one
could obtain either leave of absence or money, since a man of war is
never “paid off” until just before she proceeds to sea. But, feeling
heartily tired of the service, I wrote to my mother, requesting her to
endeavor to procure my discharge. This, with the promptitude of
maternal affection, she pledged herself to do at the earliest possible
opportunity. How undying is a mother’s love!
When a man of war is in port, it is usual to grant the crew
occasional liberty to go on shore. These indulgences are almost
invariably abused for purposes of riot, drunkenness and debauchery;
rarely does it happen, but that these shore sprees end in bringing
“poor Jack” into difficulty of some sort; for, once on shore, he is like
an uncaged bird, as gay and quite as thoughtless. He will then follow
out the dictates of passions and appetites, let them lead him whither
they may. Still, there are exceptions; there are a few who spend their
time more rationally. Were the principles of modern temperance fully
triumphant among sailors, they would all do so.
I resolved not to abuse my liberty as I saw others doing; so when,
one fine Sabbath morning, I had obtained leave from our surly first
lieutenant, I chose the company of a brother to a messmate, named
Rowe, who lived at Plymouth. At the request of my messmate, I
called to see him. He received me very kindly, and took me in
company with his children into the fields, where the merry notes of
the numerous birds, the rich perfume of the blooming trees, the tall,
green hedges, and the modest primroses, cowslips and violets, which
adorned the banks on the road-side, filled me with inexpressible
delight. True, this was not the proper manner of spending a Sabbath
day, but it was better than it would have been to follow the example
of my shipmates generally, who were carousing in the tap-rooms of
the public houses.
At sunset I went on board and walked aft to the lieutenant, to
report myself. He appeared surprised to see me on board so early
and so perfectly sober, and jocosely asked me why I did not get
drunk and be like a sailor. Merely smiling, I retired to my berth,
thinking it was very queer for an officer to laugh at a boy for doing
right, and feeling happy within myself because I had escaped
temptation.
By and by, three other boys, who had been ashore, returned, in a
state which a sailor would call “three sheets in the wind.” They
blustered, boasted of the high time they had enjoyed, and roundly
laughed at me for being so unlike a man-of-war’s-man; while they felt
as big as any man on board. The next morning, however, they looked
rather chop-fallen, when the captain, who had accidentally seen their
drunken follies on shore, ordered them to be flogged, and forbade
their masters to send them ashore while we remained at Plymouth.
Now, then, it was pretty evident who had the best cruise; the joke
was on the other side; for while their drunken behavior cost them a
terrible whipping and a loss of liberty, my temperance gained me the
real approbation of my officers, and more liberty than ever, since
after that day I had to go on shore to do errands for their masters, as
well as for my own. The young sailor may learn from this fact the
benefit of temperance, and the folly of getting drunk, for the sake of
being called a fine fellow.
My frequent visits to the shore gave me many opportunities to run
away; while my dislike of everything about the Macedonian inspired
me with the disposition to improve them. Against this measure my
judgment wisely remonstrated, and, happily for my well being,
succeeded. Such an attempt would inevitably have been followed by
my recovery, since a handsome bounty was paid for the delivery of
every runaway. There are always a sufficient number to be found who
will engage in pursuit for the sake of money—such men as the
Canadian landlord, described by Rev. Wm. Lighton, in his interesting
narrative,[6] a work with which, no doubt, most of my readers are
acquainted, since it has enjoyed an immense circulation. Endurance,
therefore, was the only rational purpose I could form.
Perhaps the hope of a speedy discharge, through my mother’s
efforts, tended somewhat to this result in my case; besides, my
situation had become somewhat more tolerable from the fact, that by
dint of perseverance in a civil and respectful behavior, I had gained
the good will both of the officers and crew. Yet, with this advantage,
it was a miserable situation.
There are few worse places than a man of war, for the favorable
development of the moral character in a boy. Profanity, in its most
revolting aspect; licentiousness, in its most shameful and beastly
garb; vice, in the worst of its Proteus-like shapes, abound there.
While scarcely a moral restraint is thrown round the victim, the
meshes of temptation are spread about his path in every direction.
Bad as things are at sea, they are worse in port. There, boat-loads of
defiled and defiling women are permitted to come alongside; the
men, looking over the side, select whoever best pleases his lustful
fancy, and by paying her fare, he is allowed to take and keep her on
board as his paramour, until the ship is once more ordered to sea.
Many of these lost, unfortunate creatures are in the springtime of life,
some of them are not without pretensions to beauty. The ports of
Plymouth and Portsmouth are crowded with these fallen beings. How
can a boy be expected to escape pollution, surrounded by such works
of darkness? Yet, some parents send their children to sea because
they are ungovernable ashore! Better send them to the house of
correction.
There is one aspect in which life at sea and life in port materially
differ. At sea, a sense of danger, an idea of insecurity, is ever present
to the mind; in harbor, a sense of security lulls the sailor into
indulgence. He feels perfectly safe. Yet, even in harbor, danger
sometimes visits the fated ship, stealing upon her like the spirit of
evil. This remark was fearfully illustrated in the loss of the Royal
George, which sunk at Spithead, near Portsmouth, on the 29th of
August, 1782.
This splendid line of battle ship of one hundred and eight guns, had
arrived at Spithead. Needing some repairs, she was “heeled down,” or
inclined on one side, to allow the workmen to work on her sides.
Finding more needed to be done to the copper sheathing than was
expected, the sailors were induced to heel her too much. While in this
state, she was struck by a slight squall; the cannon rolled over to the
depressed side; her ports were open, she filled with water, and sunk
to the bottom!
This dreadful catastrophe occurred about ten o’clock in the
morning. The brave Admiral Kempenfeldt was writing in his cabin;
most of the crew, together with some three hundred women, were
between decks: these nearly all perished. Captain Waghorn, her
commander, was saved; his son, one of her lieutenants, was lost.
Those who were on the upper deck were picked up by the boats of
the fleet, but nearly one thousand souls met with a sudden and
untimely end. The poet Cowper has celebrated this melancholy event
in the following beautiful lines:
Toll for the brave!
The brave that are no more!
All sunk beneath the wave,
Fast by their native shore.
ebookgate.com