0% found this document useful (0 votes)
17 views

Python Data Science Cookbook Gopi Subramanian download

The document provides information about the 'Python Data Science Cookbook' by Gopi Subramanian, including links to download the book and related resources. It covers various topics in data science using Python, including data analysis, machine learning, and practical recipes for data manipulation. Additionally, it includes details about the author and reviewer, as well as support resources available through Packt Publishing.

Uploaded by

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

Python Data Science Cookbook Gopi Subramanian download

The document provides information about the 'Python Data Science Cookbook' by Gopi Subramanian, including links to download the book and related resources. It covers various topics in data science using Python, including data analysis, machine learning, and practical recipes for data manipulation. Additionally, it includes details about the author and reviewer, as well as support resources available through Packt Publishing.

Uploaded by

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

Python Data Science Cookbook Gopi Subramanian

download

https://ebookbell.com/product/python-data-science-cookbook-gopi-
subramanian-50195042

Explore and download more ebooks at ebookbell.com


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

Python Data Science Cookbook 1st Edition Gopi Subramanian

https://ebookbell.com/product/python-data-science-cookbook-1st-
edition-gopi-subramanian-38440514

Python Data Science Cookbook Gopi Subramanian

https://ebookbell.com/product/python-data-science-cookbook-gopi-
subramanian-33681254

Python Data Science Cookbook Over 60 Practical Recipes To Help You


Explore Python And Its Robust Data Science Capabilities Gopi
Subramanian

https://ebookbell.com/product/python-data-science-cookbook-
over-60-practical-recipes-to-help-you-explore-python-and-its-robust-
data-science-capabilities-gopi-subramanian-10816954

Python Data Science Cookbook Gopi Subramanian Gopi Subramanian

https://ebookbell.com/product/python-data-science-cookbook-gopi-
subramanian-gopi-subramanian-23686092
Python Data Science Cookbook Gopi Subramanian

https://ebookbell.com/product/python-data-science-cookbook-gopi-
subramanian-7264708

Practical Data Science Cookbook Data Preprocessing Analysis And


Visualization Using R And Python 2nd Edition Prabhanjan Narayanachar
Tattar Tony Ojeda Sean Patrick Murphy Benjamin Bengfort Abhijit
Dasgupta
https://ebookbell.com/product/practical-data-science-cookbook-data-
preprocessing-analysis-and-visualization-using-r-and-python-2nd-
edition-prabhanjan-narayanachar-tattar-tony-ojeda-sean-patrick-murphy-
benjamin-bengfort-abhijit-dasgupta-36155248

Python Data Science Handbook Essential Tools For Working With Data 2nd
Edition 2nd Edition Jake Vanderplas

https://ebookbell.com/product/python-data-science-handbook-essential-
tools-for-working-with-data-2nd-edition-2nd-edition-jake-
vanderplas-47710390

Python Data Science Learn Python In A Week And Master It An Handson


Introduction To Big Data Analysis And Mining A Projectbased Guide With
Practical Exercises 7 Days Crash Course Book 3 Computer Programming
Academy
https://ebookbell.com/product/python-data-science-learn-python-in-a-
week-and-master-it-an-handson-introduction-to-big-data-analysis-and-
mining-a-projectbased-guide-with-practical-exercises-7-days-crash-
course-book-3-computer-programming-academy-49850014

Python Data Science 3 Books In 1 Travis Booth

https://ebookbell.com/product/python-data-science-3-books-in-1-travis-
booth-50417292
Table of Contents
Python Data Science Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Python for Data Science
Introduction
Using dictionary objects
Getting ready
How to do it…
How it works…
There's more…
See also
Working with a dictionary of dictionaries
Getting ready
How to do it…
How it works…
See also
Working with tuples
Getting ready
How to do it…
How it works…
There's more…
See also
Using sets
Getting ready
How to do it…
How it works…
There's more…
Writing a list
Getting ready
How to do it…
How it works…
There's more…
Creating a list from another list - list comprehension
Getting ready
How to do it…
How it works…
There's more…
Using iterators
Getting ready
How to do it…
How it works…
There's more…
Generating an iterator and a generator
Getting ready
How it do it…
How it works…
There's more…
Using iterables
Getting ready
How to do it…
How it works..
See also
Passing a function as a variable
Getting ready
How to do it…
How it works…
Embedding functions in another function
Getting ready
How to do it…
How it works…
Passing a function as a parameter
Getting ready
How to do it…
How it works…
Returning a function
Getting ready
How to do it…
How it works…
There's more…
Altering the function behavior with decorators
Getting ready
How to do it…
How it works…
Creating anonymous functions with lambda
Getting ready
How to do it…
How it works…
Using the map function
Getting ready
How to do it…
How it works…
There's more…
Working with filters
Getting ready
How to do it…
How it works…
Using zip and izip
Getting ready
How to do it…
How it works…
There's more…
See also
Processing arrays from the tabular data
Getting ready
How to do it…
How it works…
There's more…
Preprocessing the columns
Getting ready
How to do it…
How it works…
There's more…
Sorting lists
Getting ready
How to do it…
How it works…
There's more…
Sorting with a key
Getting ready
How to do it…
How it works…
There's more…
Working with itertools
Getting ready
How to do it…
How it works…
2. Python Environments
Introduction
Using NumPy libraries
Getting ready
How to do it…
How it works…
There's more…
See also
Plotting with matplotlib
Getting ready
How to do it…
How it works…
There's more…
Machine learning with scikit-learn
Getting ready
How to do it…
How it works…
There's more…
See also
3. Data Analysis – Explore and Wrangle
Introduction
Analyzing univariate data graphically
Getting ready
How to do it…
How it works…
See also
Grouping the data and using dot plots
Getting ready
How to do it…
How it works…
See also
Using scatter plots for multivariate data
Getting ready
How to do it…
How it works…
See also
Using heat maps
Getting ready
How to do it…
How it works…
There's more...
See also
Performing summary statistics and plots
Getting ready
How to do it…
How it works…
See also
Using a box-and-whisker plot
Getting ready
How to do it…
How it works…
There's more…
Imputing the data
Getting ready
How to do it…
How it works…
There's more…
See also
Performing random sampling
Getting ready
How to do it…
How it works…
There's more…
Stratified sampling
Progressive sampling
Scaling the data
Getting ready
How to do it…
How it works…
There's more…
Standardizing the data
Getting ready
How to do it…
How it works…
There's more…
Performing tokenization
Getting ready
How to do it…
How it works…
There's more…
See also
Removing stop words
How to do it…
How it works…
There's more…
See also
Stemming the words
Getting ready
How to do it…
How it works…
There's more…
See also
Performing word lemmatization
Getting ready
How to do it…
How it works…
There's more…
See also
Representing the text as a bag of words
Getting ready
How to do it…
How it works…
There's more…
See also
Calculating term frequencies and inverse document frequencies
Getting ready
How to do it…
How it works…
There's more…
4. Data Analysis – Deep Dive
Introduction
Matrix Decomposition:
Extracting the principal components
Getting ready
How to do it…
How it works…
There's more…
See also
Using Kernel PCA
Getting ready
How to do it…
How it works…
There's more…
Extracting features using singular value decomposition
Getting ready
How to do it…
How it works…
There's more…
Reducing the data dimension with random projection
Getting ready
How to do it…
How it works…
There's more…
See also
Decomposing the feature matrices using non-negative matrix
factorization
Getting ready
How to do it…
How it works…
There's more…
See also
5. Data Mining – Needle in a Haystack
Introduction
Working with distance measures
Getting ready
How to do it…
How it works…
There's more...
See also
Learning and using kernel methods
Getting ready
How to do it…
How it works…
There's more...
See also
Clustering data using the k-means method
Getting ready
How to do it…
How it works…
There's more...
See also
Learning vector quantization
Getting ready
How to do it…
How it works…
There's more...
See also
Finding outliers in univariate data
Getting ready
How to do it…
How it works…
There's more…
See also
Discovering outliers using the local outlier factor method
Getting ready
How to do it…
How it works…
There's more…
6. Machine Learning 1
Introduction
Preparing data for model building
Getting ready
How to do it…
How it works…
There's more...
Finding the nearest neighbors
Getting ready
How to do it…
How it works…
There's more…
See also
Classifying documents using Naïve Bayes
Getting ready
How to do it…
How it works…
There's more…
See also
Building decision trees to solve multiclass problems
Getting ready
How to do it…
How it works…
There's more…
See also
7. Machine Learning 2
Introduction
Predicting real-valued numbers using regression
Getting ready
How to do it…
How it works…
There's more...
See also
Learning regression with L2 shrinkage – ridge
Getting ready
How to do it…
How it works…
There's more…
See also
Learning regression with L1 shrinkage – LASSO
Getting ready
How to do it…
How it works…
There's more…
See also
Using cross-validation iterators with L1 and L2 shrinkage
Getting ready
How to do it…
How it works…
There's more…
See also
8. Ensemble Methods
Introduction
Understanding Ensemble – Bagging Method
Getting ready…
How to do it
How it works…
There's more…
See also
Understanding Ensemble – Boosting Method
Getting Started…
How to do it
How it works…
There's more…
See also
Understanding Ensemble – Gradient Boosting
Getting Started…
How to do it
How it works…
There's more…
See also
9. Growing Trees
Introduction
Going from trees to Forest – Random Forest
Getting ready
How to do it...
How it works…
There's more…
See also
Growing Extremely Randomized Trees
Getting ready…
How to do it...
How it works…
There's more…
See also
Growing Rotational Forest
Getting ready…
How to do it...
How it works…
There's more…
See also
10. Large-Scale Machine Learning – Online Learning
Introduction
Using perceptron as an online learning algorithm
Getting ready
How to do it…
How it works…
There's more…
See also
Using stochastic gradient descent for regression
Getting ready
How to do it…
How it works…
There's more…
See also
Using stochastic gradient descent for classification
Getting ready
How to do it…
How it works…
There's more…
See also
Index
Python Data Science
Cookbook
Python Data Science
Cookbook
Copyright © 2015 Packt Publishing All rights reserved.
No part of this book may be reproduced, stored in a
retrieval system, or transmitted in any form or by any
means, without the prior written permission of the
publisher, except in the case of brief quotations
embedded in critical articles or reviews.

