(This page has no text content)
(This page has no text content)
PYTHON TOOLS FOR SCIENTISTS
(This page has no text content)
San Francisco P Y T H O N T O O L S F O R S C I E N T I S T S A n I n t r o d u c t i o n t o U s i n g A n a c o n d a , J u p y t e r L a b , a n d Py t h o n’s S c i e n t i f i c L i b r a r i e s by Lee Vaughan ®
PYTHON TOOLS FOR SCIENTISTS. Copyright © 2023 by Lee Vaughan. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. First printing 27 26 25 24 23 1 2 3 4 5 ISBN-13: 978-1-7185-0266-6 (print) ISBN-13: 978-1-7185-0267-3 (ebook) Published by No Starch Press ® , Inc. 245 8th Street, San Francisco, CA 94103 phone: +1.415.863.9900 www.nostarch.com; info@nostarch.com Publisher: William Pollock Managing Editor: Jill Franklin Production Manager: Sabrina Plomitallo-González Production Editor: Katrina Horlbeck Olsen Developmental Editor: Frances Saux Cover Illustrator: Gina Redman Interior Design: Octopod Studios Technical Reviewer: John Mayhew Production Services: Octal Publishing, LLC Library of Congress Control Number: 2022942882 For customer service inquiries, please contact info@nostarch.com. For information on distribution, bulk sales, corporate sales, or translations: sales@nostarch.com. For permission to translate this work: rights@nostarch.com. To report counterfeit copies or piracy: counterfeit@nostarch.com. No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. ®
This book is dedicated to the worldwide army of open source software developers. I am immensely grateful for your hard work and the immeasurable good it produces.
About the Author Lee Vaughan is a programmer, educator, and author of Impractical Python Projects (No Starch Press, 2019) and Real-World Python (No Starch Press, 2021). As an executive-level scientist at ExxonMobil, he constructed and reviewed computer models, developed and tested software, and trained geoscientists and engineers. His books are dedicated to helping self-learners develop and hone their Python skills and have fun doing it! About the Technical Reviewer John Mayhew is a geoscientist with an extensive background in math- ematics, data analysis, and scientific computing. He is a co-founder of the nonprofit organization Land of Jershon and currently serves on its board of directors and as the CEO. He has also established a charitable giving consultantship, East Gate Advocates, designed to connect donors with non- profit projects.
B R I E F C O N T E N T S Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii PART I: SETTING UP YOUR SCIENTIFIC CODING ENVIRONMENT . . . . . . . . .1 Chapter 1: Installing and Launching Anaconda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Chapter 2: Keeping Organized with Conda Environments . . . . . . . . . . . . . . . . . . . . . . . . 21 Chapter 3: Simple Scripting in the Jupyter Qt Console . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Chapter 4: Serious Scripting with Spyder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Chapter 5: Jupyter Notebook: An Interactive Journal for Computational Research . . . . . . . . 93 Chapter 6: JupyterLab: Your Center for Science . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 PART II: A PYTHON PRIMER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173 Chapter 7: Integers, Floats, and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Chapter 8: Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Chapter 9: The Container Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Chapter 10: Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Chapter 11: Functions and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Chapter 12: Files and Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Chapter 13: Object-Oriented programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 Chapter 14: Documenting Your Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
viii Brief Contents PART III: THE ANACONDA ECOSYSTEM . . . . . . . . . . . . . . . . . . . . . . . . .397 Chapter 15: The Scientific Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 Chapter 16: The InfoVis, SciVis, and Dashboarding Libraries . . . . . . . . . . . . . . . . . . . . . 419 Chapter 17: The GeoVis Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 PART IV: THE ESSENTIAL LIBRARIES . . . . . . . . . . . . . . . . . . . . . . . . . . . .489 Chapter 18: NumPy: Numerical Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491 Chapter 19: Demystifying Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537 Chapter 20: pandas, seaborn, and scikit-learn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583 Chapter 21: Managing Dates and Times with Python and pandas . . . . . . . . . . . . . . . . . . 625 Appendix: Answers to the “Test Your Knowledge” Challenges . . . . . . . . . . . . . . . . . . . . . 665 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685
C O N T E N T S I N D E T A I L ACKNOWLEDGMENTS XXI INTRODUCTION XXIII Why Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Navigating This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Part I: Setting Up Your Scientific Coding Environment . . . . . . . . . . . . . . . . . xxviii Part II: A Python Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxix Part III: The Anaconda Ecosystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx Part IV: The Essential Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxi Updates and Errata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxi Leaving Reviews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxi PART I: SETTING UP YOUR SCIENTIFIC CODING ENVIRONMENT 1 1 INSTALLING AND LAUNCHING ANACONDA 7 About Anaconda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Installing Anaconda on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Installing Anaconda on macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Installing Anaconda on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Getting to Know Anaconda Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Launching Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 The Home Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 The Environments Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 The Learning Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 The Community Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 File Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2 KEEPING ORGANIZED WITH CONDA ENVIRONMENTS 21 Understanding Conda Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Working with Conda Environments Using Navigator . . . . . . . . . . . . . . . . . . . . . . . . . 24 Launching Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Creating a New Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Managing Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Duplicating Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Backing Up Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Removing Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
x Contents in Detail Working with Conda Environments Using the Command Line Interface . . . . . . . . . . . . . 34 Launching the Command Line Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Creating a New Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Specifying an Environment’s Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Managing Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Duplicating and Sharing Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Restoring Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Removing Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Cleaning the Package Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3 SIMPLE SCRIPTING IN THE JUPYTER QT CONSOLE 49 Installing seaborn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Installing and Launching the Jupyter Qt Console Using Navigator . . . . . . . . . . . . . . . . 51 Installing and Launching the Jupyter Qt Console Using the CLI . . . . . . . . . . . . . . . . . . . 52 The Qt Console Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Choosing a Syntax Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Using Keyboard Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Using Tabs and Kernels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Printing and Saving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Multiline Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4 SERIOUS SCRIPTING WITH SPYDER 61 Installing and Launching Spyder with Anaconda Navigator . . . . . . . . . . . . . . . . . . . . . 62 Installing and Launching Spyder Using the CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Launching Spyder from the Start Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Configuring the Spyder Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Using Spyder with Environments and Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 The Naive Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 The Modular Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Using Project Files and Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Creating a Project in a New Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Creating a Project in an Existing Directory . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Using the Project Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 The Help Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 The IPython Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Using the Console for Output and Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Using Kernels with the Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Clearing the Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 The History Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Special Consoles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 The Editor Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Writing a Program Using the Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Defining Code Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Setting the Run Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Autocompleting Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 The Code Analysis Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Contents in Detail xi The Variable Explorer Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 The Profiler Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 The Debugger Pane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 5 JUPYTER NOTEBOOK: AN INTERACTIVE JOURNAL FOR COMPUTATIONAL RESEARCH 93 Installing Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 The Naive Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 The Modular Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Your First Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Creating Dedicated Project Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Navigating the Notebook Dashboard and User Interface . . . . . . . . . . . . . . . 100 Naming a Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Adding Text with a Markdown Cell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Adding Code and Making Plots with a Code Cell . . . . . . . . . . . . . . . . . . . . 104 Working with Output Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Adding an Image with a Markdown Cell . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Saving the Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Closing the Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Keyboard Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 The Command Palette . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Using Notebook Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Installing Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Enabling Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Working with Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Installing ipywidgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Creating Widgets with Interact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Creating Widgets with Interactive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Manually Creating Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Handling Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Customizing Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Embedding Widgets in Other Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Sharing Notebooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Checking and Running Notebooks with the Kernel Menu . . . . . . . . . . . . . . . 123 Downloading Notebooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Sharing Notebooks via GitHub and Gist . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Sharing Notebooks via Jupyter Notebook Viewer . . . . . . . . . . . . . . . . . . . . 128 Sharing Notebooks via Binder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Other Sharing Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Trusting Notebooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Turning Notebooks into Slideshows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Installing the RISE Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Creating a Slideshow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Using Speaker Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
xii Contents in Detail 6 JUPYTERLAB: YOUR CENTER FOR SCIENCE 139 When to Use JupyterLab Instead of Notebook? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Installing JupyterLab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 The Naive Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 The Modular Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Building a 3D Astronomical Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Using Dedicated Project Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 The JupyterLab Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 The Menu Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 The Left Sidebar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Creating a New Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Naming the Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Using Markdown Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Adding Code and Making Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Adding a Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Displaying an Image File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Exploring the Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Opening Multiple Notebooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Saving the Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Clearing the Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Closing the Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Taking Advantage of the JupyterLab Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Creating Synchronized Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Copying Cells Between Notebooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Staying Focused by Using Single Document Mode . . . . . . . . . . . . . . . . . . . 160 Using the Text Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Running a Script in a Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Running a Script in a Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Simultaneously Writing and Documenting Code . . . . . . . . . . . . . . . . . . . . . 164 Using JupyterLab Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Installing and Managing Extensions with the Extension Manager . . . . . . . . . 166 Installing and Managing Extensions Using the CLI . . . . . . . . . . . . . . . . . . . . 169 Installing ipywidgets for JupyterLab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Creating Custom Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 PART II: A PYTHON PRIMER 173 7 INTEGERS, FLOATS, AND STRINGS 175 Mathematical Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Mathematical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 The Assignment Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Augmented Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 The math Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Contents in Detail xiii Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Accessing the Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Floats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 8 VARIABLES 201 Variables Have Identities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Assigning Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Using Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Chained Assignment and Internment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Using f-Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Naming Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Reserved Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Variables Are Case Sensitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Best Practices for Naming Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Managing Dynamic Typing Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Handling Insignificant Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Getting User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Using Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 9 THE CONTAINER DATA TYPES 219 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Creating Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Converting Other Types to Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Working with Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Creating Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Working with Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Creating Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Working with Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Creating Frozensets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Creating Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Combining Two Sequences into a Dictionary . . . . . . . . . . . . . . . . . . . . . . . 248 Creating Empty Dictionaries and Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Working with Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
xiv Contents in Detail 10 FLOW CONTROL 257 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Working with Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 Using the else and elif Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Using Ternary Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Using Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Loop Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Replacing Loops with Comprehensions . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Using try and except . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Forcing Exceptions with the raise Keyword . . . . . . . . . . . . . . . . . . . . . . . . . 276 Ignoring Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Tracing Execution with Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 11 FUNCTIONS AND MODULES 283 Defining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Using Parameters and Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Positional and Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Using Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Returning Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Naming Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Functions and the Flow of Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Using Namespaces and Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 Using Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 Using a main() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Advanced Function Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Designing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 Lambda Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Importing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Inspecting Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 Writing Your Own Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Naming Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 Writing Modules That Work in Stand-Alone Mode . . . . . . . . . . . . . . . . . . . . 310 Built-in Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Contents in Detail xv 12 FILES AND FOLDERS 315 Creating a New Spyder Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 Working with Directory Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 The Operating System Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Absolute vs . Relative Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 The pathlib Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 The Shell Utilities Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Working with Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 Reading a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 Closing Files Using the with Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 Writing to a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 Reading and Writing Text Files Using pathlib . . . . . . . . . . . . . . . . . . . . . . . 332 Working with Complex Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Pickling Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 Shelving Pickled Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 Storing Data with JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Catching Exceptions When Opening Files . . . . . . . . . . . . . . . . . . . . . . . . . 342 Other Storage Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 13 OBJECT-ORIENTED PROGRAMMING 347 When to Use OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 Creating a New Spyder Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 Defining the Frigate Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Defining Instance Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 Instantiating Objects and Calling Instance Methods . . . . . . . . . . . . . . . . . . . 353 Defining a Guided-Missile Frigate Class Using Inheritance . . . . . . . . . . . . . . . . . . . . 355 Instantiating a New Guided-Missile Frigate Object . . . . . . . . . . . . . . . . . . . 357 Using the super() Function for Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Objects Within Objects: Defining the Fleet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Reducing Code Redundancy with Dataclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Using Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362 Defining the Ship Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 Identifying Friend or Foe with Fields and Post-Init Processing . . . . . . . . . . . . . 370 Optimizing Dataclasses with __slots__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 Making a Class Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 14 DOCUMENTING YOUR WORK 377 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 Single-Line Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 Multiline Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 Inline Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 Commenting-Out Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
xvi Contents in Detail Docstrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Documenting Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 Documenting Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386 Documenting Functions and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 Keeping Docstrings Up to Date with doctest . . . . . . . . . . . . . . . . . . . . . . . . 388 Checking Docstrings in the Spyder Code Analysis Pane . . . . . . . . . . . . . . . . 391 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396 PART III: THE ANACONDA ECOSYSTEM 397 15 THE SCIENTIFIC LIBRARIES 399 The SciPy Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 NumPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 SciPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 SymPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402 pandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 A General Machine Learning Library: scikit-learn . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 The Deep Learning Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 TensorFlow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 Keras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 PyTorch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408 The Computer Vision Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 scikit-image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 PIL/Pillow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 The Natural Language Processing Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 NLTK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 spaCy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412 The Helper Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 Beautiful Soup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414 Regex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 Dask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 16 THE INFOVIS, SCIVIS, AND DASHBOARDING LIBRARIES 419 InfoVis and SciVis Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422 seaborn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424 The pandas Plotting API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428 Altair . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 Bokeh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430 Plotly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 HoloViews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 Datashader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 Mayavi and ParaView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Contents in Detail xvii Dashboards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 Dash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 Streamlit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 Voilà . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 Choosing a Plotting Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 Size of Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451 Types of Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452 Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452 Versatility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 Maturity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 Making the Final Choice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456 17 THE GEOVIS LIBRARIES 457 The Geospatial Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 GeoPandas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 Cartopy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464 Geoplot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465 Plotly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467 folium . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470 ipyleaflet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 GeoViews: The HoloViz Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 KeplerGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479 pydeck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 Bokeh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 Choosing a GeoVis Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487 PART IV: THE ESSENTIAL LIBRARIES 489 18 NUMPY: NUMERICAL PYTHON 491 Introducing the Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492 Describing Arrays Using Dimension and Shape . . . . . . . . . . . . . . . . . . . . . 492 Creating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494 Accessing Array Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504 Indexing and Slicing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506 Manipulating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518 Shaping and Transposing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518 Joining Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 Splitting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522 Doing Math Using Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524 Vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524 Broadcasting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526 The Matrix Dot Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527 Incrementing and Decrementing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 528 Using NumPy Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
xviii Contents in Detail Reading and Writing Array Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536 19 DEMYSTIFYING MATPLOTLIB 537 Anatomy of a Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538 The pyplot and Object-Oriented Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539 Using the pyplot Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540 Creating and Manipulating Plots with pyplot Methods . . . . . . . . . . . . . . . . . 542 Working with Subplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545 Building Multipanel Displays Using GridSpec . . . . . . . . . . . . . . . . . . . . . . . 549 Using the Object-Oriented Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555 Creating and Manipulating Plots with the Object-Oriented Style . . . . . . . . . . 557 Working with Subplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561 Building Multipanel Displays Using GridSpec . . . . . . . . . . . . . . . . . . . . . . . 564 Insetting Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567 Plotting in 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 Animating Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569 Styling Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 Changing Runtime Configuration Parameters . . . . . . . . . . . . . . . . . . . . . . . 574 Creating and Using a Style File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576 Applying Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580 20 PANDAS, SEABORN, AND SCIKIT-LEARN 583 Introducing the pandas Series and DataFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584 The Series Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584 The DataFrame Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585 The Palmer Penguins Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586 The Project Outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587 Setting Up the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587 Importing Packages and Setting Up the Display . . . . . . . . . . . . . . . . . . . . . 589 Loading the Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 Displaying the DataFrame and Renaming Columns . . . . . . . . . . . . . . . . . . . 590 Checking for Duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592 Handling Missing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592 Exploring the Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596 Predicting Penguin Species Using K-Nearest Neighbors . . . . . . . . . . . . . . . . 609 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623 21 MANAGING DATES AND TIMES WITH PYTHON AND PANDAS 625 Python datetime Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626 Getting the Current Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626 Assigning Timestamps and Calculating Time Delta . . . . . . . . . . . . . . . . . . . . 627 Formatting Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628 Converting Strings to Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630 Plotting with datetime Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632 Creating Naive vs . Aware Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Comments 0
Loading comments...
Reply to Comment
Edit Comment