An Introduction To Statistics With Python With Applications In The Life Sciences 2nd 2nd Edition Thomas Haslwanter instant download
An Introduction To Statistics With Python With Applications In The Life Sciences 2nd 2nd Edition Thomas Haslwanter instant download
https://ebookbell.com/product/an-introduction-to-statistics-with-
python-with-applications-in-the-life-sciences-2nd-2nd-edition-
thomas-haslwanter-47278200
https://ebookbell.com/product/an-introduction-to-statistics-with-
python-with-applications-in-the-life-sciences-thomas-
haslwanter-49052704
https://ebookbell.com/product/an-introduction-to-statistical-learning-
with-applications-in-python-1st-gareth-james-50703060
https://ebookbell.com/product/an-introduction-to-statistics-with-
python-thomas-haslwanter-42679398
https://ebookbell.com/product/an-introduction-to-algebraic-statistics-
with-tensors-cristiano-bocci-10519584
An Introduction To Algebraic Statistics With Tensors Cristiano Bocci
https://ebookbell.com/product/an-introduction-to-algebraic-statistics-
with-tensors-cristiano-bocci-10519594
https://ebookbell.com/product/an-introduction-to-econometric-theory-
measuretheoretic-probability-and-statistics-with-applications-to-
economics-a-ronald-gallant-50731408
https://ebookbell.com/product/biostatistics-with-r-an-introduction-to-
statistics-through-biological-data-1st-edition-babak-shahbaba-
auth-2445754
https://ebookbell.com/product/simulation-and-statistics-with-excel-an-
introduction-to-business-students-ibarra-luis-fernando-56337800
https://ebookbell.com/product/elements-of-probability-and-statistics-
an-introduction-to-probability-with-de-finettis-approach-and-to-
bayesian-statistics-1st-edition-francesca-biagini-5200136
Statistics and Computing
Thomas Haslwanter
An Introduction
to Statistics
with Python
With Applications in the Life Sciences
Second Edition
Statistics and Computing
Series Editor
Wolfgang Karl Härdle, Humboldt-Universität zu Berlin, Berlin, Germany
Statistics and Computing (SC) includes monographs and advanced texts on statistical
computing and statistical packages.
An Introduction to Statistics
with Python
With Applications in the Life Sciences
Second Edition
Thomas Haslwanter
School of Medical Engineering and Applied
Social Sciences
University of Applied Sciences Upper Austria
Linz, Austria
This Springer imprint is published by the registered company Springer Nature Switzerland AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
To my two-, three-, and four-legged
household companions: Jean, Felix, and his
sister Jessica: Thank you so much for all the
support you have provided over the years!
Preface
In the data analysis for my own research work, I was often slowed down by two
things: (1) I did not know enough statistics, and (2) the books available would provide
a theoretical background, but no real practical help. The book you are holding in your
hands (or on your tablet or laptop) is intended to be the book that will solve this very
problem. It is designed to provide enough basic understanding so that you know what
you are doing, and it should equip you with the tools you need. I believe that the
Python solutions provided in this book for the most basic statistical problems address
at least 90% of the problems that most physicists, biologists, and medical doctors
encounter in their work. So if you are the typical graduate student working on your
degree, or a medical researcher analyzing your latest experiments, chances are that
you will find the tools you require here—explanation and source-code included.
This is the reason I have focused on statistical basics and hypothesis tests in
this book, and refer only briefly to other statistical approaches. I am well aware
that most of the tests presented in this book can also be carried out using statistical
modeling. But in many cases, this is not the methodology used in many life science
journals. Advanced statistical analysis goes beyond the scope of this book, and—to
be frank—exceeds my own knowledge of statistics.
My motivation for providing the solutions in Python is based on two considera-
tions. One is that I would like them to be available to everyone. While commercial
solutions like Matlab, SPSS, Minitab etc. offer powerful tools, most can only use
them legally in an academic setting. In contrast, Python is completely free (as in free
beer is often heard in the Python community). The second reason is that Python is the
most beautiful coding language that I have yet encountered; and around 2010 Python
and its documentation matured to the point where one can use it without being an
serious coder. Together, this book, Python, and the tools that the Python ecosystem
offers today provide a beautiful, free package that covers all the statistics that most
researchers will need in their lifetime.
vii
viii Preface
Since the publication of the first edition, Python has continuously gained popularity
and become firmly established as one of the foremost programming languages for
statistical data analysis. All the core packages have matured. And thanks to the
stunning development of Jupyter as an interactive programming environment, Python
has become even more accessible for people with little programming background.
To reflect these developments, and to incorporate the suggestions I have received for
improving the presentation of the material, Springer has given me the opportunity to
bring out a new edition of Introduction to Statistics with Python.
Compared to the first edition, the following changes have been made:
• The package pandas and its DataFrames have become an integral part of
scientific Python, as has the Jupyter framework for interactive data environ-
ments. Correspondingly, a bigger amount of space has been dedicated to their
introduction.
• A new package, pingouin, is promising a simplified and more powerful inter-
face for many common statistics function. This package is introduced, and many
application examples have been added.
• The visualization of data has been expanded, including the preparation of
publication-ready graphics.
• The design of experiments and power analyses are discussed in more detail.
• A new section has been added on the confidence intervals of frequently used
statistical parameters.
• A new chapter has been added on finding patterns in data, including an introduction
to the correlation coefficient, cross- and autocorrelation. For an application of
these concepts, a short introduction is given to time series analysis.
As for the first edition, all examples and solutions from this book are again avail-
able online. This includes code samples and example programs, Jupyter Notebooks
with additional or extended information, as well as the data and Python code used to
generate most of the figures. They can be downloaded from https://github.com/tho
mas-haslwanter/statsintro-python-2e.
I hope this book will help you with the statistical analysis of your data, and
convey some of the often really simple ideas behind the sometimes awkwardly named
statistical analysis procedures.
much all at once. However, solutions provided to the exercises at the end of most
chapters should help you to get up to speed with Python.
• you are not a statistics expert: If you have advanced statistics experience, the
online help in Python and the Python packages may be sufficient to allow you to
do most of your data analysis right away. This book may still help you to get started
with Python. However, the book concentrates on the basic ideas of statistics and
on hypothesis tests, and only the last part introduces linear regression modeling
and Bayesian statistics.
This book is designed to give you all (or at least most of) the tools that you
will need for statistical data analysis. I attempt to provide the background you need
to understand what you are doing. I do not prove any theorems, and do not apply
mathematics unless necessary. For all tests, a working Python program is provided.
In principle, you just have to define your problem, select the corresponding program,
and adapt it to your needs. This should allow you to get going quickly, even if you
have little Python experience. This is also the reason why I have not provided the
software as one single Python package; I expect that you will have to tailor each
program to your specific setup (data format, etc.).
This book is organized into three parts
Part I gives an introduction to Python: how to set it up, simple programs to get
started, and tips on how to avoid some common mistakes. It also shows how to
read data from different sources into Python, and how to visualize statistical data.
Part II provides an introduction to statistical analysis; on how to design a study,
power analysis, and how best to analyze data; probability distributions; and an
overview of the most important hypothesis tests. Even though modern statistics
is firmly based in statistical modeling, hypothesis tests still seem to dominate the
life sciences. For each test, a Python program is provided that shows how the test
can be implemented.
Part III provides an introduction to correlation and regression analysis, time
series analysis, and statistical modeling, and a look at advanced statistical analysis
procedures. I have also included tests on discrete data in this section, such as
logistic regression, as they utilize “generalized linear models” which I regard
as advanced. This part ends with a presentation of the basic ideas of Bayesian
statistics.
To achieve all those goals as quickly as possible, the Appendix A of the book
provides hints on how to efficiently develop correct and working code. This should
get you to the point where you can get things done quickly.
x Preface
Acknowledgments
Python is built on the contributions from the user community, and some of the sections
in this book are based on some of the excellent information available on the web.
(Permission has been granted by the authors to reprint their contributions here.)
I especially want to thank the following people:
• Christiane Takacs helped me enormously by polishing the introductory statistics
sections.
• Connor Johnson wrote a very nice blog explaining the results of the statsmodels
OLS command, which provided the basis for the section on Statistical Models.
• Cam Davidson Pilon wrote the excellent open-source e-book Probabilistic-
Programming-and-Bayesian-Methods-for-Hackers. From there, I took the
example of the Challenger disaster to demonstrate Bayesian statistics.
• Fabian Pedregosa’s blog on ordinal logistic regression allowed me to include this
topic, which otherwise would be admittedly beyond my own skills.
I also want to thank Springer Publishing for the chance to bring out the second
edition of this book, and to base the three introductory chapters (Python, Data Import,
and Data Display) to a significant part on the corresponding chapters of my book
Hands-on Signal Analysis with Python.
If you have a suggestion or correction, please send an email to my work address
[email protected]. If I make a change based on your feedback, I will add
you to the list of contributors unless advised otherwise. If you include at least part
of the sentence the error appears in, that makes it easy for me to search. Page and
section numbers are fine, too, but not as easy to work with. Thanks!
xi
xii Contents
xv
xvi Abbreviations
The first part of the book presents an introduction to statistics based on Python. It
is impossible to cover the whole language in thirty or forty pages, so if you are
a beginner, please see one of the excellent Python introductions available on the
Internet for details. Links are given below. This part is a kick-start for Python; it
shows how to install Python under Windows, Linux, or MacOS, and walks step-by-
step through documented programming examples. Tips are included to help avoid
some of the problems frequently encountered while learning Python.
Because most of the data for statistical analysis are commonly obtained from text
files, Excel files, or data preprocessed by Matlab, the third chapter presents simple
ways to import these types of data into Python.
The last chapter of Part I illustrates various ways of visualizing data in Python.
Since the flexibility of Python for interactive data analysis has led to a certain com-
plexity that can frustrate new Python programmers, code samples for various types
of interactive plots should help future Pythonistas to avoid these problems.
Chapter 1
Introduction
Every day, we are confronted with situations with uncertain outcomes, and must
make decisions based on incomplete data: “Should I run for the bus? Which stock
should I buy? Which man should I marry? Should I take this medication? Should
I have my children vaccinated?” Some of these questions are beyond the realm
of statistics (“Which person should I marry?”), because they involve too many
unknown variables. But in many situations, statistics can help extract maximum
knowledge from information given, and clearly spell out what we know and what
we don’t know. For example, it can turn a vague statement like “This medication
may cause nausea,” or “You could die if you don’t take this medication” into a spe-
cific statement like “Three patients in one thousand experience nausea when taking
this medication,” or “If you don’t take this medication, there is a 95% chance that
you will die.”
Without statistics, the interpretation of data can quickly become massively flawed.
Take, for example, the estimated number of German tanks produced during World
War II, also known as the “German Tank Problem.” The estimate of the number of
German tanks produced per month from standard intelligence data was 1550; how-
ever, the statistical estimate based on the number of tanks observed was 327, which
was very close to the actual production number of 342 (http://en.wikipedia.org/wiki/
German_tank_problem).
Similarly, using the wrong tests can also lead to erroneous results.
• http://www.statsref.com/
• http://www.vassarstats.net/
• http://www.biostathandbook.com/
• http://onlinestatbook.com/2/index.html
• http://www.itl.nist.gov/div898/handbook/index.htm.
1.2 Conventions
All the examples and solutions shown in this book are available online. This
includes code samples and example programs, Jupyter Notebooks with additional
or extended information, as well as the data and Python code used to generate many
of the figures. They can be downloaded from GitHub
https://github.com/thomas-haslwanter/statsintro-python-2e
which is organized in folders:
Python is free, consistently and completely object oriented, and has a large num-
ber of (free) scientific toolboxes (e.g., http://www.scipy.org/). It is used by Google,
NASA, and many others. Information on Python can be found under http://www.
python.org/. If you want to use Python for scientific applications, currently the best
way to get started is with a Python distribution, either WinPython, or Anaconda
from Continuum Analytics. These distributions contain the complete scientific and
engineering development software for numerical computations, data analysis, and
data visualization based on Python. They also come with Qt graphical user inter-
faces, and the interactive scientific/development environment Spyder. If you already
have experience with Matlab, the article NumPy for Matlab Users (https://numpy.
org/devdocs/user/numpy-for-matlab-users.html) provides an overview of the simi-
larities and differences between the two languages.
Python is a very-high-level dynamic object-oriented programming language
(Fig. 2.1). It is designed to be easy to program and easy to read. It was started
in 1980, and has since gained immense popularity in a broad range of fields from
web development, system administration, and in science and engineering. Python is
open source, and has become one of the most successful programming languages.
There are three reasons why I have switched from other programming languages to
Python:
1. It is the most elegant programming language that I know.
2. It is free.
3. It is powerful.
The Python core distribution contains only the essential features of a general pro-
gramming language. Python itself is an interpretative programming language, with
no optimization for working with vectors or matrices, or for producing plots. Pack-
ages which extend the abilities of Python must be loaded explicitly. The most impor-
tant packages for scientific applications are numpy, which makes working with vec-
tors and matrices fast and efficient, and matplotlib, which is the most common
package used for producing graphical output. SciPy contains important scientific
algorithms. And pandas has become widely adopted for statistical data analysis. It
provides DataFrames which are labeled, 2D data structures, making work with data
more flexible and intuitive. Python can be used with different front-ends (Fig. 2.2):
from the command line or terminal; interactively, using Jupyter and IPython; and
from “Integrated Development Environments” (IDEs).
IPython provides the tools for interactive data analysis, and Jupyter provides the
different frontends for IPython. IPython lets you quickly display graphs and change
directories, explore the workspace, provides a command history, etc.
Fig. 2.2 Python can be used from different front-ends: from the command line (left), interactively
(center), and from an IDE (right)
2.1 Getting Started 9
Fig. 2.3 The structure of the most important Python packages for statistics
Figure 2.3 shows the modular structure of the most important Python packages
that are used in this book.
To facilitate the use of Python, so-called Python distributions collect matching
versions of the most important packages, and I strongly(!) recommend using one
of these distributions when getting started. Otherwise, one can easily become over-
whelmed by the huge number of Python packages available. My favorite Python
distributions are
• WinPython recommended for Windows users. At the time of writing, the latest
version was 3.10.5
https://winpython.github.io/
• Anaconda by Continuum, for Windows, Mac, and Linux. The latest Anaconda
version at the time of writing was 2020.11, with Python 3.9.
https://www.anaconda.com/products/individual
All of these packages come with the WinPython and Anaconda distributions.
Additional packages, which may be required by individual applications, can easily
be installed using pip or conda.
a) PyPI—The Python Package Index
The Python Package Index (PyPI) (https://pypi.org/) is a repository of software for
the Python programming language and contains more than 390’000 projects!
Packages from PyPI can be installed easily from the Windows command shell
(cmd) or the Linux terminal with
pip install <package>
To get a list of all the Python packages installed on your computer, type
pip list
Anaconda uses conda, a more powerful installation manager. But pip also works
with Anaconda.
While Python and the required packages can be installed manually, it is typically
much easier to start out with a complete Python distribution.
a) Under Windows
Neither WinPython nor Anaconda require administrator rights for installation.
WinPython In the following, I assume that <WinPythonDir> is the installation
directory for WinPython.
Tip: Do NOT install WinPython into the Windows program directory (typically
C:\Program Files or C:\Program Files (x86)), because this can lead
to permission problems during the execution of WinPython.
• Download WinPython from https://winpython.github.io/.
• Run the downloaded .exe-file, and install WinPython into the
<WinPythonDir> of your choice. (On my own system, I place all programs
that do not modify the Windows Registry, such as WinPython, vim, and ffmpeg,
into a folder C:\Programs.)
• After the installation, make a change to your Windows Environment, by typing
Win -> env -> Edit environment variables for your
account (Note that this is different from the system environment!):
2.1 Getting Started 11
b) Under Mac OS X
• Go to https://www.anaconda.com/distribution/.
• Choose the Mac installer (make sure you select the Mac OS X Python 3.x Graph-
ical Installer), and follow the instructions listed beside this button.
• After the installation: in the Anaconda Launcher, click update (besides the Apps),
in order to ensure that you are running the latest version.
After the installation, the Anaconda icon should appear on the desktop. No admin
password is required. This downloaded version of Anaconda includes the Jupyter
Notebook, Jupyter Qt console, and the IDE Spyder.
To see which packages (e.g., numpy, scipy, matplotlib, and pandas) are featured
in your installation, look up the Anaconda Package List for your Python version. For
example, the Python-installer may not include seaborn. To add an additional pack-
age, e.g., seaborn, open the terminal, and enter pip install seaborn.
If you have not used R previously, you can safely skip this section. However, if you
are already an avid R user, the following adjustments will allow you to also harness
the power of R from within Python, using the package rpy2.
a) Under Windows
Also, R does not require administrator rights for installation. You can download
the latest version (at the time of writing R 4.1.0) from http://cran.r-project.org/, and
install it into the <RDir> installation directory of your choice.
• After the installation of R, add the following two variables to your Windows Envi-
ronment, by typing
Win -> env -> Edit environment variables for your account:
– R_HOME=<RDir> R-4.1.0
– R_USER=<YourLoginName>
The first entry is required for rpy2. The last entry is not really necessary, just
better style.
with Anaconda While WinPython comes with rpy2 installed, Anaconda comes
without rpy2. So after the installation of Anaconda and R, you should install rpy2
with
• conda install -c conda-forge rpy2.
b) Under Linux
• After the installation of Anaconda, install R and rpy2 with
conda install -c conda-forge rpy2.
2.1 Getting Started 13
• Python Scientific Lecture Notes If you don’t read anything else, read this!
http://scipy-lectures.org/
• NumPy for Matlab Users Start here if you have Matlab experience
https://numpy.org/doc/stable/user/numpy-for-matlab-users.html
also check
http://mathesaurus.sourceforge.net/matlab-numpy.html
• Lectures on scientific computing with Python Great IPython notebooks, from JR
Johansson.
https://github.com/jrjohansson/scientific-python-lectures
• The Python tutorial The official introduction
http://docs.python.org/3/tutorial
• 7 Steps to Python My own efforts to smoothen the first steps into Python
https://work.thaslwanter.at/py_intro/
When running into a problem while developing a new piece of code, most of
the time I just google; thereby, I stick primarily to the official Python documenta-
tion pages and to http://stackoverflow.com. Also, I have found Python user groups
surprisingly active and helpful!
a) Hello World
Python Shell The simplest way to start Python is to type python on the command
line. (When I say command line, I refer in Windows to the command shell started
with cmd, and in Linux or Mac OS X to the terminal.) Then you can already
start to execute Python commands, e.g., the command to print “Hello World” to the
screen: print('Hello World'). On my Windows computer, this results in
Python 3.8.9 (tags/v3.8.9:0a7dcbd, May 3 2021, 17:27:52)...
Type "help", "copyright", "credits" or "license" for more...
>>> print('Hello World')
Hello World
>>>
14 2 Python
However, most of the time it is more recommendable to start with the IPython/
Jupyter Qt console described in more detail in Sect. 2.3. For example, the Jupyter Qt
console is an interactive programming environment which offers a number of advan-
tages. When you type print( in the Qt console, you immediately see information
about the possible input arguments for the command print.
Python Modules are files with the extension .py, and are used to store Python
commands in a file for later use. Let us create a new file with the name
helloWorld.py, which contains the single line
print('Hello World')
On these two systems, you also have to make the file executable, by typing
chmod +x helloWorld.py
3 def squared(x=10):
4 return x**2
5
6 for ii in range(6):
7 print(ii, squared(ii))
8
9 print(squared())
3–4 These two lines define the function squared, which takes the variable x as
input, and returns the square (x**2) of this variable. If the function is called
with no input, x is by default set to 10. This notation makes it very simple to
define default values for function inputs.
Note: The range of the function is defined by the indentation! This is a fea-
ture loved by many Python programmers, but often found a bit confusing by
newcomers. Here, the last indented line is line 4, which ends the function
definition.
6–7 Here, the program loops over the first 6 numbers. Also, the range of the for-
loop is defined by the indentation of the code.
In line 7, each number and its corresponding square are printed to the output.
9 This command is not indented, and therefore is executed after the for-loop
has ended. It tests if the function call with “()”, which uses the default param-
eter for x, also works, and prints the result.
Notes:
• Since Python starts at 0, the loop in line 6 includes the six numbers from 0 to 5.
• In contrast to some other languages, Python distinguishes the syntax for function
calls from the syntax for addressing elements of an array, etc.: function calls, as
in line 7, are indicated with round brackets (...); and individual elements of
arrays or vectors are addressed by square brackets [...].
Compared to the simple example above, real-world applications involve not only
individual numbers but also vectors and matrices. These, together with the most
important Python data- and file-structures, will be described in this section.
Python offers a number of powerful data structures, and it pays off to make yourself
familiar with them. The most common ones are
For simple programs, you will mainly work with lists and arrays. Dictionaries are
used to group related information together. And tuples are used primarily to return
multiple parameters from functions.
In the following, we will use for Python code the input/output formatting of
IPython which will be presented in Sect. 2.3.
List [] Lists are typically used to collect items of the same type (numbers,
strings, ...). They are “mutable”, i.e., their elements can be modified.
Note that “+” concatenates lists.
In [1]: myList = ['abc', 'def', 'ghij']
In [2]: myList.append('klm')
In [3]: myList
Out[3]: ['abc', 'def', 'ghij', 'klm']
Array [] vectors and matrices, for numerical data manipulation. They are defined
in numpy. Note that vectors and 1D arrays are different: vectors CANNOT be
transposed! With arrays, “+” adds the corresponding elements; and the array-
method .dot performs a scalar multiplication. (Since Python 3.5, scalar multi-
plications can also be performed with the operator “@”.)
In [7]: import numpy as np
...: myArray2 = np.array(myList2)
...: myArray3 = np.array(myList3)
In [9]: myArray2.dot(myArray3)
Out[9]: 32
In [16]: myDict['info']
Out[16]: 'some information'
In [17]: myDict.keys()
Out[17]: dict_keys(['one', 'info', 'two'])
DataFrame Data structure optimized for working with named, statistical data. It
is defined in pandas. (See Sect. 2.2.4.)
The rules for addressing individual elements in Python lists, tuples, or numpy arrays
have been nicely summarized by Greg Hewgill on stackoverflow1 :
a[start:end] # items start through end-1
a[start:] # items start through the rest of the array
a[:end] # items from the beginning through end-1
a[:] # a copy of the whole array
There is also the step value, which can be used with any of the above:
a[start:end:step] # start up to end, by step
The key points to remember are that indexing starts at 0, not at 1; and the :end
value represents the first value that is not in the selected slice. So, the difference
end - start is the number of elements selected (if step is 1, the default).
start or end may be a negative number. In that case the count goes from the end
of the array instead of the beginning. So
a[-1] # last item in the array
a[-2:] # last two items in the array
a[:-2] # everything except the last two items
As a result, a[:5] gives you the first five elements (Hello in Fig. 2.4), and a[-5:]
the last five elements (World).
1 http://stackoverflow.com/questions/509211/explain-pythons-slice-notation.
18 2 Python
Fig. 2.4 The expressions in the top row indicate possible notations to select elements of a list or
an array. Indexing starts at 0, and slicing does not include the last value
numpy is the Python module that makes working with numbers efficient. It is com-
monly imported with
import numpy as np
np.zeros generates numpy arrays containing zeros. Note that it takes only one(!)
input. If you want to generate a matrix of zeroes, this input has to be a tuple or a
list, containing the number of rows/columns!
In [1]: import numpy as np
In [2]: np.zeros(3)
# by default numpy-functions generate 1D-vectors
Out[2]: array([ 0., 0., 0.])
In [7]: xLow
Out[7]: array([ 0., 0.5, 1., 1.5, 2., 2.5])
In [8]: xHigh
Out[8]: array([ 3., 3.5, 4., 4.5])
np.array generates a numpy array from given numerical data, and is a convenient
notation to enter small matrices
In [10]: np.array([[1,2], [3,4]])
Out[10]: array([ [1, 2],
[3, 4] ])
There are a few points that are peculiar to Python, and that are worth noting:
matrices are simply “lists of lists”. Therefore, the first element of a matrix gives
you the first row, the second element the second row, etc.:
In [11]: Amat = np.array([ [1, 2],
[3, 4] ])
In [12]: Amat[0]
Out[12]: array([1, 2])
Warning: A vector is not the same as a 1D matrix! This is one of the few features
of Python that is not intuitive (at least to me), and can lead to mistakes that are
hard to find. For example, vectors cannot be transposed, but matrices can.
In [13]: x = np.arange(3)
In [15]: x.T == x
Out[15]: array([ True, True, True])
# This indicates that a vector stays a vector, and that
# the transposition with ''.T'' has no effect on
# its shape
np.r_ Useful command to quickly construct small row vectors. But I only use
it to try things out quickly. In my programs, I prefer the clearer but equivalent
np.array([...])
20 2 Python
In [17]: np.r_[1,2,3]
Out[17]: array([1, 2, 3], dtype=int32)
np.c_ Useful command to quickly build up small column vectors. Note that
column-vectors can also be generated with the command np.newaxis:
In [18]: np.c_[[1.5,2,3]] # note the double brackets!
Out[18]: array([[1.5],
[2. ],
[3. ]])
In [21]: x
Out[21]: array([0, 1, 2, 3, 4])
In [22]: x.T
Out[22]: array([0, 1, 2, 3, 4]) # no effect on 1D-vectors
In [24]: x_2d.T
Out[24]: array([[0],
[1],
[2],
[3],
[4]])
In [26]: y = np.arange(3,6)
functions that facilitate data input, data organization, and data manipulation. pandas
is commonly imported with
import pandas as pd
The official pandas documentation contains a very good “Getting started” section:
https://pandas.pydata.org/docs/getting_started/.
a) Basic Syntax of DataFrames
Especially in statistical data analysis (read “data science”), panelled data structures
(→ “pandas”) have turned out to be immensely useful. To handle such labeled data
in Python, pandas introduces so-called “DataFrame” objects. A DataFrame is a 2D
labeled data structure with columns of potentially different types. You can think of
it as a spreadsheet or SQL table (see Fig. 2.5). DataFrames are the most commonly
used pandas objects.
For statistical analysis, pandas becomes really powerful when combined with the
package statsmodels (https://www.statsmodels.org/).
pandas DataFrames can have some distinct advantages over numpy arrays:
• A numpy array requires homogeneous data. In contrast, with a pandas
DataFrame you can have a different data type (float, int, string, datetime, etc.)
in each column (Fig. 2.6).
• pandas has built-in functionality for a lot of common data-processing applica-
tions: for example, easy grouping by syntax, easy joins (which are also really
efficient in pandas), and rolling windows.
• DataFrames, where the data can be addressed with column names, can help a lot
in keeping track of your data.
22 2 Python
Fig. 2.6 Demonstration of some features of pandas DataFrames in JupyterLab (see Sect. 2.3.2).
Unlike np.array, a pd.DataFrame can be used to combine different data types (here, strings,
floats, integers, and dates: commands 2–4). It also can work efficiently with date and time data
(commands 2 and 6, also note the output of “6” in the “Output View” on the right. This is a new
feature of JupyterLab)
In addition, pandas has excellent tools for data input and output.
Let me start with a specific example, by creating a DataFrame with three columns
called “Time”, “x”, and “y”:
import numpy as np
import pandas as pd
In pandas, rows are addressed through indices, and columns through their name.
To address the first column only, you have two options:
df.Time
df['Time']
To extract two columns at the same time, put the variable names in a list. With the
following command, a new DataFrame data is generated, containing the columns
Time and y:
data = df[['Time', 'y']]
2.2 Elements of Scientific Python Programming 23
After reading in the data, it is good practice to check if the data have been read
in correctly. The first or last few rows can be displayed with
data.head()
data.tail()
For example, the following statement shows the 5th − 10th row (note that these
are 6 rows):
data[4:10]
as 10 − 4 = 6. (I know, the array indexing takes some time to get used to. It helps
me to think of the indices as pointers to the elements, and that they start at 0. see
Fig. 2.4)
The handling of DataFrames is somewhat different from the handling of numpy
arrays. For example, (numbered) rows and (labeled) columns can be addressed
simultaneously as follows:
df[['Time', 'y']][4:10]
The standard row/column notation can be used by applying the method iloc:
df.iloc[4:10, [0,2]]
Finally, sometimes one wants direct access to the data, not to the DataFrame.
This can be achieved with
data.values
which returns a numpy array if all data have the same data type.
b) Note: Data Selection
While pandas’ DataFrames are similar to numpy arrays, their philosophy is differ-
ent. The numpy syntax comes from the mathematical description of n-dimensional
matrices. In contrast, pandas has its origin in the data analysis of column-oriented
database information. Some of the differences between the two that you should
watch out for are
• numpy handles “rows” first. For example, data[0] is the first row of an array.
• pandas starts with the columns. For example, df['values'][0] is the first ele-
ment of the column 'values'.
• If a DataFrame has labeled rows, one can extract, for example, the row “row_label”
with df.loc['row_label']. If one wants to address a row by its number, e.g.,
row number “15”, one can use df.iloc[15]. And iloc can be used to address
“rows/columns”, e.g., df.iloc[2:4,3].
• Slicing of rows also works, e.g., df[0:5] for the first 5 rows. A sometimes con-
fusing convention is that if you want to slice out a single row, e.g., row “5”, you
have to use df[5:6]. df[5] raises an error!
24 2 Python
10
2 <ISP2e>/02_GettingStarted/ISP_showPandas.py.
2.2 Elements of Scientific Python Programming 25
14 Parameters
15 ----------
16 data : numpy array (,n)
17 Incoming and outgoing account transactions
18
19 Returns
20 -------
21 income : Sum of the incoming transactions
22 expenses : Sum of the outgoing account transactions
23 """
24
25 income = np.sum(data[data>0])
26 expenses = np.sum(data[data<0])
27
28 return (income, expenses)
29
30
31 if __name__=='__main__':
32 testData = np.array([-5, 12, 3, -6, -4, 8])
33
3 For the rest of the book, the “author/date” information will be left away, to keep the program
listings more compact.
26 2 Python
float, with 5 digits, 2 of which are after the comma”.4 The corresponding values
are then passed into the f-string for formatted output. And the '\' at the end of
the line indicates a line continuation.
b) Modules
To execute the module L2_2_python_module.py from the command line, type
python L2_2_python_module.py. In Windows, if the extension “.py” is
associated with the Python program, it suffices to double-click the module, or to
type python_module.py on the command line. In WinPython, the association
of the extension “.py” with the Python function can be set by the WinPython Control
Panel.exe, by the command Register Distribution ... in the menu Advanced.
To run a module in IPython, use the magic function %run:
In [1]: %run L2_2_python_module
Your first transaction was a loss and is dropped.
You have earned 23.00 EUR, and spent 10.00 EUR.
Note that you either have to be in the directory where the function is defined, or
you have to give the full path name.
If you want to use a function or variable that is defined in a different module,
you have to import that module. This can be done in three different ways. For the
following example, assume that the other module is called new_module.py, and
the function that we want from there new_function.
• import new_module: The function can then be accessed with
new_module.new_function().
• from new_module import new_function: In this case, the function can be
called directly new_function().
• from new_module import *: This imports all variables and functions from
new_module into the current workspace; again, the function can be called directly
with new_function(). However, use of this syntax is discouraged! It clutters up
the current workspace, and one risks overwriting existing variables with the same
name as an imported variable.
If you import a module multiple times, Python recognizes that the module is
already known and skips later imports.
The next example shows you how to import functions from one module into
another module:
Listing 2.3: python_import.py
9 # Generate test-data
10 testData = np.arange(-5, 10)
11
2.3.1 Workflow
The best way to start a program is to take a paper and pencil and explicitly write
down the algorithms to be implemented! This helps to clarify the required program-
ming steps, which parameters have to be provided explicitly, and which have to be
calculated during the execution of the program. In most cases, this is also the most
efficient way to start the development of a new program.
The next step is to work out the command syntax. In Python, this is best done
with IPython/Jupyter. IPython (http://ipython.org/) provides a programming envi-
ronment that is optimized for interactive computing with Python, similar to the
command line in Matlab. It comes with a command history, interactive data visual-
ization, command completion, and a lot of features that make it quick and easy to
try out code.
Once the individual steps are working, one can use the IPython command
%history to get the commands used. One can use either copy/paste that history, or
save it directly to a file with
%history -f [fname]
Fig. 2.7 Still, the best start to the successful development of a new program!
While IPython can also be run in a terminal-environment, its full power becomes
available with Jupyter. In 2013 the IPython Notebook, a browser-based frontend for
Python, became a very popular way to share research and results in the Python com-
munity. In 2015, the development of the frontend became its own project, called
Project Jupyter (https://jupyter.org/). Today, Jupyter can be used not only with
Python, but also with Julia, R, and more than 100 other programming languages.
The most important interfaces provided by Jupyter are
• Qt console.
• Jupyter Notebook.
• JupyterLab.
Fig. 2.8 The Qt console, displaying parameter tips for the current command, np.arange
b) Jupyter Notebook
The Jupyter Notebook is a browser-based interface, which is especially well suited
for teaching, documentation, and collaborations. It allows you to combine a struc-
tured layout, equations in the popular LaTeX format, and images, and can include
resulting graphs and videos, as well as the output from Python commands (see
Fig. 2.9). Packages such as plotly (https://plot.ly/) or bokeh (https://bokeh.org/) build
on such browser-based advantages, and allow easy construction of interactive inter-
faces inside Jupyter Notebooks.
Code samples accompanying this book are also available as Jupyter Notebooks,
and can be downloaded from
https://github.com/thomas-haslwanter/statsintro-python-2e.
c) JupyterLab
JupyterLab is the successor to the Jupyter Notebook. As Fig. 2.10 shows, it extends
the Notebook with very useful capabilities such as a file browser, easy access to
commands and shortcuts, and flexible image viewers. The file format stays the same
as the Notebook, and both are saved as .ipynb-files.
2.3 Interactive Programming—IPython/Jupyter 31
Fig. 2.9 The Jupyter Notebook makes it easy to share research, formulas, and results
When working on a new problem, I always start out with the Qt console (see
Fig. 2.8).
In the following, <mydir> has to be replaced with your home directory (i.e., the
directory that opens up when you run cmd in Windows, or terminal in Linux).
And <myname> should be replaced by your name or your userID.
To start up IPython in a folder of your choice, and with personalized startup
scripts, proceed as follows:
a) In Windows
• Type Win+R, and start a command shell with cmd.
• In the newly created command shell, type
ipython profile create.
(This creates the directory <mydir>\.ipython.)
32 2 Python
Fig. 2.10 JupyterLab is the successor to Jupyter Notebook. The blue labels indicate new features
in JupyterLab
• Add the Variable IPYTHONDIR to your environment (see Sect. 2.1.2), and set
it to <mydir>\.ipython. This directory contains the startup commands for
your IPython-sessions.
• Into the startup folder
<mydir>\.ipython\profile\_default\startup,
place a file for example with the name <myname>.py, containing the startup
commands that you want to execute every time that you launch IPython. My per-
sonal startup file contains the following lines, which will import frequently used
packages:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy import stats
To see all Jupyter Notebooks that come with this book, for example, do the fol-
lowing:
• Type Win+R, and start a command shell with cmd.
• Run the commands
cd <ipynb-dir>
jupyter lab
where <ipynb-dir> is the directory where all the Jupyter Notebooks are
stored.
• Again, if you want, you can put this command-sequence into a batch-file.
b) In Linux
• In your .bashrc file (which contains the startup commands for your shell-
scripts), enter the lines
alias ipy='jupyter qtconsole'
IPYTHONDIR='~/.ipython'
You can now start “your” IPython by just typing ipy, and JupyterLab by typing
ipynb.sh.
34 2 Python
c) In Mac OS X
• The next steps are somewhat tricky. Mac OS X by default hides the folders
that start with “.” (They can be shown with cmd-shift-.). So to access
.ipython, open File -> Save as..... Now open a Finder window,
click the Go menu, select Go to Folder, and enter
<mydir>/.ipython/profile_default/startup. This will open a
Finder window with a header named “startup”. On the left of this text, there
should be a blue folder icon. Drag and drop the folder into the Save as... win-
dow open in the editor. IPython has a README file explaining the naming
conventions. In our case, the file should begin with 00-, so we could name it
00-<myname>. .
• Open your .bash_profile (which contains the startup commands for your
shell scripts), and enter the line
alias ipy='jupyter qtconsole'.
• To see all IPython Notebooks, do the following:
– Go to <mydir>.
– Create the file ipynb.sh, containing the lines
#!/bin/bash
cd <ipynb_dir>
jupyter lab
In this example, I start my IPython sessions from the command line, with the
command jupyter qtconsole. (Under WinPython: if you have problems start-
ing Jupyter from the cmd console, use the WinPython Command Prompt instead—it
is nothing else but a command terminal with the environment variables set such that
Python is readily found.)
To get started with Python and IPython, let me go step-by-step through the
IPython session in Fig. 2.11:
• IPython starts out listing the versions of IPython and Python that are used.
• In [1]: It is customary to import numpy as np, and matplotlib.pyplot, the mat-
plotlib module containing all the plotting commands, as plt. Note that by hitting
CTRL+Enter, one can execute multiline commands. (The command sequence
gets executed after the next empty line.)
• In [2]: The command t = np.arange(0,10,0.1) generates a vector from 0 to
10, with a step size of 0.1. arange is a command in the numpy package.
• In [3]: Calculates omega. Note that the value of pi is only defined in numpy,
and does not exist in Python!
• In [4]: Since t is a vector, and sin is a function from numpy, the sine value is
calculated automatically for each value of t.
• In [5]: The “IPython magic function” pwd stands for “print working directory”,
and does just that. Tasks common with interactive computing, such as directory
changes (%cd), bookmarks for directories (%bookmark), and inspection of the
workspace (%who and %whos), are implemented as “IPython magic functions”.
If no Python variable with the same name exists, the “%” sign can be left away,
as here.
• In [7]: All the plotting commands are in the package plt. IPython generates plots
by default in the Jupyter Qt console, as shown in Fig. 2.11. Generating graphics
files is also very simple: here, I generate the PNG-file “Sinewave.png”, with a
resolution of 200 dots-per-inch.
I have mentioned above that matplotlib handles the graphics output. In Jupyter,
you can switch between inline graphs and output into an external Graphics win-
dow with %matplotlib inline and %matplotlib qt5 (see Fig. 2.12).
(Depending on your version of Python, you may have to replace %matplotlib
qt5 with %matplotlib or with %matplotlib tk.) An external graphics win-
dow allows zooming and panning, gets the cursor position (which can help to find
outliers), and gets interactive input with the command plt.ginput. matplotlib’s
plotting commands closely follow Matlab conventions.
IPython is very helpful in working out the command syntax and sequence. The next
step is to turn these commands into a Python program with comments that can be
run from the command line. This section introduces a number of Python conventions
and syntax features.
For me, an efficient way to turn IPython commands into a script is to
• first obtain the command history with the command %hist or %history. (With
the option -f, you can save the history directly with the desired filename.)
• copy the history into a good integrated development environment (IDE): my pre-
ferred IDE is Wing (http://www.wingware.com/), because it provides a very com-
Random documents with unrelated
content Scribd suggests to you:
animals belonging to the articulate type, the Insecta possess the
highest instincts. To this class belong the following orders: I.
Neuroptera,[14] or lace-winged insects, of which the Dragon-fly, or
Devil’s Darning Needle, is a good representative. II. The
Orthoptera[15] (straight-winged). They have four wings, the front
pair thick and narrow, overlapping along the back; the hind pair
broad, net-veined and folded upon the abdomen. The representative
forms are Crickets, Grasshoppers, Locusts, and Cockroaches. III.
Hemiptera[16] (half winged). To this order belong the wingless Bed-
bug, the Squash-bug, the Seventeen-year Locust, and the Cochineal.
Coccus-cacti[17]. The Mexican cochineal insect is of great value as
a dye, and from it the most beautiful scarlet and crimson colors are
obtained. The female is wingless, and, as an uncomely parasite, lives
and feeds on cactus plants, especially those of the nopal[18] species,
native in Mexico and Peru. The male only is represented in the
diagram, and magnified somewhat larger than life. They are
comparatively few in number, and of no commercial value. The
plants are cultivated for the purpose, and the care of the insects,
which increase very rapidly, is an industry giving employment to
thousands of laborers, and in some parts of Mexico the product of
the cochineal farms is among the most valuable of their exports.
Cortes[19] in the sixteenth century received instructions from the
Spanish court to obtain cochineal in as large quantities as possible.
The export became very large, both Spaniards and others becoming
skilled in the use of the beautiful dye stuff, nearly a hundred years
before its real nature was known. The dried insect being very small,
and crushed in preparing it for market, it was supposed to be the
seed of some plant; and it was not until in 1703 that its true nature
was discovered by microscopic observations. The industry still
flourishes in Mexico, but both plant and the insect have been taken
to other countries, and do well. The annual export of cochineal from
the Canary islands has, in the present century, amounted to over
6,000 tons, valued at more than $4,000,000. The manner of
collecting the insects is very simple. When of sufficient age, some
already dead, and others yet alive, they are brushed off into bags,
and the living killed by holding them either in boiling water or heated
ovens, and then exposed in the sun till quite dry. The dried insects
have the form and appearance of irregular fluted and concave
grains, of which it is estimated there are 70,000 in a pound.
THE COCHINEAL.
METAMORPHOSIS OF A BEETLE.
SUB-KINGDOM VIII.—TUNICATA.
This very small class of animals is distinguished by the leathery
sac-like covering, from which they take their name of Tunicata
(having a tunic). The Ascidian[28] is the best known representative.
It is found fastened to rocks, shells, crabs, and other bodies. These
animals are both simple and compound; the latter are often
phosphorescent. They have neither feet, head nor shell, but a
shapeless body with apertures at both ends.
SUB-KINGDOM IX.—VERTEBRATA.
This division includes the most perfect animals. Their chief
distinguishing characteristics are an internal skeleton; a backbone; a
dorsal nervous cord, separated from the body cavity; a complete
circulation, and limbs not exceeding four. There are about 25,000
living species, beside the numberless host now extinct.
Lowest of the vertebrates, and closely related to the true fishes,
are the Lancelet and Lamprey.[29] The former is a lance-shaped
animal having no skeleton, but boasting the rudiment of a backbone
in a string-like cartilaginous cord. The organs are very simple, the
heart being a long sac in which colorless blood circulates. It breathes
by taking in water through the mouth and letting it out through the
gill-slits. The Lamprey belongs to the pouch-gilled vertebrates or
Marsipobranchii.[30] It is an eel-shaped animal of about three feet in
length.
The round, soft mouth is suctorial, the tongue acting as a piston.
By suction it can anchor itself to a rock, and allow the long body to
float freely, without being carried by the current from the place.
There are seven gill openings on each side, and the whole breathing
apparatus so arranged that the animal can live some days out of the
water. In some parts of England it is in demand for the table, and
“lamprey pie”[31] is esteemed a great luxury. The American lamprey
is similar, and the flesh good, but in less demand in our markets.
During the breeding season those about the estuaries go up stream
as do the shad, and by rolling stones together construct large conical
nests[32] for the protection of their young. With apparently little
adaptation for such architecture they accomplish wonders in that
line.
THE LAMPREY.
BY FRANCES E. WILLARD,
President National W. C. T. U.
CHAPTER III.
But—as I was saying when the stern old gentleman was pleased
to interrupt me—I am to give you reasons why you are to cultivate
your specialty. And I claim, first (as has been implied already), that
you should do this because you have a specialty to cultivate. (This,
on the principle of the old cook book, which begins its “Recipe for
Broiling Hares,” with the straightforward exhortation: “First catch
your hare.”) The second reason is, because you will then work more
easily and naturally, with the least friction, with the greatest pleasure
to yourself and the most advantage to those around you. “Paddle
your own canoe,” but paddle it right out into the swift, sure current
of your strongest, noblest inclination. Thirdly, by this means you will
get into your cranium, in place of aimless reverie, a resolute aim.
This is where your brother has had his chief intellectual advantage
over you. Quicker of wit than he, far less unwieldy in your mental
processes, swifter in judgment, and every whit as accurate, you still
have felt, when measuring intellectual swords with him, that yours
was in your left hand, that his was in his right; and you have felt this
chiefly, as I believe, because from the dawn of thought in his sturdy
young brain, he has been taught that he must have a definite aim in
life if he ever meant to swell the ranks of the somebodies upon this
planet, while you have been just as sedulously taught that the
handsome prince might whirl past your door “’most any day,” lift you
to a seat beside him in his golden chariot and carry you off to his
castle in Spain.
And of course you dream about all this; why shouldn’t you? Who
wouldn’t? But, my dear friends, dreaming is the poorest of all
grindstones on which to sharpen one’s wits. And to my thinking, the
rust of woman’s intellect, the canker of her heart, the “worm i’ the
bud” of her noblest possibilities has been this aimless reverie; this
rambling of the thoughts; this vagueness, which when it is finished,
is vacuity. Let us turn our gaze inward, those of us who are not
thorough-going workers with brain or hand. What do we find? A wild
chaos; a glimmering nebula of fancies; an insipid brain-soup where a
few lumps of thought swim in a watery gravy of dreams, and, as
nothing can come of nothing, what wonder if no brilliancy of
achievement promises to flood our future with its light? Few women,
growing up under the present order of things, can claim complete
exemption from this grave intellectual infirmity.
Somehow one falls so readily into a sort of mental indolence;
one’s thoughts flow onward in a pleasant, gurgling stream, a sort of
intellectual lullaby, coming no-whence, going no-whither. Only one
thing can help you if you are in this extremity, and that is what your
brothers have—the snag of a fixed purpose in this stream of
thought. Around this will soon cluster the dormant ideas, hopes, and
possibilities that have thus far floated at random. The first one in the
idle stream of my life was the purpose, lodged there by my life’s best
friend, my mother, to have an education. Then, later on, Charlotte
Bronte’s “Shirley” was a tremendous snag in the stream to me.
Around that brave and steadfast character clustered a thousand new
resolves. I was never quite so steeped in reveries again, though my
temptations were unusual; my “Forest Home,” by a Wisconsin river,
offering few reminders to my girlish thought, of the wide, wide world
and its sore need of workers. The next jog that I got was from the
intellectual attrition of a gifted and scholarly woman who asked me
often to her home and sent me away laden with volumes of
Wordsworth, Niebuhr and the British essayists. Margaret Fuller Ossoli
was another fixed point—shall I not rather say a fixed star?—in the
sky of my thought, while Arnold of Rugby, to one who meant to
make teaching a profession, was chief of all. Well, is it possible that
any word I have here written may set some of you thinking—that’s
it, set you, a fixed purpose rather than a floating one—about a
definite object in life toward which, henceforth, you may bend a
steady, earnest gaze? I am not speaking of a thorough intellectual
training only. It is rather to the life-work, which only a lifetime can
fully compass, that I would direct your thoughts. Rather than that
you should fail to have a fixed purpose concerning it, I would that
your mental attitude might be like the one confided to me by a
charming Philadelphia girl, whose letter of this morning has the
following naïve statement:
“I feel such an aching in me to do or be something
uncommon, and yet a kind of awful assurance that I never
shall.”
Nor do I here refer to that general knowledge of household arts
which forms the sole acquirement set forth in the regulation
“Women’s Department” of the bygone age newspaper, which in
many localities remains in this like the boulder of a past epoch.
It was once thought to be a high virtue for women, no matter
how lofty in station or how ample of fortune, to do their own work
with the needle. Homer represents Penelope spinning, surrounded
by her maids, and classic art abounds with illustrations of like
character. But the virtues of one age often become the mistakes of
the next. When loom, needle and broom were woman’s only
weapons, she did well to handle them deftly, no matter what her
rank, for they were her bread-winning implements, and fortune has
been proverbially fickle in all ages. But men, by their “witty
inventions,” have perpetually encroached on “woman’s sphere.”
Eli Whitney, with his cotton gin, Elias Howe, with his sewing
machine, and a hundred other intricate-brained mechanics who have
set steel fingers to do in an hour what women’s fingers could not
accomplish in a year; all these have combined to revolutionize the
daily cares of the gentler sex. With former occupations gone, and
the world’s welcome ready when they succeed in special vocations
new to them, it becomes not only the privilege but the sacred duty
of every woman to cultivate and utilize her highest gift. There is no
more practical form of philanthropy than this, for every one who
makes a place for herself “higher up” leaves one lower down for
some other woman who, but for the vacancy thus afforded her in
the world’s close crowded ranks, might be tempted into paths of sin.
There is an army of poor girls wholly dependent, for a livelihood,
upon the doing of house work. They have no other earthly resource
between them and the poor house or haunt of infamy. There is
another class to whom an honorable support can come only by
sewing or millinery work. Whoever then fits herself for some
employment involving better pay and higher social recognition,
graduates out of these lower grades and leaves them to those who
can not so advance, has helped the world along in a substantial way,
because she has added to the sum of humanity’s well being.
To young women in wealthy homes, these considerations should
come with even greater convincing force. As David Swing has wisely
said to his own rich congregation:
“The rhetoric thrown at women of property for not doing ‘their
own work’ could only be useful in an age of fashionable idleness, but
in a busy age it is a part of nature’s law that what are called the
‘better classes’ shall leave for the poorer classes some labor to be
done, just as the Mosaic law left some sheaves in the field for the
gleaner. The world’s work is to be apportioned according to the need
and capability of its workers, and the higher order of power must not
encroach upon the task which nature seems to have set apart for
the employment and support of the less capable.”
Let it not be concluded that I have meant to speak lightly of the
intricate, skilled labor involved in making healthful and attractive that
bright, consummate flower of a Christian civilization—the home. I
have felt that this theme has been so often treated that it needed no
amplification at my hands, but I will add that, having been
entertained in scores of homes belonging to “exceptional women,”
“women with a career,” etc., my testimony is that for
wholesomeness, heartsomeness, and every quality that superadds
home-making to housekeeping, I have never seen their superiors,
and seldom, take them all in all, their peers. But as a rule, these
women have earned the “wherewithal” to make a home, by the
exercise of some good gift of brain or hand, and thus having been
enabled to put a proxy in the kitchen, they direct, but do not attend
to the minutiæ of their daily household cares.
Cultivate, then, your specialty, because the independence thus
involved will lift you above the world’s pity to the level of its respect,
perchance its honor. Understand this first, last, and always: The
world wants the best thing. It wants your best. It needs you as a
significant figure to give its ciphers value; to designate as an
example; to serve up in a eulogy, perchance to shine in the galaxy
by whose light alone its centuries maintain their places in the
firmament of history. I know this may strike you as contradiction, for
the paradox of paradoxes is this crotchety but kind, narrow-minded
but just old world in which you and I are cast away, like Æneas in
the domain of Dido. The effrontery of “Madame Grundy” passes all
comprehension, and would be laughable if it were not so sad. She
tells us women distinctly that we positively shall not do for society
the thing we can do best; she declares that if we attempt it we shall
be frowned down, and practically ostracised, if not utterly made
away with, and then, if we go right on and succeed, she trumpets
our names from sea to shore, showers us with greenbacks, and nods
her conventional old head with a knowing “I told you so.” And per
contra, while on one hand this same unreasonable old lady cripples
our attempts to succeed, on the other she snubs us for not doing so.
In fact, she is so poor a mathematician that she has never yet so
much as tried to learn the value of the “unknown quantity.” The
mute Milton is, to her, indeed “inglorious.” Her code of ethics
recognizes just one crime (not mentioned in the Decalogue), and it
is Failure. Her law is written on a single table—it is a table of stone—
and it reads thus: “Succeed and live; make shipwreck of success,
and die.”
And so, young friends, fold away your talents in a napkin if you
choose; the world will not openly reprove you. She will never urge
you to bring out your hidden treasure, but she knows right well
when you defraud her, and the relentless old tyrant will punish you,
with tireless lash, because you did not bring all your tithes into the
storehouse of the common good, because you lived “beneath your
privilege;” because, for yourself (which means for her), you did not
“covet earnestly the best gifts.” She will cut you on the public street
when she would have shown you all her teeth in smiles. She will
send poverty on your track, when you might have sat down at her
banquet an honored guest. Yes, the world wants the best thing; your
best, and she will smite you stealthily if you do not hand over your
gift. Now last, but not least (under the head of reasons for seeking
to know your true vocation as a human being), let me bring forward
the rationale of the bread-and-butter argument. In sooth, no writer
or speaker may omit it with impunity, if he would retire in good order
from an American audience. Briefly, then, your specialty, well
trained, is your best bread-winning implement, and she who earliest
grasps this, and who firmest holds it, comes off best in the race. “Be
not simply good, be good for something,” said Henry D. Thoreau. A
bright eyed girl of eighteen used to come to me on Friday evenings
to give me German lessons. To be sure, I have lived in Germany, and
she has never been out of Illinois, but then that language is not my
specialty, while it is hers. “How is it that though so young, you have
made yourself independent?” I inquired of her one day. Listen to the
reply: “My mother was always quoting this saying of Carlyle: ‘The
man who has a sixpence commands the world—to the extent of that
sixpence.’ I early laid this sentiment to heart. Besides, when I was
fifteen years old, I heard a sermon on the text: ‘This one thing I do.’
Being of a practical turn of mind, I made an application of which the
preacher, perhaps, had no intention. I thought, why not in everyday
affairs as well as in religion do one thing well, rather than many
things indifferently, and in that way secure the magic sixpence of
Carlyle! My father was a rich man then, but I resolved to prepare
myself to teach the German language, of which I was very fond, by
way of a profession. When the Chicago fire came we lost our
property, but I discovered that I could not only support myself, but
help my father to many a convenient sixpence, because, in
prosperous days I had forearmed myself with a cultivated specialty.”
As she told me this, I thought how, from widely different premises
and conditions in life young people may reach similar conclusions.
For instance, on the top of the great St. Bernard, I said to the
“Hospitable Father,” a noble young monk, “How is it that you, so
gifted and well taught, are spending your life away up here among
eternal snows?” And I shall never forget his look of exaltation as he
simply answered, “’Tis my vocation.”
After all, this is the vital question: With what sort of a weapon will
you ward off the attacks of the blood-hound Poverty, which Dame
Fortune is pretty sure to let on everybody’s track sooner or later, that
she may try his mettle, and learn what manner of spirit he is of? In
times like these, when men’s hearts are failing them for fear, when
riches are saved the trouble of “taking to themselves wings” by the
faithless cashiers and bookkeepers who are adepts at furnishing
these flying implements, and, above all, when labor is coming to be
king, the question “What will you do?” has fresh significance.
Remember, going forth from the uncertain Eden of your dreams, into
the satisfying pleasures of honest, hard work, “the world is all before
you, where to choose.” Will you share some other woman’s home,
and help her make it beautiful? No task more noble or more needed
awaits the thoughtful worker of to-day. The world exists but for the
sake of its homes. Will you bestow your hand upon some fine
æsthetic industry, as drawing, designing, engraving, telegraphing,
phonographing, photographing? Will you be an architect? a printer?
an editor? Will you enter one of the three learned professions?
Braver women than you or I have won a foot-hold for us in each of
them; as to the brain-hold, that is our affair. I will not now pursue
the question further. Only the “Cyclopædia of Woman’s Occupations”
(a book I recommend to your attention) can exhaust it, and with it
exhaust you and the world’s work, too, for that matter!
After all, it doesn’t so much signify what you may do as that you
do it well, whatever it may be. For the value of skilled labor is
estimated on a democratic basis, nowadays. President Eliot, of
Harvard University, the cook in the Parker House restaurant, and
Mary L. Booth, who edits Harper’s Bazar, each receive $4,000 per
year.
Think a moment. Will you be led to say: “The good old ways are
good enough for me,” and so drop into the swollen ranks of
teacherdom, or rattle awhile on a martyrized piano, and then set up
for a musician, though you have not a particle of music in throat or
finger-tips? Or will you stay at home and let papa support you until
you grow tired of doing nothing and expecting nothing, and proceed
to marry some man whom you endure rather than love, just to get
decently out of your dilemma?
Nay, I do you injustice. Few girls who breathe the free air of our
western prairies will be so cowardly. I may not construct your
horoscope, but this much I will venture—that when you marry, no
matter what you find, you will seek not a name, behind which to
cover up the insignificance of your own; not a “good provider,” to
feed and clothe one who has learned how to feed and clothe herself;
not a “natural protector,” to shield you in his plaidie, the gallant,
gallant laddie, from the cauld, cauld blast; but you will seek (and
may heaven grant that you shall find) that rarest, choicest, most
elusive prize of man’s existence, as of woman’s; one which—
mournfully I say it—the modern marriage is by no means certain to
involve, namely, a mate. At this juncture, shrewd mater familias
whispers to pater: “That’s the first orthodox word she’s said.” Some
youth throws down the magazine and mutters to himself: “There, I
knew it would come to this! Look at the absurdity of these women!
Why, they preach up all sorts of trades and professions, and then
they come back, at last, to the ‘good old way’ they have forsaken,
and advise every young lady to get a situation in a school of one
scholar, and her board thrown in.”
Meanwhile, heroic Hypatia sits near by, and “musing in maiden
meditation, fancy free,” on a “career,” murmurs within herself, “To
this complexion must it come at last!”
Peace, peace, good friends! This seeming inconsistency is readily
explained. In this century, when the wage of battle has cost our land
an army of her sons, when widows mourn, and unwedded
thousands are forced to meet the hard-faced world (from which
rose-water theorists would shield them), America is coming to the
rescue of her daughters! For the nearer perfect—that is, the more
Christian—a civilization has become, the more carefully are the
exceptional classes of society provided for. All our philanthropic
institutions under state or private patronage illustrate this. In less
enlightened days, your ideal woman composed the single, grand
class for which public prejudice set itself to provide. She was to be
the wife and mother, and she was carefully enshrined at home. But,
happily, this is the world’s way no longer. The exceptions are so
many, made by war, by the thousand misunderstandings and cross-
purposes of social intercourse, by the peculiar features of the
transition period in which we live, by the absurdly extravagant
customs of our day, and the false notions of both men and women—
that not to provide for them would be a monstrous meanness, if not
a crime. And the provision made in this instance is the most rational,
indeed, the only rational one which it is in the power of society or
government to make for any save the utterly incapable, namely: a
fair chance for self-help. Nor (to pursue the line of our argument still
further) can we forget that skeleton hand which, in utter disregard
of “the proprieties” in destiny’s drama, thrusts itself so often into the
charmed domestic circle, and snatches the beloved “provider” away
forever, while it sets gaunt famine by the fireside in his stead? Can
we forget that, in ten thousand families, wives are this moment
waiting in suspense and agony the return of wretched husbands to
homes made hideous by the drunkard’s sin—wives whose work of
brain or hand alone keeps their children from want, now that their
“strong staff is broken, and their beautiful rod?” There are delicate
white fingers turning the page on which I print these words, that will
never wear the marriage ring; there are slight forms bending over
my friendly lines, which, not far down the years, will be clothed in
widow’s weeds. Alas, there are as surely others, who, when they
have been wooed and won, shall find that they are worse than
widowed. And what of these three classes of women, sweet and
helpless? Clearly, to all of them I am declaring a true and blessed
gospel, in this good news concerning honest independence and
brave self-help! Clearly, also, no one is wise enough to go through
the assembly of my readers, and tell me who, in future years, shall
need a bread-winning weapon with which to defend herself and
perchance, also, the helpless ones between whom and the world
there may be no arm but hers. But it is a principle in public as well
as private economy, that the wisest foresight provides for the
remotest contingency, and thus, in its full force, all that I have been
saying applies to every woman who may read this article on “How to
Win.” Suppose that many of you, dear girls, are destined to a downy
nest, instead of a strong-winged flight. What then? Will the years
spent in making the most of the best powers with which God has
endowed you be worse employed than if you had given them to
fashion and frivolity? Those “ad interim” years which separate the
graduate’s diploma from the bride’s marriage certificate, can they
possibly be invested better than in the acquisition of some useful
trade or dignified profession? And then, aside from this, I would help
the youngest of you to remember (even in the bewildered years of
her second decade) what noble Margaret Fuller said: “No woman
can give her hand with dignity, or her heart with loyalty, until she
has learned how to stand alone.” It is not so much what comes to
you as what you come to, that determines whether you are a winner
in the great race of life. Never forget that the only indestructible
material in destiny’s fierce crucible is character. Say this, not to
another—say it to yourself; utter it early, and repeat it often: Fail me
not, thou.
THE LIFE OF MINERALS.
BY M. J. THOULET.
ebookbell.com