Every effort has been made in the preparation of this


book to ensure the accuracy of the information
presented. However, the information contained in this
book is sold without warranty, either express or implied.
Neither the author nor Packt Publishing, and its dealers
and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by
this book.

Packt Publishing has endeavored to provide trademark


information about all of the companies and products
mentioned in this book by the appropriate use of
capitals. However, Packt Publishing cannot guarantee
the accuracy of this information.

First published: November 2015

Production reference: 1041115


Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78439-640-4

www.packtpub.com
Credits
Author

Gopi Subramanian

Reviewer

Bastiaan Sjardin

Commissioning Editor

Akram Hussain

Acquisition Editor

Nikhil Karkal

Content Development Editor

Siddhesh Salvi

Technical Editor

Danish Shaikh

Copy Editor

Tasneem Fatehi

Project Coordinator
Kranti Berde

Proofreader

Safis Editing

Indexer

Mariammal Chettiyar

Graphics

Disha Haria

Production Coordinator

Nilesh Mohite

Cover Work

Nilesh Mohite
About the Author
Gopi Subramanian is a data scientist with over 15 years
of experience in the field of data mining and machine
learning. During the past decade, he has designed,
conceived, developed, and led data mining, text mining,
natural language processing, information extraction and
retrieval, and search systems for various domains and
business verticals, including engineering infrastructure,
consumer finance, healthcare, and materials. In the
loyalty domain, he has conceived and built innovative
consumer loyalty models and designed enterprise-wide
systems for personalized promotions. He has filed over
ten patent applications at the US and Indian patent office
and has several publications to his credit. He currently
lives and works in Bangaluru, India.
About the Reviewer
Bastiaan Sjardin is a data scientist and entrepreneur
with a background in artificial intelligence, mathematics,
and machine learning. He has an MSc degree in
cognitive science and mathematical statistics from the
University of Leiden. In the past 5 years, he has worked
on a wide range of data science projects. He is a
frequent community TA at Coursera in the social network
analysis course from the University of Michigan and the
practical machine learning course from Johns Hopkins
University. His programming language of choice is R and
Python. Currently, he is the cofounder of Quandbee
(www.quandbee.com), a company specializing in
machine learning applications.
www.PacktPub.com
Support files, eBooks,
discount offers, and more
For support files and downloads related to your book,
please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every


book published, with PDF and ePub files available? You
can upgrade to the eBook version at www.PacktPub.com
and as a print book customer, you are entitled to a
discount on the eBook copy. Get in touch with us at
<service@packtpub.com> for more details.

At www.PacktPub.com, you can also read a collection of


free technical articles, sign up for a range of free
newsletters and receive exclusive discounts and offers
on Packt books and eBooks.

https://www2.packtpub.com/books/subscription/packtlib

Do you need instant solutions to your IT questions?


PacktLib is Packt's online digital book library. Here, you
can search, access, and readPackt's entire library of
books.

Why Subscribe?
Fully searchable across every book published by Packt

Copy and paste, print, and bookmark content

On demand and accessible via a web browser

Free Access for Packt account


holders
If you have an account with Packt at
www.PacktPub.com, you can use this to access
PacktLib today and view 9 entirely free books. Simply
use your login credentials for immediate access.
Preface
Today, we live in a world of connected things where tons
of data is generated and it is humanly impossible to
analyze all the incoming data and make decisions.
Human decisions are increasingly replaced by decisions
made by computers. Thanks to the field of data science.
Data science has penetrated deeply in our connected
world and there is a growing demand in the market for
people who not only understand data science algorithms
thoroughly, but are also capable of programming these
algorithms. Data science is a field that is at the
intersection of many fields, including data mining,
machine learning, and statistics, to name a few. This
puts an immense burden on all levels of data scientists;
from the one who is aspiring to become a data scientist
and those who are currently practitioners in this field.
Treating these algorithms as a black box and using them
in decision-making systems will lead to
counterproductive results. With tons of algorithms and
innumerable problems out there, it requires a good grasp
of the underlying algorithms in order to choose the best
one for any given problem.

Python as a programming language has evolved over


the years and today, it is the number one choice for a
data scientist. Its ability to act as a scripting language for
quick prototype building and its sophisticated language
constructs for full-fledged software development
combined with its fantastic library support for numeric
computations has led to its current popularity among
data scientists and the general scientific programming
community. Not just that, Python is also popular among
web developers; thanks to frameworks such as Django
and Flask.

This book has been carefully written to cater to the


needs of a diverse range of data scientists—starting
from novice data scientists to experienced ones—
through carefully crafted recipes, which touch upon the
different aspects of data science, including data
exploration, data analysis and mining, machine learning,
and large scale machine learning. Each chapter has
been carefully crafted with recipes exploring these
aspects. Sufficient math has been provided for the
readers to understand the functioning of the algorithms
in depth. Wherever necessary, enough references are
provided for the curious readers. The recipes are written
in such a way that they are easy to follow and
understand.

This book brings the art of data science with power


Python programming to the readers and helps them
master the concepts of data science. Knowledge of
Python is not mandatory to follow this book. Non-Python
programmers can refer to the first chapter, which
introduces the Python data structures and function
programming concepts.

The early chapters cover the basics of data science and


the later chapters are dedicated to advanced data
science algorithms. State-of-the-art algorithms that are
currently used in practice by leading data scientists
across industries including the ensemble methods,
random forest, regression with regularization, and others
are covered in detail. Some of the algorithms that are
popular in academia and still not widely introduced to the
mainstream such as rotational forest are covered in
detail.

With a lot of do-it-yourself books on data science today


in the market, we feel that there is a gap in terms of
covering the right mix of math philosophy behind the
data science algorithms and implementation details. This
book is an attempt to fill this gap. With each recipe, just
enough math introductions are provided to contemplate
how the algorithm works; I believe that the readers can
take full benefits of these methods in their applications.

