SciPy and NumPy Eli Bressert Beijing•Cambridge•Farnham•Ko¨ln•Sebastopol•Tokyo 9781449305468_text.pdf 1 10/31/12 2:35 PM SciPyandNumPy byEliBressert Copyright©2013EliBressert.Allrightsreserved. PrintedintheUnitedStatesofAmerica. PublishedbyO’ReillyMedia,Inc.,1005GravensteinHighwayNorth,Sebastopol,CA95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editionsarealsoavailableformosttitles(http://my.safaribooksonline.com).Formoreinformation, contactourcorporate/institutionalsalesdepartment:(800)[email protected]. InteriorDesigner: DavidFutato ProjectManager: PaulC.Anagnostopoulos CoverDesigner: RandyComer Copyeditor: MaryEllenN.Oliver Editors: RachelRoumeliotis, Proofreader: RichardCamp MeghanBlanchette Illustrators: EliBressert,LaurelMuller ProductionEditor: HollyBauer November2012: Firstedition RevisionHistoryfortheFirstEdition: 2012-10-31 Firstrelease Seehttp://oreilly.com/catalog/errata.csp?isbn=0636920020219forreleasedetails. NutshellHandbook,theNutshellHandbooklogo,andtheO’Reillylogoareregisteredtrademarks ofO’ReillyMedia,Inc.SciPyandNumPy,theimageofathree-spinedstickleback,andrelatedtrade dressaretrademarksofO’ReillyMedia,Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimedastrademarks.Wherethosedesignationsappearinthisbook,andO’ReillyMedia,Inc., wasawareofatrademarkclaim,thedesignationshavebeenprintedincapsorinitialcaps. Whileeveryprecautionhasbeentakeninthepreparationofthisbook,thepublisherandauthors assume no responsibility for errors or omissions, or for damages resulting from the use of the informationcontainedherein. ISBN:978-1-449-30546-8 [LSI] 9781449305468_text.pdf 2 10/31/12 2:35 PM Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 WhySciPyandNumPy? 1 1.2 GettingNumPyandSciPy 2 1.3 WorkingwithSciPyandNumPy 3 2. NumPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.1 NumPyArrays 5 2.2 BooleanStatementsandNumPyArrays 10 2.3 ReadandWrite 12 2.4 Math 14 3. SciPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.1 OptimizationandMinimization 17 3.2 Interpolation 22 3.3 Integration 26 3.4 Statistics 28 3.5 SpatialandClusteringAnalysis 32 3.6 SignalandImageProcessing 38 3.7 SparseMatrices 40 3.8 ReadingandWritingFilesBeyondNumPy 41 4. SciKit:TakingSciPyOneStepFurther. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 4.1 Scikit-Image 43 4.2 Scikit-Learn 48 5. Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 5.1 Summary 55 5.2 What’sNext? 55 iii 9781449305468_text.pdf 3 10/31/12 2:35 PM 9781449305468_text.pdf 4 10/31/12 2:35 PM Preface Python,ahigh-levellanguagewitheasy-to-readsyntax,ishighlyflexible,whichmakes itanideallanguagetolearnanduse.ForscienceandR&D,afewextrapackagesareused tostreamlinethedevelopmentprocessandobtaingoalswiththefeweststepspossible. Among the best of these are SciPy and NumPy. This book gives a brief overview of differenttoolsinthesetwoscientificpackages,inordertojumpstarttheiruseinthe reader’sownresearchprojects. NumPy and SciPy are the bread-and-butter Python extensions for numerical arrays andadvanceddataanalysis.Hence,knowingwhattoolstheycontainandhowtouse themwillmakeanyprogrammer’slifemoreenjoyable.Thisbookwillcovertheiruses, rangingfromsimplearraycreationtomachinelearning. Audience Anyonewithbasic(andupward)knowledgeofPythonisthetargetedaudienceforthis book.AlthoughthetoolsinSciPyandNumPyarerelativelyadvanced,usingthemis simpleandshouldkeepevenanovicePythonprogrammerhappy. ContentsofthisBook This book covers the basics of SciPy and NumPy with some additional material. The first chapter describes what the SciPy and NumPy packages are, and how to accessandinstallthemonyourcomputer.Chapter2goesoverthebasicsofNumPy, startingwitharraycreation.Chapter3,whichcomprisesthebulkofthebook,covers asmallsampleofthevoluminousSciPytoolbox.Thischapterincludesdiscussionand examples on integration, optimization, interpolation, and more. Chapter 4 discusses two well-known scikit packages: scikit-image and scikit-learn. These provide much more advanced material that can be immediately applied to real-world problems. In Chapter5,theconclusion,wediscusswhattodonextforevenmoreadvancedmaterial. v 9781449305468_text.pdf 5 10/31/12 2:35 PM ConventionsUsedinThisBook Thefollowingtypographicalconventionsareusedinthisbook: Plaintext Indicates menu titles, menu options, menu buttons, and keyboard accelerators (suchasAltandCtrl). Italic Indicatesnewterms,URLs,emailaddresses,filenames,fileextensions,pathnames, directories,andUnixutilities. Constantwidth Indicatescommands,options,switches,variables,attributes,keys,functions,types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, XML tags, HTML tags, macros, the contents of files, or theoutputfromcommands. Thisiconsignifiesatip,suggestion,orgeneralnote. Thisiconindicatesawarningorcaution. UsingCodeExamples Thisbookisheretohelpyougetyourjobdone.Ingeneral,youmayusethecodein this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission.SellingordistributingaCD-ROMofexamplesfromO’Reillybooksdoes require permission. Answering a question by citing this book and quoting example codedoesnotrequirepermission.Incorporatingasignificantamountofexamplecode fromthisbookintoyourproduct’sdocumentationdoesrequirepermission. Weappreciate,butdonotrequire,attribution.Anattributionusuallyincludesthetitle, author,publisher,andISBN.Forexample:“SciPyandNumPybyEliBressert(O’Reilly). Copyright2013EliBressert,978-1-449-30546-8.” Ifyoufeelyouruseofcodeexamplesfallsoutsidefairuseorthepermissiongivenabove, [email protected]. We’dLiketoHearfromYou Pleaseaddresscommentsandquestionsconcerningthisbooktothepublisher: vi | Preface 9781449305468_text.pdf 6 10/31/12 2:35 PM O’ReillyMedia,Inc. 1005GravensteinHighwayNorth Sebastopol,CA95472 (800)998-9938(intheUnitedStatesorCanada) (707)829-0515(internationalorlocal) (707)829-0104(fax) Wehaveawebpageforthisbook,wherewelisterrata,examples,linkstothecodeand datasetsused,andanyadditionalinformation.Youcanaccessthispageat: http://oreil.ly/SciPy_NumPy Tocommentorasktechnicalquestionsaboutthisbook,sendemailto: [email protected] Formoreinformationaboutourbooks,courses,conferences,andnews,seeourwebsite athttp://www.oreilly.com. FindusonFacebook:http://facebook.com/oreilly FollowusonTwitter:http://twitter.com/oreillymedia WatchusonYouTube:http://www.youtube.com/oreillymedia Safari® BooksOnline SafariBooksOnline(www.safaribooksonline.com)isanon-demanddigital librarythatdeliversexpertcontentinbothbookandvideoformfromthe world’sleadingauthorsintechnologyandbusiness. Technology professionals, software developers, web designers, and business and cre- ative professionals use Safari Books Online as their primary resource for research, problemsolving,learning,andcertificationtraining. SafariBooksOnlineoffersarangeofproductmixesandpricingprogramsfororgani- zations,governmentagencies,andindividuals.Subscribershaveaccesstothousandsof books, trainingvideos, andprepublicationmanuscriptsinonefullysearchabledata- basefrompublisherslikeO’ReillyMedia,PrenticeHallProfessional,Addison-Wesley Professional,MicrosoftPress,Sams,Que,PeachpitPress,FocalPress,CiscoPress,John Wiley&Sons,Syngress,MorganKaufmann,IBMRedbooks,Packt,AdobePress,FT Press,Apress,Manning,NewRiders,McGraw-Hill,Jones&Bartlett,CourseTechnol- ogy,anddozensmore.FormoreinformationaboutSafariBooksOnline,pleasevisitus online. Acknowledgments I would like to thank Meghan Blanchette and Julie Steele, my current and previous editors, for their patience, help, and expertise. This book wouldn’t have materialized withouttheirassistance.Thetips,warnings,andpackagetoolsdiscussedinthebook Preface | vii 9781449305468_text.pdf 7 10/31/12 2:35 PM were much improved thanks to the two book reviewers: Tom Aldcroft and Sarah Kendrew.Colleaguesandfriendsthathavehelpeddiscusscertainaspectsofthisbook and bolstered my drive to get it done are Leonardo Testi, Nate Bastian, Diederik Kruijssen, Joao Alves, Thomas Robitaille, and Farida Khatchadourian. A big thanks goes to my wife and son, Judith van Raalten and Taj Bressert, for their help and inspiration,andwillingnesstodealwithmebeinghuddledawaybehindthecomputer forendlesshours. viii | Preface 9781449305468_text.pdf 8 10/31/12 2:35 PM