PDF Learning IPython for Interactive Computing and Data Visualization - Second Edition Cyrille Rossant download
PDF Learning IPython for Interactive Computing and Data Visualization - Second Edition Cyrille Rossant download
com
https://textbookfull.com/product/learning-ipython-for-
interactive-computing-and-data-visualization-second-edition-
cyrille-rossant/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/interactive-data-visualization-for-
the-web-murray/
textboxfull.com
https://textbookfull.com/product/interactive-data-visualization-for-
the-web-an-introduction-to-designing-with-d3-1st-edition-murray/
textboxfull.com
https://textbookfull.com/product/python-for-data-analysis-data-
wrangling-with-pandas-numpy-and-ipython-wes-mckinney/
textboxfull.com
https://textbookfull.com/product/interactive-data-visualization-for-
the-web-an-introduction-to-designing-with-d3-2nd-edition-scott-murray/
textboxfull.com
Learning d3 js Data Visualization Ændrew Rininsland
https://textbookfull.com/product/learning-d3-js-data-visualization-
aendrew-rininsland/
textboxfull.com
https://textbookfull.com/product/interactive-web-based-data-
visualization-with-r-plotly-and-shiny-1st-edition-carson-sievert/
textboxfull.com
https://textbookfull.com/product/practical-python-data-visualization-
a-fast-track-approach-to-learning-data-visualization-with-python-
ashwin-pajankar/
textboxfull.com
Cyrille Rossant
BIRMINGHAM - MUMBAI
Learning IPython for Interactive Computing
and Data Visualization
Second Edition
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.
ISBN 978-1-78398-698-9
www.packtpub.com
Credits
Reviewers Proofreader
Damián Avila Safis Editing
Nicola Rainiero
G Scott Stukey Indexer
Monica Ajmera Mehta
Commissioning Editor
Kartikey Pandey Production Coordinator
Conidon Miranda
Acquisition Editors
Kartikey Pandey Cover Work
Conidon Miranda
Richard Brookes-Bland
Technical Editor
Pranil Pathare
Copy Editor
Stephen Copestake
About the Author
In the open source area, he is a core developer for several interesting and popular
projects, such as IPython/Jupyter, Bokeh, and Nikola. He has also started his own
projects, being RISE, an extension to enable amazing live slides in the Jupyter
notebook, the most popular one. He has also written several tutorials about
the Scientific Python tools (available at Github) and presented several talks
at international conferences.
He has an ambition to simplify his work with open software and use and develop
new ones; sometimes obtaining good results, at other times, negative. You can reach
Nicola on his website at http://rainnic.altervista.org.
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.
TM
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 read Packt'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
[i]
Table of Contents
[ ii ]
Table of Contents
Manipulating data 69
Selecting data 69
Selecting columns 70
Selecting rows 70
Filtering with boolean indexing 72
Computing with numbers 73
Working with text 75
Working with dates and times 76
Handling missing data 77
Complex operations 78
Group-by 78
Joins 80
Summary 83
Chapter 3: Numerical Computing with NumPy 85
A primer to vector computing 85
Multidimensional arrays 86
The ndarray 86
Vector operations on ndarrays 87
How fast are vector computations in NumPy? 88
How an ndarray is stored in memory 89
Why operations on ndarrays are fast 91
Creating and loading arrays 91
Creating arrays 91
Loading arrays from files 93
Basic array manipulations 94
Computing with NumPy arrays 97
Selection and indexing 98
Boolean operations on arrays 99
Mathematical operations on arrays 100
A density map with NumPy 103
Other topics 107
Summary 108
Chapter 4: Interactive Plotting and Graphical Interfaces 109
Choosing a plotting backend 109
Inline plots 109
Exported figures 111
GUI toolkits 111
Dynamic inline plots 113
Web-based visualization 114
[ iii ]
Table of Contents
[ iv ]
Table of Contents
[v]
Preface
Data analysis skills are now essential in scientific research, engineering, finance,
economics, journalism, and many other domains. With its high accessibility and
vibrant ecosystem, Python is one of the most appreciated open source languages for
data science.
The first edition of Learning IPython for Interactive Computing and Data Visualization
was published in April 2013, several months before the release of IPython 1.0. This
new edition targets IPython 4.0, released in August 2015. In addition to reflecting the
novelties of this new version of IPython, the present book is also more accessible to
non-programmer beginners. The first chapter contains a brand new crash course on
Python programming, as well as detailed installation instructions.
Since the first edition of this book, IPython's popularity has grown significantly,
with an estimated user base of several millions of people and ongoing collaborations
with large companies like Microsoft, Google, IBM, and others. The project itself has
been subject to important changes, with a refactoring into a language-independent
interface called the Jupyter Notebook, and a set of backend kernels in various
languages. The Notebook is no longer reserved to Python; it can now also be used
with R, Julia, Ruby, Haskell, and many more languages (50 at the time of this
writing!).
[ vii ]
Preface
The Jupyter project has received significant funding in 2015 from the Leona M. and
Harry B. Helmsley Charitable Trust, the Gordon and Betty Moore Foundation, and
the Alfred P. Sloan Foundation, which will allow the developers to focus on the
growth and maturity of the project in the years to come.
Chapter 3, Numerical Computing with NumPy, details how to use NumPy for efficient
computing on multidimensional numerical arrays.
Chapter 6, Customizing IPython, shows how IPython and the Jupyter Notebook can be
extended for customized use-cases.
[ viii ]
Preface
Since our world is becoming more and more data-driven, knowing how to analyze
data effectively is an essential skill to learn. If you're used to spreadsheet programs
like Microsoft Excel, you will appreciate Python for its much larger range of analysis
and visualization possibilities. Knowing this general-purpose language will also let
you share your data and analysis with other programs and libraries.
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, database table names, folder names, filenames, file extensions,
pathnames, dummy URLs, user input, and Twitter handles are shown as follows:
"Run it with a command like bash Anaconda3-2.3.0-Linux-x86_64.sh (if
necessary, replace the filename by the one you downloaded)."
[ ix ]
Preface
New terms and important words are shown in bold. Words that you see on the
screen, for example, in menus or dialog boxes, appear in the text like this: "To create
a new notebook, click on the New button, and select Notebook (Python 3)."
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.
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.
You can also report any issues at https://github.com/ipython-books/minibook-
2nd-code/issues.
[x]
Preface
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.
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.
[ xi ]
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.
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.
Getting Started with IPython
In this chapter, we will cover the following topics:
In the past few years, Python has also emerged as one of the leading open
platforms for data science and high-performance numerical computing. This might
seem surprising as Python was not originally designed for scientific computing.
Python's interpreted nature makes it much slower than lower-level languages like
C or Fortran, which are more amenable to number crunching and the efficient
implementation of complex mathematical algorithms.
However, the performance of these low-level languages comes at a cost: they are
hard to use and they require advanced knowledge of how computers work. In the
late 1990s, several scientists began investigating the possibility of using Python for
numerical computing by interoperating it with mainstream C/Fortran scientific
libraries. This would bring together the ease-of-use of Python with the performance
of C/Fortran: the dream of any scientist!
[1]
Getting Started with IPython
Consequently, the past 15 years have seen the development of widely-used libraries
such as NumPy (providing a practical array data structure), SciPy (scientific
computing), matplotlib (graphical plotting), pandas (data analysis and statistics),
scikit-learn (machine learning), SymPy (symbolic computing), and Jupyter/IPython
(efficient interfaces for interactive computing). Python, along with this set of
libraries, is sometimes referred to as the SciPy stack or PyData platform.
Competing platforms
Python has several competitors. For example, MATLAB (by Mathworks)
is a commercial software focusing on numerical computing that is
widely-used in scientific research and engineering. SPSS (by IBM) is a
commercial software for statistical analysis. Python, however, is free and
open source, and that's one of its greatest strengths. Alternative open
source platforms include R (specialized in statistics) and Julia (a young
language for high-performance numerical computing).
This book provides a solid introduction to the whole platform by focusing on one
of its main components: Jupyter/IPython.
[2]
Chapter 1
Example of a notebook
It quickly became clear that this interface could be used with languages other than
Python such as R, Julia, Lua, Ruby, and many others. Further, the Notebook is not
restricted to scientific computing: it can be used for academic courses, software
documentation, or book writing thanks to conversion tools targeting Markdown,
HTML, PDF, ODT, and many other formats. Therefore, the IPython developers
decided in 2014 to acknowledge the general-purpose nature of the Notebook by
giving a new name to the project: Jupyter.
[3]
Getting Started with IPython
In August 2015, the IPython/Jupyter developers achieved the "Big Split" by splitting
the previous monolithic IPython codebase into a set of smaller projects, including
the language-independent Jupyter Notebook (see https://blog.jupyter.
org/2015/08/12/first-release-of-jupyter/). For example, the parallel
computing features of IPython are now implemented in a standalone Python
package named ipyparallel, the IPython widgets are implemented in ipywidgets,
and so on. This separation makes the code of the project more modular and facilitates
third-party contributions. IPython itself is now a much smaller project than before
since it only features the interactive Python terminal and the Python kernel for the
Jupyter Notebook.
This book gives you a solid introduction to Jupyter and the SciPy
platform. The IPython Interactive Computing and Visualization Cookbook
(http://ipython-books.github.io/cookbook/) is the sequel of
this introductory-level book. In 15 chapters and more than 500 pages,
it contains a hundred recipes covering a wide range of interactive
numerical computing techniques and data science topics. The IPython
Cookbook is an excellent addition to the present IPython minibook if
you're interested in delving into the platform in much greater detail.
[4]
Chapter 1
References
Here are a few references about IPython and the Notebook:
Anaconda comes with a package manager named conda, which lets you manage
your Python distribution and install new packages.
Miniconda
Miniconda (http://conda.pydata.org/miniconda.html) is
a light version of Anaconda that gives you the ability to only install
the packages you need.
[5]
Getting Started with IPython
Downloading Anaconda
The first step is to download Anaconda from Continuum Analytics' website
(http://continuum.io/downloads). This is actually not the easiest part since
several versions are available. Three properties define a particular version:
• The operating system (OS): Linux, Mac OS X, or Windows. This will depend
on the computer you want to install Python on.
• 32-bit or 64-bit: You want the 64-bit version, unless you're on an old or low-
end computer. The 64-bit version will allow you to manipulate large datasets.
• The version of Python: 2.7, or 3.4 (or later). In this book, we will use
Python 3.4. You can also use Python 3.5 (released in September 2015)
which introduces many features, including a new @ operator for matrix
multiplication. However, it is easy to temporarily switch to a Python 2.7
environment with Anaconda if necessary (see the next section).
Once you have found the right link for your OS and Python 3 64-bit, you can
download the package. You should then find it in your downloads directory
(depending on your OS and your browser's settings).
Installing Anaconda
The Anaconda installer comes in different flavors depending on your OS, as follows:
• Linux: The Linux installer is a bash .sh script. Run it with a command
like bash Anaconda3-2.3.0-Linux-x86_64.sh (if necessary, replace the
filename by the one you downloaded).
• Mac: The Mac graphical installer is a .pkg file that you can run with a
double-click.
• Windows: The Windows graphical installer is an .exe file that you can run
with a double-click.
[6]
Chapter 1
Then, follow the instructions to install Anaconda on your computer. Here are a few
remarks:
• You don't need administrator rights to install Anaconda. In most cases, you
can choose to install it in your personal user account.
• Choose to put Anaconda in your system path, so that Anaconda's Python is
the system default.
Anaconda comes with a graphical launcher that you can use to start
IPython, manage environments, and so on. You will find more details
at http://docs.continuum.io/anaconda-launcher/
You can skip this section if you already know how to do these things.
Opening a terminal
A terminal is a command-line application that lets you interact with your computer
by typing commands with the keyboard, instead of clicking on windows with the
mouse. While most computer users only know Graphical User Interfaces, developers
and scientists generally need to know how to use the command-line interface for
advanced usage. To use the command-line interface, follow the instructions that are
specific to your OS:
• On Windows, you can use Powershell. Press the Windows + R keys, type
powershell in the Run box, and press Enter. You will find more information
about Powershell at https://blog.udemy.com/powershell-tutorial/.
Alternatively, you can use the older Windows terminal by typing cmd in the
Run box.
• On OS X, you can open the Terminal application, for example by pressing
Cmd + Space, typing terminal, and pressing Enter.
• On Linux, you can open the Terminal from your application manager.
[8]
Chapter 1
If you have this problem, you may need to add the path to the Anaconda executables
to your system path. For example, this path will be ~/anaconda3/bin if you chose to
install Anaconda in ~/anaconda3. The bin directory contains Anaconda executables
including python.
If you have any problem installing and testing Anaconda, you can ask for help on
the mailing list (see the link in the References section under the Installing Python with
Anaconda section of this chapter).
Next, exit the Python prompt by typing exit() and pressing Enter.
Managing environments
Anaconda lets you create different isolated Python environments. For example, you
can have a Python 2 distribution for the rare cases where you need to temporarily
switch to Python 2.
[9]
Getting Started with IPython
This will create a new isolated environment named py2 based on the original
Anaconda distribution, but with Python 2.7. You could also use the command conda
env: type conda env -h to see the details.
You can now activate your py2 environment by typing the following command in a
terminal:
• Windows: activate py2 (note that you might have problems with
Powershell, see https://github.com/conda/conda/issues/626, or use the
old cmd terminal)
• Linux and Mac OS X: source activate py2
Now, you should see a (py2) prefix in front of your terminal prompt. Typing
python in your terminal with the py2 environment activated will open a Python 2
interpreter.
[ 10 ]
Another Random Scribd Document
with Unrelated Content
make it easy for the other party to yield to his interest. It mattered
little to him on what terms she accepted him as her husband. He
would have given the greater part of his fortune to assure the
performance of the ceremony which the world awaited at noon.
“There is an alternative,” he said, solemnly, “that would satisfy the
obligation honor puts upon you and at the same time leave inviolate
the sentiment you have just expressed.”
“An alternative?” she repeated, wondering.
“Yes. I will be satisfied if you become my wife only in name—in
the eyes of society, the Church, and the civil law.”
Hera understood as she had not until then how desperate was
the strait to which her refusal had brought him. For a moment she
did not answer the entreaty in his eyes. She walked to the open
window and looked out on the night. Tarsis had planned shrewdly in
keeping this for the last card to play. In her state of mind it was the
one appeal that could have the effect he desired. To Hera the offer
did seem the only way that remained of serving honor as well as
saving herself from what she contemplated as a loathsome
degradation. The inevitable misery of the sort of relation he proposed
rose before her mind; but of her happiness she thought no more, so
eager was she to mitigate in some degree the wrong of which she
perceived he must be the greater victim. Presently Tarsis was at her
side again, saying:
“Will you do this? Be my wife only in name. On these terms, if
you will, you may redeem your promise—you may save me.”
And wishing to do that—wishing to save him, to do him justice—
swayed, too, by pity for him and remorse for her broken promise,
and crushed in spirit by her disappointment in Mario—she yielded.
“There is no other way,” she said, turning to him, wearily—“no
other way to screen you—to meet the demand of honor.”
He caught up her hand and kissed it.
“You will never regret this act of justice,” he said, confident that
his complete triumph was only a matter of time. Perhaps he betrayed
the working of his mind in some unguarded gleam of the eye, some
play of the lip, for she said to him, her manner showing grave
determination:
“Don’t think I shall change—that you can swerve me in the least
from this position. You must foster no false hopes. When I become
your wife I shall remain to the last only that in appearance—in the
eyes of the world. In reality I shall be as far removed from you as if I
were actually married to another. I tell you this as emphatically as
possible, because it is only just that you clearly understand what our
marriage will mean to both.”
“All is quite clear,” Tarsis returned, cunningly.
“Oh, it is a terrible deed!” she exclaimed, the consequences rising
to her mind and filling it with horror. “Think well, I beg of you. In
despoiling me of my life’s happiness you are going to ruin your own.
Perhaps you did not think I should make the conditions so absolute,
so irrevocable. If you wish to withdraw your offer do so, and save us
from a lot that can not fail to be one of misery so long as we both are
alive.”
She had only multiplied his motives for wishing to make her his
wife. She understood him even less than he understood her. At no
time before had her beauty made such a living appeal to him. Until
now it had never been his privilege to behold her when emotion was
at play. Her outward image of loveliness was all she had ever
revealed to him. The voice she gave him in the past was not the
passionate one he had just heard; the soul her eyes had mirrored
was not the one that looked from them when she spoke the name of
Mario Forza. The heave of her bosom, the come and go of carnation
in her cheeks, the tides of tenderness that rose amid her promises of
a vehement strength, portrayed to him a Hera he had not known
before—a woman he would have given all his vast fortune to win.
“What you have said does not deter me,” he told her, “though I
apprehend the situation as fully as you wish me to. I accept.”
And thus the thread of the story took a new twist, but one of
which Aunt Beatrice never learned, nor did Don Riccardo.
CHAPTER VIII
A WEDDING JOURNEY
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com