A word of caution though is that these recipes are written


with the objective of explaining the data science
algorithms to the reader. They have not been hard-
tested in extreme conditions in order to be production
ready. Production-ready data science code has to go
through a rigorous engineering pipeline.

This book can be used both as a guide to learn data


science methods and quick references. It is a self-
contained book to introduce data science to a new
reader with little programming background and help
them become experts in this trade.
What this book covers
Chapter 1, Python for Data Science, introduces Python's
built-in data structures and functions, which are very
handy for data science programming.

Chapter 2, Python Environments, introduces Python's


scientific programming and plotting libraries, including
NumPy, matplotlib, and scikit-learn.

Chapter 3, Data Analysis – Explore and wrangle, covers


data preprocessing and transformation routines to
perform exploratory data analysis tasks in order to
efficiently build data science algorithms.

Chapter 4, Data Analysis – Deep Dive, introduces the


concept of dimensionality reduction in order to tackle the
curse of dimensionality issues in data science. Starting
with simple methods and moving on to the advanced
state-of-the-art dimensionality reduction techniques are
discussed in detail.

Chapter 5, Data Mining – Needle in a haystack Name,


discusses unsupervised data mining techniques, starting
with elaborate discussions on distance methods and
kernel methods and following it up with clustering and
outlier detection techniques.

Chapter 6, Machine Learning 1, covers supervised data


mining techniques, including nearest neighbors, Naïve
Bayes, and classification trees. In the beginning, we will
lay a heavy emphasis on data preparation for supervised
learning.

Chapter 7, Machine Learning 2, introduces regression


problems and follows it up with topics on regularization
including LASSO and ridge. Finally, we will discuss
cross-validation techniques as a way to choose
hyperparameters for these methods.

Chapter 8, Ensemble Methods, introduces various


ensemble techniques including bagging, boosting, and
gradient boosting This chapter shows you how to make a
powerful state-of-the-art method in data science where,
instead of building a single model for a given problem,
an ensemble or a bag of models are built.

Chapter 9, Growing Trees, introduces some more


bagging methods based on tree-based algorithms. Due
to their robustness to noise and universal applicability to
a variety of problems, they are very popular among the
data science community.

Chapter 10, Large scale machine learning – Online


Learning, covers large scale machine learning and
algorithms suited to tackle such large scale problems.
This includes algorithms that work with streaming data
and data that cannot be fitted into memory completely.
What you need for this book
All the recipes in this book were developed and tested
on an 8 GB machine with Intel i7 CPU running Windows
7 64-bit software.

Python 2.7.5, NumPy 1.8.0, SciPy 0.13.2, Matplotlib


1.3.1, NLTK 3.0.2, and scikit-learn 0.15.2 versions were
used for the developing methods.

The same code should work on Linux variants and Macs


with the appropriate libraries mentioned here.
Alternatively, a Python virtual environment can be
created with the version of these libraries and you can
run all the recipes.
Who this book is for
This book is intended for all levels of data science
professionals, both students and practitioners from
novice to experts. Different recipes in the chapters cater
to the needs of different audiences. Novice readers can
spend some time in getting themselves acquainted with
data science in the first five chapters. Experts can refer
to the later chapters to refer/understand how advanced
techniques are implemented using Python. The book
covers just enough mathematics and provides the
necessary references for computer programmers who
wish to understand data science. People from a non-
Python background can effectively use this book. The
first chapter of the book introduces Python as a
programming language for data science. It will be helpful
if you have some prior basic programming experience.
The book is mostly self-contained and introduces data
science to a new reader and can help him become an
expert in this trade.
Sections
In this book, you will find several headings that appear
frequently (Getting ready, How to do it, How it works,
There's more, and See also).

To give clear instructions on how to complete a recipe,


we use these sections as follows:

Getting ready
This section tells you what to expect in the recipe, and
describes how to set up any software or any preliminary
settings required for the recipe.

How to do it…
This section contains the steps required to follow the
recipe.

How it works…
This section usually consists of a detailed explanation of
what happened in the previous section.

There's more…
This section consists of additional information about the
recipe in order to make the reader more knowledgeable
about the recipe.

See also
This section provides helpful links to other useful
information for the recipe.
Conventions
In this book, you will find a number of text styles that
distinguish between different kinds of information. Here
are some examples of these styles and an explanation of
their meaning.

Code words in text,like function names are shown as


follows:

We call get_iris_data() function to get the input


data. We leverage the function train_test_split
from Scikit learn's model cross_validation to split
the input datasets into two.

A block of code is set as follows:

# Shuffle the dataset


shuff_index =
np.random.shuffle(range(len(y)))
x_train =
x[shuff_index,:].reshape(x.shape)
y_train =
np.ravel(y[shuff_index,:])

Formulas are typically provided as images as follows,

Typically the math section is introduced at the beginning


of each recipe. In some chapters the common math
required for most of the recipes in that chapter are
included in the introduction section of the first recipe.

External url's are specified as follows:

http://scikit-
learn.org/stable/modules/generated/sklearn.metrics.log_l
oss.html

Specific call-outs in some algorithm implementation


details in a third party library is provided as follows.

'The predicted class of an input sample is computed as


the class with the highest mean predicted probability. If
base estimators do not implement a predict_proba
method, then it resorts to voting.'

Where ever applicable references to scientific journals


and papers are provided as follows:

Please refer to the paper by Leo Breiman for more


information about bagging.

Leo Breiman. 1996. Bagging predictors.Mach. Learn.24,


2 (August 1996), 123-140.
DOI=10.1023/A:1018054314350
http://dx.doi.org/10.1023/A:1018054314350

Program output and graphs are typically provided as


images. For example:
Any command-line input or output is written as follows:

Counter({'Peter': 4, 'of': 4, 'Piper': 4,


'pickled': 4, 'picked': 4, 'peppers': 4,
'peck': 4, 'a': 2, 'A': 1, 'the': 1,
'Wheres': 1, 'If': 1})

In places where we would like the reader to inspect


some of the variables in Python shell, we specify it as
follows:

>>> print b_tuple[0]


1
>>> print b_tuple[-1]
c
>>>

NOTE
Warnings or important notes appear in a box like this.

TIP
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us
know what you think about this book—what you liked or
disliked. Reader feedback is important for us as it helps
us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail


<feedback@packtpub.com>, and mention the book's
title in the subject of your message.

If there is a topic that you have expertise in and you are


interested in either writing or contributing to a book, see
our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we
have a number of things to help you to get the most from
your purchase.

Downloading the example code


You can download the example code files from your
account at http://www.packtpub.com for all the Packt
Publishing books you have purchased. If you purchased
this book elsewhere, you can visit
http://www.packtpub.com/support and register to have
the files emailed directly to you.

Downloading the color images of


this book
We also provide you with a PDF file that has color
images of the screenshots/diagrams used in this book.
The color images will help you better understand the
changes in the output. You can download this file from
http://www.packtpub.com/sites/default/files/downloads/1
234OT_ColorImages.pdf.

Errata
Although we have taken every care to ensure the
accuracy of our content, mistakes do happen. If you find
a mistake in one of our books—maybe a mistake in the
text or the code—we would be grateful if you could
report this to us. By doing so, you can save other
readers from frustration and help us improve subsequent
versions of this book. If you find any errata, please report
them by visiting http://www.packtpub.com/submit-errata,
selecting your book, clicking on the Errata Submission
Form link, and entering the details of your errata. Once
your errata are verified, your submission will be accepted
and the errata will be uploaded to our website or added
to any list of existing errata under the Errata section of
that title.

To view the previously submitted errata, go to


https://www.packtpub.com/books/content/support and
enter the name of the book in the search field. The
required information will appear under the Errata
section.

Piracy
Piracy of copyrighted material on the Internet is an
ongoing problem across all media. At Packt, we take the
protection of our copyright and licenses very seriously. If
you come across any illegal copies of our works in any
form on the Internet, please provide us with the location
address or website name immediately so that we can
pursue a remedy.

Please contact us at <copyright@packtpub.com>


with a link to the suspected pirated material.

We appreciate your help in protecting our authors and


our ability to bring you valuable content.

Questions
If you have a problem with any aspect of this book, you
can contact us at <questions@packtpub.com>, and
we will do our best to address the problem.
Chapter 1. Python for Data
Science
In this chapter, we will cover the following recipes:

Using dictionary objects

Working with a dictionary of dictionaries

Working with tuples

Using sets

Writing a list

Creating a list from another list - list comprehension

Using iterators

Generating an iterator and a generator

Using iterables

Passing a function as a variable

Embedding functions in another function

Passing a function as a parameter

Returning a function

Altering the function behavior with decorators

Creating anonymous functions with lambda

Using the map function

Working with filters

Using zip and izip

Processing arrays from the tabular data

Preprocessing the columns

Sorting lists
Sorting with a key

Working with itertools

Introduction
The Python programming language provides a lot of
built-in data structures and functions that are very handy
for data science programming. In this chapter, we will
look at some that are most frequently used. In the
subsequent chapters, you will see that these will be used
in various sections for different topics. A good grasp of
these will help you in the long run to quickly bootstrap a
program in order to handle data and develop algorithms.

While this chapter is a quick overview of handy data


structures and methods, you will start discovering your
own ways of combining these data structures to achieve
your requirements as you become a seasoned Python
user.

Each of these data structures have an utility, though


different circumstances may require using two or more
data structures in tandem to achieve your requirements.
You will see this in action in some of the examples in this
book.
Using dictionary objects
In Python, containers are objects that can hold any
number of arbitrary objects. They provide a way to
access the child objects and iterate over them.
Dictionary, tuple, list, and set are container objects in
Python. More container types are available with the
collections module. Let's look at the dictionary object
in detail in this section.

Getting ready
Let's look at an example Python script to understand
how a dictionary operates. So, with a text, this script tries
to get the word count, that is, how many times each word
has appeared in the given text.

How to do it…
Let's proceed to demonstrate how to operate a dictionary
in Python. Let's use a simple sentence to demonstrate
the use of a dictionary. We will follow it up with an actual
dictionary creation:

# 1.Load a variable with sentences


sentence = "Peter Piper picked a peck of
pickled peppers A peck of pickled \
peppers Peter Piper picked If Peter Piper
picked a peck of pickled \
peppers Wheres the peck of pickled peppers
Peter Piper picked"
Peter Piper picked"

# 2.Initialize a dictionary object


word_dict = {}

# 3.Perform the word count


for word in sentence.split():
if word not in word_dict:
word_dict[word] =1
else:
word_dict[word]+=1
# 4.print the outputprint (word_dict)

How it works…
The preceding code builds a word frequency table; every
word and its frequency is calculated. The final print
statement produces the following output:

{'a': 2, 'A': 1, 'Peter': 4, 'of': 4,


'Piper': 4, 'pickled': 4, 'picked': 4,
'peppers': 4, 'the': 1, 'peck': 4,
'Wheres': 1, 'If': 1}

The preceding output is a key value pair. For each word


(key), we have a frequency (value). A dictionary data
structure is a hash map where values are stored against
a key. In the preceding example, we used a string as a
key; however, any other immutable data type can also
be used as a key.

Refer to the following URL for a detailed discussion


about mutable and immutable objections in Python:
https://docs.python.org/2/reference/datamodel.html

Similarly, values can be any data type including custom


classes.

In step 2, we initialized the dictionary. Its empty when


initialized. When a new key is added to a dictionary,
accessing the dictionary through the new key will throw
KeyError. In the preceding example in step 3, we
included an if statement in the for loop to handle this
situation. However, we can also use the following:

word_dict.setdefault(word,0)

With every key access to the dictionary, this statement


has to be repeated if we are adding elements to a
dictionary in a loop, as in a loop, we are not aware of
new keys. Rewriting step 3 using setdefault will look
as follows:

for word in sentence.split():


word_dict.setdefault(word,0)
word_dict[word]+=1

There's more…
Python 2.5 and above has a class named
defaultdict; it's in the collections module. This
takes care of the setdefault action. A defaultdict
class is invoked as follows:

from collections import defaultdict


sentence = "Peter Piper picked a peck of
pickled peppers A peck of pickled \
peppers Peter Piper picked If
Peter Piper picked a peck of pickled \
peppers Wheres the peck of
pickled peppers Peter Piper picked"

word_dict = defaultdict(int)

for word in sentence.split():


word_dict[word]+=1print word_dict

As you have noticed, we included


collections.defaultdict in our code and
initialized our dictionary. Note that the int parameter,
defaultdict, takes a function as an argument. In this
case, we passed the int() function and thus, when the
dictionary encounters a key that was not seen before, it
initializes the key with a value returned by the int()
function, in this case, zero. We will use defaultdict
later in this book.

NOTE
A typical dictionary does not remember the order in which the keys were inserted. In its
collections module, Python provides a container called OrderedDict that can remember
the order in which the keys were inserted. See the following Python documentation for
more details:

https://docs.python.org/2/library/collections.html#collections.OrderedDict

Looping through a dictionary is very easy; using the


keys() function provided in the dictionary, we can loop
through the key and using values(), we can loop through
the values or using items(), we can loop through both the
keys and values. Look at the following example:
For key, value in word_dict.items():
print key,value

In this example, using dict.items(), we can iterate


through the keys and values present in the dictionary.

The Python documentation for dictionaries is very


exhaustive and is a handy companion when working with
dictionaries:

https://docs.python.org/2/tutorial/datastructures.html#dict
ionaries

Dictionaries are very useful as an intermediate data


structure. If your program uses JSON as a way to move
around information between modules, dictionary is the
right data type for the job. It is very convenient to load a
dictionary from a JSON file and similarly dump a
dictionary as JSON strings.

Python provides us with libraries to handle JSON very


efficiently:

https://docs.python.org/2/library/json.html

Counter is a dictionary subclass to count the hashable


objects. Our example of the word count can be easily
done using counter.

Look at the following example:

from collections import Counter


sentence = "Peter Piper picked a peck of
pickled peppers A peck of pickled \
peppers Peter Piper picked If
Peter Piper picked a peck of pickled \
peppers Wheres the peck of
pickled peppers Peter Piper picked"

words = sentence.split()

word_count = Counter(words)

print word_count['Peter']print word_dict

The output is as follows and you can verify this output


with the previous one:

Counter({'Peter': 4, 'of': 4, 'Piper': 4,


'pickled': 4, 'picked': 4, 'peppers': 4,
'peck': 4, 'a': 2, 'A': 1, 'the': 1,
'Wheres': 1, 'If': 1})

You can go through the following link to understand


more about Counters:

https://docs.python.org/2/library/collections.html#collectio
ns.Counter

See also
Working with Dictionary of Dictionaries recipe in Chapter 1, Using
Python for Data Science
Working with a dictionary of
dictionaries
As we mentioned earlier, the real power of these data
structures lies in how creatively you can use them to
achieve your tasks. Let's look at an example to
understand how to use dictionaries in a dictionary.

Getting ready
Look at the following table:

In the first column, we have three users and the rest of


the columns are movies. The cell values are ratings
given by a user for a movie. Let's say we want to
represent this in memory so that some other part of a
larger code base can easily access this information. We
will use a dictionary of dictionaries to achieve this
objective.

How to do it…
We will create the user_movie_rating dictionary
using an anonymous function to demonstrate the
concept of a dictionary of dictionaries.

We will fill it with data to show the effective use of a


dictionary of dictionaries:

from collections import defaultdict

user_movie_rating = defaultdict(lambda
:defaultdict(int))

# Initialize ratings for Alice


user_movie_rating["Alice"]["LOR1"] = 4
user_movie_rating["Alice"]["LOR2"] = 5
user_movie_rating["Alice"]["LOR3"] = 3
user_movie_rating["Alice"]["SW1"] = 5
user_movie_rating["Alice"]["SW2"] = 3
print user_movie_rating

How it works…
The user_movie_rating is a dictionary of
dictionaries. As explained in the previous section,
defaultdict takes a function for argument; in this
case, we passed a built-in anonymous function, lambda,
which returns a dictionary. So, every time a new key is
passed to user_movie_rating, a new dictionary will
be created for this key. We will see more about the
lambda function in the subsequent section.

This way, we can access the rating of any user movie


combination very quickly. Similarly, there are plenty of
use cases where a dictionary of dictionaries comes in
very handy.

As a closing note on the dictionary, I would like to


mention that having a good grasp of the dictionary data
structure will help ease a lot of your data science
programming tasks. As we will see later, dictionaries are
frequently used to store features and labels in machine
learning. The Python NLTK library uses a dictionary
extensively to store features in text mining:

http://www.nltk.org/book/ch05.html

The section titled Mapping words to Properties using


Python Dictionaries is a good read to understand how
effectively dictionaries can be used.

See also
Creating Anonymous Functions recipe in Chapter 1, Using Python for
Data Science
Working with tuples
A tuple is a type of container object known as sequence
types in Python. Tuples are immutable and can have a
heterogeneous sequence of elements separated by a
comma and enclosed in parentheses. They support the
following operations:

in and not in

Comparision, concatenation, slicing, and indexing

min() and max()

Getting ready
Rather than having a full program as we did with
dictionaries, we will see tuples as fragmented codes
where we will concentrate on the creation and
manipulation activities.

How to do it…
Let's see some scripts demonstrating the creation and
manipulation of tuples:

# 1.Ways of creating a tuple


a_tuple = (1,2,'a')
b_tuple =1,2,'c'

# 2.Accessing elements of a tuple through


index
print b_tuple[0]
print b_tuple[-1]
print b_tuple[-1]

# 3.It is not possible to change the value


of an item in a tuple,
# for example the next statement will
result in an error.
try:
b_tuple[0] = 20
except:
print "Cannot change value of tuple by
index"

# 4.Though tuples are immutable


# But elements of a tuple can be mutable
objects,
# for instance a list, as in the following
line of code
c_tuple =(1,2,[10,20,30])
c_tuple[2][0] = 100

# 5.Tuples once created cannot be extended


like list,
# however two tuples can be concatenated.

print a_tuple + b_tuple

# 6 Slicing of uples
a =(1,2,3,4,5,6,7,8,9,10)
print a[1:]
print a[1:3]
print a[1:6:2]
print a[:-1]

# 7.Tuple min max


print min(a),max(a)

# 8.in and not in


if 1 in a:
print "Element 1 is available in tuple
a"
else:
print "Element 1 is available in tuple a"
print "Element 1 is available in tuple a"

How it works…
In step 1, we created a tuple. Though strictly speaking,
the parentheses are not needed, still it's an option for
better readability. As you can see, we created a
heterogeneous tuple with numeric and string values.
Step 2 details how the elements of a tuple can be
accessed through the index. Indices start from zero. A
negative number can be used to access the tuple in
reverse. The output of the print statement is as follows:

>>> print b_tuple[0]


1
>>> print b_tuple[-1]
c
>>>

The Python tuple indices start from 0. Tuples are


immutable.

In step 3, we will look at the most important property of a


tuple called immutability. It is not possible to change the
value of an item in a tuple; step 3 will result in an error
thrown by the interpreter:

Traceback (most recent call last):


File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support
item assignment

This may look restrictive; however, the immutable


feature has immense value from a data science
perspective.

TIP
While building programs for machine learning, in particular during the feature generation
from raw data, creating feature tuples ensures that values cannot be changed by
downstream programs.

As these features reside in a tuple, no downstream


program can accidently change the feature values.

However, we want to point out that a tuple can have a


mutable object as its member, for example, a list. If we
have a tuple as shown in step 4, the third element of the
tuple is a list. Now, let's try to change an element in the
list:

c_tuple[2][0] = 100

We will print the tuple as follows:

print c_tuple

We will get the following output:

(1, 2, [100, 20, 30])

As you can see, the value of the first element in the list is
changed to 100.

In step 5, we concatenated two tuples. Another


interesting way to use tuples is when different modules
are creating different features for a machine learning
Random documents with unrelated
content Scribd suggests to you:
centre of our line; this was defeated, and repeated twice, the armies
constantly mixed actually with each other, and the French always
covering each attack by the most tremendous cannonade you can
possibly imagine. With respect to the particular situation in which my
brigade was placed, it did not suffer much until towards the last
attack; the ground on the left did not admit of the cavalry
advancing, and I, being on the left of all, consequently suffered only
from the cannonade. About six o’clock, however, I learnt that the
cavalry in the centre had suffered dreadfully, and the Prussians
about that time having formed to my left, I took upon myself to
move off from our left, and halted directly to the centre of our line,
where I arrived most opportunely at the instant that Bonaparte was
making his last and most desperate effort. And never did I witness
anything so terrific: the ground actually covered with dead and
dying, cannon shot and shells flying thicker than I ever heard
musquetry.
“In this state of affairs I wheeled my brigade into line close
(within ten yards) in the rear of our infantry, and prepared to charge
the instant they had retreated through my intervals (the three
squadron officers were wounded at this instant). This, however, gave
them confidence, and the brigades that were literally running away
halted on our cheering them and again began fighting. The enemy
on their part began to waver. The Duke observed it, and ordered the
infantry to advance. I immediately wheeled the brigade by half-
squadrons to the right and in column over the dead and dying,
trotted round the right of our infantry, passed the French infantry,
and formed lines of regiments on the first half-squadrons. With the
10th I charged a body of French Cuirassiers and Lancers infinitely
superior to them, and completely routed them. I then went to the
18th, and charged a second body that was supporting a square of
Imperial Guards, and the 18th not only defeated them, but took
fourteen pieces of cannon that had been firing grape at us during
our movement. I then, with the 10th, having reformed them,
charged a square of infantry, Imperial Guards, the men of which we
cut down in the ranks, and here the last shot was fired—from this
moment all was deroute.... I never saw such a day, nor any one
else.”
In confirmation of the last statement Sir Harry Smith, who also
fought under Wellington in this campaign, says “I had never seen
anything to be compared with what I saw,” excepting only “one spot
at New Orleans, and the breach of Badajos.” He adds a description
of the field as he observed it on the following day:
“At Waterloo,” he writes, “the whole field from right to left was a
mass of dead bodies. In one spot, to the right of La Haye Sainte, the
French Cuirassiers were literally piled on each other; many soldiers
not wounded lying under their horses; others, fearfully wounded,
occasionally with their horses struggling upon their wounded bodies.
The sight was sickening, and I had no means or power to assist
them. Imperative duty compelled me to the field of my comrades,
where I had plenty to do to assist many who had been left out all
night; some had been believed to be dead, but the spark of life had
returned. All over the field you saw officers, and as many soldiers as
were permitted to leave the ranks, leaning and weeping over some
dead or dying brother or comrade. The battle was fought on a
Sunday, the 18th June, and I repeated to myself a verse from the
Psalms of that day—91st Psalm, 7th verse: ‘A thousand shall fall at
thy side, and ten thousand at thy right hand, but it shall not come
nigh thee.’ I blessed Almighty God our Duke was spared, and
galloped to my General, whom I found with some breakfast awaiting
my arrival.” In Sir Harry’s opinion “Napoleon fought the battle badly,
his attacks were not simultaneous, but partial and isolated, and
enabled the Duke to repel each by a concentration.”
A fleeting glimpse of the fallen Colossus as he rushes towards
Paris is afforded us by Alexandre Dumas, then staying with his
mother at the posting-house of Villers-Cotterets, about fifty-five
miles from the capital. The novelist had seen the Emperor pass
through the little town before the crushing conflict. He had then
been accompanied by General Letort and Jerome Bonaparte. Says
Dumas:
“At seven o’clock a courier arrived; he was covered with mud, his
horse shook from head to foot, and was ready to drop with fatigue.
He ordered four horses to be ready for a carriage which was
following him, then he leapt on his horse and set off on his journey
again.
“It was in vain we questioned him; he either knew nothing or
would not say anything.
“The four horses were taken out of the stables and harnessed in
readiness for the carriage: a rapidly approaching heavy rumble
announced it was coming, soon we saw it appear round the corner
of the street and draw up at the door.
“The master of the post came forward and stood stupefied. I
took hold of his coat tails and asked: ‘It is he? the Emperor?’
“‘Yes.’
“It was indeed the Emperor, just in the same place and carriage,
with one aide-de-camp near him and one opposite him, as I had
seen him before. But his companions were neither Jerome nor
Letort. Letort was killed, and Jerome was commissioned to rally the
army by Laon.
“It was just the same man, it was just the same pale, sickly,
impassive face, but his head was bent a little more forward on his
chest.
“Was it merely from fatigue, or from grief at having staked the
world and lost it?
“As on the first occasion, he raised his head when he felt the
carriage pull up, and threw exactly vague look around him which
became so penetrating when he fixed it upon a person or scanned
the horizon, those two unknown elements behind which danger
might always lurk.
“‘Where are we?’ he asked.
“‘At Villers-Cotterets, sire.’
“‘Good! eighteen leagues from Paris?’
“‘Yes, sire.’
“‘Go on.’”
In his second abdication, signed on the 22nd June, the Emperor
declared that his public life was finished, and proclaimed his son as
Napoleon II., Emperor of the French. But the child for whom his
father had anticipated so glorious a career in 1811, who had been
born with the mighty title of King of Rome, was never destined to
wear the crown of France. That insignia of royal rank was donned
once more by Louis XVIII.
The mighty conqueror had run his course. He threw himself on
the mercy of the nation to which he had shown no mercy, and which
he had hated with exceeding hatred. Great Britain consigned him to
the island rock of St Helena, far away on the broad bosom of the
Atlantic, and in the well-known picture by the late Sir W. Q.
Orchardson, “Napoleon on the ‘Bellerophon,’” we see Napoleon
taking his final farewell of France. He stands alone, bearing, in place
of the weight of Empire, the almost insupportable burden of
shattered hopes. Gone dynasty and throne and kindred, everything
that was worth while in his complex life, but the Imperial Dignity will
never be discarded. He remains Napoleon the Great. The rigidity of
the mouth and the stern and unbending demeanour tell you that the
will is still unconquered.
Napoleon on Board the “Bellerophon”
By Sir W. Q. Orchardson, R.A.
By permission of the Berlin Photographic Co., London, W.

His “star” had led him far from insignificant Ajaccio and was now
leading him still further. Unknown lad, cadet, lieutenant, general,
emperor, statesman, constructor, destructor, he had been all, and
more. Destiny had now set him a far more difficult task, namely, to
reign over himself. In this he was perhaps less successful than
myriads who have gone down to the grave in silence, and whose
names find no place in the printed page or the scrolls of history. In
lonely St Helena, isolated from other human habitation, spied on by
soldiers of the army which had done so much to bring about his
downfall, but surrounded by a little band of men who refused to
desert him in his last days of trial and despair, he spent the
remainder of a life which had been lived to the full. Sometimes his
old enthusiasm would revive as he reviewed the history of a
campaign, at others he would show the capriciousness of a spoilt
child at the over-conscientious sense of duty displayed by Sir Hudson
Lowe, the Governor of the island. It is perhaps a more dramatic
ending to so marvellous a story than if he had fallen in battle. Many
men have met their death in that way, but there has been but one
Imperial prisoner at St Helena, the exiled monarch whose soul took
its flight on the stormy night of the 5th May 1821.
“The glories of our blood and state
Are shadows, not substantial things;
There is no armour against fate;
Death lays his icy hand on kings;
Sceptre and crown
Must tumble down,
And in the dust be equal made
With the poor crooked scythe and spade.”

“I desire that my ashes may repose on the banks of the Seine, in


the midst of the French people I have loved so well,” wrote Napoleon
in his will, and the nation responded with emotion to the wish of its
great Son. They forgot that he had lavished French treasure and
resources as a spendthrift, that his insane ambitions had brought
them financial and political ruin; they forgave him that he had led
the youth of France to the shambles and had bereaved their homes
of fathers, of husbands, of brothers, of sons. They remembered only
that he had glorified France, and in the midst of beautiful Paris they
raised the most noble Tomb that the genius of modern times has
conceived. It is a sacred place of pilgrimage to every son and
daughter of France, and men and women of other nations pass, a
continual stream, before the massive sarcophagus which—oh, irony
of fate!—was hewn out of a Russian quarry, the memorial tribute of
Czar Nicholas I. to his brother’s mighty antagonist. None who enters
that quiet place fails to bow the head before those ashes, and we,
too, perhaps from afar, may reflect one moment upon the vanity of
human glory and ponder the eternal truth:
“Only the actions of the just
Smell sweet, and blossom in their dust.”

The less deeply shaded portion shows the extent of the French Empire at the height of
Napoleon’s power. The darker part shows its diminished size after 1815.
Index

Abensberg, 224
Acre, 101–2
Ajaccio, 15, 34, 39, 41, 44, 52, 55, 92, 105
Alexander I., 123, 178, 298, 306
Alexandria, 94, 95
Amiens (Treaty), 124, 131
Arcola, 85
Aspern, 236, 237
Augsburg, 148, 220
Austerlitz, 150, 153
Auxonne, 34, 39, 41, 42
Avignon, 59, 233

Bagration, Prince, 175, 272, 273, 278


Barras, 71, 72, 107
Bartenstein, Treaty of, 173
Bastia, 38, 39, 54, 55
Bautzen, Battle of, 298
Beauharnais, Josephine de, 72, 139, 249, 250, 251
Beauharnais, Eugene, 119, 140, 241, 268
Bennigsen, General, 168, 169, 173, 176, 177
Beresina River, 289
Berlin, Decree of, 181, 182
Bernadotte, General, 138, 147, 149, 159, 167, 170
Berthier (War Minister), 75, 80, 104, 107, 111, 115, 154, 210,
223, 304
Bessières, Marshal, 119, 138, 200, 296
Bey, Murad, 96, 99, 100, 103
Blücher, General, 167, 296, 297, 298, 303, 307
Bocognano, 54
Bonaparte, Charles, 17, 20
” Letizia, 16, 33, 59
Bonaparte, Louis, 41, 49, 69, 85, 154, 195, 253, 254
” Jerome, 272, 304
” Joseph, 16, 19, 44, 48, 54, 59, 68, 154, 171, 196,
304
” Lucien, 18, 28, 33, 53, 59, 107, 109, 255
” ” (Archdeacon), 18, 33, 44
” Élise, 51
Borodino, Battle of, 277, 278
Boulogne, 133, 139, 143, 153
Bourrienne, 25, 27, 47, 69, 89, 101, 123, 183, 235
Brienne, 22, 27, 28, 303
Brueys, Admiral, 93, 97, 98
Brunswick, Duke of, 164
Burgos, Battle of, 213

Cagliari, 52
Cairo, 95
Campo Formio, Treaty of, 88
Carteaux, 60, 65
Casa-Bianca, Commodore, 98
Charles, Archduke, 88, 147, 150, 221
Charles IV. (Spain), 194, 195, 196
Cintra, Convention of, 204
Clary, Mlle. Désirée, 68
Copenhagen, 123, 188, 189
Corsica, 15, 33, 34, 38, 39, 44, 66, 69, 202
Coruña, Battle of, 216

Danzig, 175
D’Enghien, Duc, 135
Desaix, General, 93, 99, 117, 119
Dnieper River, 289
Doppet, 60, 62, 65
Dresden, 259, 260, 261, 297, 298, 300
Dugommier, General, 61, 63
Dumouriez, General, 57, 58
Duroc, General, 75, 104, 298

Ebelsberg (Ebersberg), 229


Eckmühl, Battle of, 226
El Arish, 100
Elba, Isle of, 141, 167, 259, 306
Erfurt, 207, 208, 211
Essling, 237
Etruria, 191, 195
Eylau, 170, 173

Ferdinand, Archduke, 148


” Prince Louis, 160
” VII., King (Spain), 194, 195, 196
Flushing, 245
Fox, Charles James, 132, 156
Frederick, William III. (Prussia), 178, 239, 291, 292, 297
Fréjus, 105
Friedland, 175, 176

Geneva, 115, 116


Genoa, 66, 92, 113
Godoy, Manuel, 192, 194, 195
Gross-Görschen (Lützen), Battle of, 296

Hanover, 153, 156


Hohenlinden, Battle of, 121
Hood, Lord, 60, 63

Jaffa, 100
Jemappes, Battle of, 57
Jena, Battle of, 158, 159, 160, 162, 173, 194, 207
John, Archduke, 150
Joubert, General, 88
Junot, General, 61, 75, 87, 183, 193, 202, 203, 204, 268
Junot, Madame, 31

Kellerman, General, 119, 138


Kléber, General, 65, 93, 95, 101, 120
Königsberg, 170, 173, 174, 175, 177, 178, 265, 293
Kray, General, 114
Kulm, Battle of, 300

Landgrafenberg, 162
Lannes, Marshal, 80, 104, 107, 108, 116, 117, 119, 138, 161,
226, 227, 235, 236, 237
Leipzig, Campaign of, 291–301
Leoben, 88
Ligny, Battle of, 307
Lobau (Ile Napoléon), 238, 240, 241
Lodi, 80, 81, 85
Lonato, 84
Louis XVI., 37, 48, 49, 50, 57, 58
Lugo, 215–6
Lunéville, Treaty of, 122, 128, 131
Lützen, Battle of, 296
Lyons, 36, 43

Macdonald, Marshal, 113, 229, 241, 243, 244, 290, 293, 300,
304
Madrid, 199, 213
Malta, 93, 96, 125
Mantua, 84, 87
Marbœuf, 20, 21
Marengo, 117, 120
Marie Antoinette, 47, 49
Marmont, Marshal, 104, 107, 147, 148, 241, 245, 304
Marseilles, 38, 60, 68
Masséna, Marshal, 65, 76, 80, 83, 84, 106, 138, 150, 220, 223,
245
Médola, 84
Milan, 76, 79, 82, 83, 116, 140, 182
Millesimo, 77, 79
Montebello, 117
Montenotte Pass, 76
Moore, Sir John, 202, 204, 212, 215, 217
Moreau, General, 113, 114, 121, 122, 135
Mortier, Marshal, 134, 138, 304
Moscow, 275, 279
Munich, 115
Murat, King of Naples, 83, 93, 104, 107, 138, 160, 167, 195,
198, 295
Naples, 113, 196
Nelson, Admiral, 94, 97, 123, 133, 142, 143, 149, 215
Ney, Marshal, 128, 138, 150, 170, 175, 264, 273, 289, 307
Nice, 64, 74, 77

Oudinot, General, 264, 289, 304

Paoli, Pascal, 16, 36, 39, 52, 53


Paris, Occupation of, 304
Parsdorf (Armistice), 115
Pichegru, M., 23, 135, 136
Piedmont, 76, 78, 127
Pitt, William, 125, 132, 140, 155, 211
Pius VI., Pope, 88
” VII., Pope, 233
Prague, 294
Pressburg, Peace of, 153
Pyramids, Battle of the, 96

Quatre Bras, Battle of, 307

Rapp, General, 150, 235


Ratisbon, 220, 221, 223, 224, 228
Rémusat, Madame de, 39
Rivoli, 86
Robespierre, Augustan, 66, 72
” Maximilien, 66

Salicetti, 54, 64, 67


San Domingo, 129, 130
San Stefano, 52
Sardinia, 52, 93
Savary, General, 232
Savoy, 57, 77
Scharnhorst, General, 292, 298
Schönbrünn, Treaty of, 248, 258
Schwarzenberg, General, 264, 290, 304
Smith, Sir Sidney, 101, 102, 103, 193
Smolensk, 207, 274, 275, 286, 289
Soult, Marshal, 138, 147, 167, 213, 215
Staël, Madame de, 89, 183
St Cloud, 108
Stein, H. F. C. (Prussia), 206, 291, 294
St Helena, 50, 312
Strachan, Sir Richard, 245

Tabor, Mt., 102


Talleyrand, 90, 107, 113, 154, 156, 210, 215
Tilsit, Peace of, 180, 191
Trafalgar, Battle of, 144
Treviso (Armistice), 122
Trinidad, 125
Toulon, 53, 56, 59, 60, 63, 92, 93, 141, 142
Turin, 77, 78, 79
Tyrol, 150, 153, 197, 246

Ulm, 115, 148, 153


University of France, 186

Valence, 30, 42
Victor, Marshal, 87, 289
Vienna, 122, 150, 233
Villeneuve, Admiral, 142, 143, 144
Vilna, 266, 268, 270, 290
Vimiero, 203
Vistula River, 169, 175, 293
Vitebsk, 207, 272, 277

Wagram, 241, 244


Walcheren, Island of, 245, 254
Warsaw, 169, 246
Waterloo, 307, 310
Wellesley, Sir Arthur (Duke of Wellington), 188, 202, 203, 204,
303, 307

York, General, 292, 293

Znaym, 245, 246


Zurich, 106
Transcriber’s Notes
Punctuation, hyphenation, and spelling were made
consistent when a predominant preference was found in
this book; otherwise they were not changed.
Simple typographical errors were corrected;
ambiguous hyphens at the ends of lines were retained.
Text contains several words that sometimes are
hyphenated and sometimes not, such as “battle-field”
and “battlefield”; both forms have been retained here.
Index not checked for proper alphabetization or
correct page references.
*** END OF THE PROJECT GUTENBERG EBOOK THE STORY OF
NAPOLEON ***

Updated editions will replace the previous one—the old editions


will be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States
copyright in these works, so the Foundation (and you!) can copy
and distribute it in the United States without permission and
without paying copyright royalties. Special rules, set forth in the
General Terms of Use part of this license, apply to copying and
distributing Project Gutenberg™ electronic works to protect the
PROJECT GUTENBERG™ concept and trademark. Project
Gutenberg is a registered trademark, and may not be used if
you charge for an eBook, except by following the terms of the
trademark license, including paying royalties for use of the
Project Gutenberg trademark. If you do not charge anything for
copies of this eBook, complying with the trademark license is
very easy. You may use this eBook for nearly any purpose such
as creation of derivative works, reports, performances and
research. Project Gutenberg eBooks may be modified and
printed and given away—you may do practically ANYTHING in
the United States with eBooks not protected by U.S. copyright
law. Redistribution is subject to the trademark license, especially
commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the


free distribution of electronic works, by using or distributing this
work (or any other work associated in any way with the phrase
“Project Gutenberg”), you agree to comply with all the terms of
the Full Project Gutenberg™ License available with this file or
online at www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand,
agree to and accept all the terms of this license and intellectual
property (trademark/copyright) agreement. If you do not agree
to abide by all the terms of this agreement, you must cease
using and return or destroy all copies of Project Gutenberg™
electronic works in your possession. If you paid a fee for
obtaining a copy of or access to a Project Gutenberg™
electronic work and you do not agree to be bound by the terms
of this agreement, you may obtain a refund from the person or
entity to whom you paid the fee as set forth in paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only


be used on or associated in any way with an electronic work by
people who agree to be bound by the terms of this agreement.
There are a few things that you can do with most Project
Gutenberg™ electronic works even without complying with the
full terms of this agreement. See paragraph 1.C below. There
are a lot of things you can do with Project Gutenberg™
electronic works if you follow the terms of this agreement and
help preserve free future access to Project Gutenberg™
electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright
law in the United States and you are located in the United
States, we do not claim a right to prevent you from copying,
distributing, performing, displaying or creating derivative works
based on the work as long as all references to Project
Gutenberg are removed. Of course, we hope that you will
support the Project Gutenberg™ mission of promoting free
access to electronic works by freely sharing Project Gutenberg™
works in compliance with the terms of this agreement for
keeping the Project Gutenberg™ name associated with the
work. You can easily comply with the terms of this agreement
by keeping this work in the same format with its attached full
Project Gutenberg™ License when you share it without charge
with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.

1.E. Unless you have removed all references to Project


Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project
Gutenberg™ work (any work on which the phrase “Project
Gutenberg” appears, or with which the phrase “Project
Gutenberg” is associated) is accessed, displayed, performed,
viewed, copied or distributed:

This eBook is for the use of anyone anywhere in the United


States and most other parts of the world at no cost and
with almost no restrictions whatsoever. You may copy it,
give it away or re-use it under the terms of the Project
Gutenberg License included with this eBook or online at
www.gutenberg.org. If you are not located in the United
States, you will have to check the laws of the country
where you are located before using this eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is


derived from texts not protected by U.S. copyright law (does not
contain a notice indicating that it is posted with permission of
the copyright holder), the work can be copied and distributed to
anyone in the United States without paying any fees or charges.
If you are redistributing or providing access to a work with the
phrase “Project Gutenberg” associated with or appearing on the
work, you must comply either with the requirements of
paragraphs 1.E.1 through 1.E.7 or obtain permission for the use
of the work and the Project Gutenberg™ trademark as set forth
in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is


posted with the permission of the copyright holder, your use and
distribution must comply with both paragraphs 1.E.1 through
1.E.7 and any additional terms imposed by the copyright holder.
Additional terms will be linked to the Project Gutenberg™
License for all works posted with the permission of the copyright
holder found at the beginning of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files
containing a part of this work or any other work associated with
Project Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute


this electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the
Project Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™
works unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or


providing access to or distributing Project Gutenberg™
electronic works provided that:

• You pay a royalty fee of 20% of the gross profits you


derive from the use of Project Gutenberg™ works
calculated using the method you already use to calculate
your applicable taxes. The fee is owed to the owner of
the Project Gutenberg™ trademark, but he has agreed to
donate royalties under this paragraph to the Project
Gutenberg Literary Archive Foundation. Royalty payments
must be paid within 60 days following each date on
which you prepare (or are legally required to prepare)
your periodic tax returns. Royalty payments should be
clearly marked as such and sent to the Project Gutenberg
Literary Archive Foundation at the address specified in
Section 4, “Information about donations to the Project
Gutenberg Literary Archive Foundation.”

• You provide a full refund of any money paid by a user


who notifies you in writing (or by e-mail) within 30 days
of receipt that s/he does not agree to the terms of the
full Project Gutenberg™ License. You must require such a
user to return or destroy all copies of the works
possessed in a physical medium and discontinue all use
of and all access to other copies of Project Gutenberg™
works.

• You provide, in accordance with paragraph 1.F.3, a full


refund of any money paid for a work or a replacement
copy, if a defect in the electronic work is discovered and
reported to you within 90 days of receipt of the work.

• You comply with all other terms of this agreement for


free distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project


Gutenberg™ electronic work or group of works on different
terms than are set forth in this agreement, you must obtain
permission in writing from the Project Gutenberg Literary
Archive Foundation, the manager of the Project Gutenberg™
trademark. Contact the Foundation as set forth in Section 3
below.

1.F.
1.F.1. Project Gutenberg volunteers and employees expend
considerable effort to identify, do copyright research on,
transcribe and proofread works not protected by U.S. copyright
law in creating the Project Gutenberg™ collection. Despite these
efforts, Project Gutenberg™ electronic works, and the medium
on which they may be stored, may contain “Defects,” such as,
but not limited to, incomplete, inaccurate or corrupt data,
transcription errors, a copyright or other intellectual property
infringement, a defective or damaged disk or other medium, a
computer virus, or computer codes that damage or cannot be
read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except


for the “Right of Replacement or Refund” described in
paragraph 1.F.3, the Project Gutenberg Literary Archive
Foundation, the owner of the Project Gutenberg™ trademark,
and any other party distributing a Project Gutenberg™ electronic
work under this agreement, disclaim all liability to you for
damages, costs and expenses, including legal fees. YOU AGREE
THAT YOU HAVE NO REMEDIES FOR NEGLIGENCE, STRICT
LIABILITY, BREACH OF WARRANTY OR BREACH OF CONTRACT
EXCEPT THOSE PROVIDED IN PARAGRAPH 1.F.3. YOU AGREE
THAT THE FOUNDATION, THE TRADEMARK OWNER, AND ANY
DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE
TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL,
PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE
NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of
receiving it, you can receive a refund of the money (if any) you
paid for it by sending a written explanation to the person you
received the work from. If you received the work on a physical
medium, you must return the medium with your written
explanation. The person or entity that provided you with the
defective work may elect to provide a replacement copy in lieu
of a refund. If you received the work electronically, the person
or entity providing it to you may choose to give you a second
opportunity to receive the work electronically in lieu of a refund.
If the second copy is also defective, you may demand a refund
in writing without further opportunities to fix the problem.

1.F.4. Except for the limited right of replacement or refund set


forth in paragraph 1.F.3, this work is provided to you ‘AS-IS’,
WITH NO OTHER WARRANTIES OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of
damages. If any disclaimer or limitation set forth in this
agreement violates the law of the state applicable to this
agreement, the agreement shall be interpreted to make the
maximum disclaimer or limitation permitted by the applicable
state law. The invalidity or unenforceability of any provision of
this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the


Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and
distribution of Project Gutenberg™ electronic works, harmless
from all liability, costs and expenses, including legal fees, that
arise directly or indirectly from any of the following which you
do or cause to occur: (a) distribution of this or any Project
Gutenberg™ work, (b) alteration, modification, or additions or
deletions to any Project Gutenberg™ work, and (c) any Defect
you cause.
Section 2. Information about the Mission
of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new
computers. It exists because of the efforts of hundreds of
volunteers and donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project
Gutenberg™’s goals and ensuring that the Project Gutenberg™
collection will remain freely available for generations to come. In
2001, the Project Gutenberg Literary Archive Foundation was
created to provide a secure and permanent future for Project
Gutenberg™ and future generations. To learn more about the
Project Gutenberg Literary Archive Foundation and how your
efforts and donations can help, see Sections 3 and 4 and the
Foundation information page at www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-
profit 501(c)(3) educational corporation organized under the
laws of the state of Mississippi and granted tax exempt status
by the Internal Revenue Service. The Foundation’s EIN or
federal tax identification number is 64-6221541. Contributions
to the Project Gutenberg Literary Archive Foundation are tax
deductible to the full extent permitted by U.S. federal laws and
your state’s laws.

The Foundation’s business office is located at 809 North 1500


West, Salt Lake City, UT 84116, (801) 596-1887. Email contact
links and up to date contact information can be found at the
Foundation’s website and official page at
www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission
of increasing the number of public domain and licensed works
that can be freely distributed in machine-readable form
accessible by the widest array of equipment including outdated
equipment. Many small donations ($1 to $5,000) are particularly
important to maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws


regulating charities and charitable donations in all 50 states of
the United States. Compliance requirements are not uniform
and it takes a considerable effort, much paperwork and many
fees to meet and keep up with these requirements. We do not
solicit donations in locations where we have not received written
confirmation of compliance. To SEND DONATIONS or determine
the status of compliance for any particular state visit
www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states


where we have not met the solicitation requirements, we know
of no prohibition against accepting unsolicited donations from
donors in such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot


make any statements concerning tax treatment of donations
received from outside the United States. U.S. laws alone swamp
our small staff.
Please check the Project Gutenberg web pages for current
donation methods and addresses. Donations are accepted in a
number of other ways including checks, online payments and
credit card donations. To donate, please visit:
www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could
be freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose
network of volunteer support.

Project Gutenberg™ eBooks are often created from several


printed editions, all of which are confirmed as not protected by
copyright in the U.S. unless a copyright notice is included. Thus,
we do not necessarily keep eBooks in compliance with any
particular paper edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg
Literary Archive Foundation, how to help produce our new
eBooks, and how to subscribe to our email newsletter to hear
about new eBooks.
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

ebookbell.com

You might also like