Download Complete Introduction to python for econometrics, statistics and data analysis kevin sheppa

Page 1


Introduction to Python for Econometrics,

Statistics and Data Analysis Kevin Sheppard

Visit to download the full and correct content document: https://ebookmass.com/product/introduction-to-python-for-econometrics-statistics-and -data-analysis-kevin-sheppard/

More products digital (pdf, epub, mobi) instant download maybe you interests ...

Introduction to Python for Econometrics, Statistics and Data Analysis. 5th Edition Kevin Sheppard.

https://ebookmass.com/product/introduction-to-python-foreconometrics-statistics-and-data-analysis-5th-edition-kevinsheppard/

Introduction to Statistics and Data Analysis 6th Edition Roxy Peck

https://ebookmass.com/product/introduction-to-statistics-anddata-analysis-6th-edition-roxy-peck/

Statistics and Data Analysis for Nursing Research (2nd Edition ) 2nd Edition

https://ebookmass.com/product/statistics-and-data-analysis-fornursing-research-2nd-edition-2nd-edition/

Introduction to Research Methods and Data Analysis in Psychology Darren Langdridge

https://ebookmass.com/product/introduction-to-research-methodsand-data-analysis-in-psychology-darren-langdridge/

Introduction to Econometrics 3rd Edition James H. Stock

https://ebookmass.com/product/introduction-to-econometrics-3rdedition-james-h-stock/

Introduction to Econometrics, Global Edition James H. Stock

https://ebookmass.com/product/introduction-to-econometricsglobal-edition-james-h-stock/

Introduction to Linear Regression Analysis (Wiley Series in Probability and Statistics) 6th Edition Montgomery

https://ebookmass.com/product/introduction-to-linear-regressionanalysis-wiley-series-in-probability-and-statistics-6th-editionmontgomery/

An Introduction to Statistical Methods and Data Analysis 7th Edition, (Ebook PDF)

https://ebookmass.com/product/an-introduction-to-statisticalmethods-and-data-analysis-7th-edition-ebook-pdf/

Enzymes. A Practical Introduction to Structure, Mechanism, and Data Analysis 3rd Edition Robert A. Copeland

https://ebookmass.com/product/enzymes-a-practical-introductionto-structure-mechanism-and-data-analysis-3rd-edition-robert-acopeland/

3rdEdition,1stRevision

KevinSheppard UniversityofOxford

Monday9th September,2019

ChangessincetheThirdEdition

• Verifiedthatallcodeandexamplesworkcorrectlyagainst2019versionsofmodules.Thenotable packagesandtheirversionsare:

– Python3.7(Preferredversion)

– NumPy:1.16

– SciPy:1.3

– pandas:0.25

– matplotlib:3.1

• Python2.7supporthasbeenofficiallydropped,althoughmostexamplescontinuetoworkwith2.7. DonotPython2.7in2019fornumericalcode.

• Smalltypofixes,thankstoMartonHuebler.

• FixeddirectdownloadofFREDdataduetoAPIchanges,thankstoJesperTermansen.

• ThanksforBillTubbsforadetailedreadandmultipletyporeports.

• Updatedtochangesinlineprofiler(seeCh. 24)

• Updateddeprecationsinpandas.

• Removed hold fromplottingchaptersincethisisnolongerrequired.

• ThanksforGenLiformultipletyporeports.

• TestedallcodeonPyton3.6.Codehasbeentestedagainstthecurrentsetofmodulesinstalledby condaasofFebruary2018.Thenotablepackagesandtheirversionsare:

– NumPy:1.13

– Pandas:0.22

Notestothe3rd Edition

Thiseditionincludesthefollowingchangesfromthesecondedition(August2014):

• RewritteninstallationsectionfocusedexclusivelyonusingContinuum’sAnaconda.

• Python3.5isthedefaultversionofPythoninsteadof2.7.Python3.5(ornewer)iswellsupportedby thePythonpackagesrequiredtoanalyzedataandperformstatisticalanalysis,andbringsomenew usefulfeatures,suchasanewoperatorformatrixmultiplication(@).

• Removeddistinctionbetweenintegersandlongsinbuilt-indatatypeschapter.Thisdistinctionis onlyrelevantforPython2.7.

• dot hasbeenremovedfrommostexamplesandreplacedwith @ toproducemorereadablecode.

• SplitCythonandNumbaintoseparatechapterstohighlighttheimprovedcapabilitiesofNumba.

• VerifiedallcodeworkingoncurrentversionsofcorelibrariesusingPython3.5.

• pandas

– Updatedsyntaxofpandasfunctionssuchas resample.

– Addedpandas Categorical

– Expandedcoverageofpandas groupby.

– Expandedcoverageofdateandtimedatatypesandfunctions.

• Newchapterintroducingstatsmodels,apackagethatfacilitatesstatisticalanalysisofdata.statsmodelsincludesregressionanalysis,GeneralizedLinearModels(GLM)andtime-seriesanalysisusing ARIMAmodels.

ChangessincetheSecondEdition

• Fixedtyposreportedbyareader–thankstoIlyaSorvachev

• CodeverifiedagainstAnaconda2.0.1.

• AddeddiagnostictoolsandasimplemethodtouseexternalcodeintheCythonsection.

• UpdatedtheNumbasectiontoreflectrecentchanges.

• FixedsometyposinthechapteronPerformanceandOptimization.

• AddedexamplesofjoblibandIPython’sclustertothechapteronrunningcodeinparallel.

• Newchapterintroducingobject-orientedprogrammingasamethodtoprovidestructureandorganizationtorelatedcode.

• Addedseaborntotherecommendedpackagelist,andhaveincludeditbedefaultinthegraphics chapter.

• BasedonexperienceteachingPythontoeconomicsstudents,therecommendedinstallationhas beensimplifiedbyremovingthesuggestiontousevirtualenvironment.Thediscussionofvirtual environmentsasbeenmovedtotheappendix.

• Rewrotepartsofthepandaschapter.

• ChangedtheAnacondainstalltousebothcreateandinstall,whichshowshowtoinstalladditional packages.

• Fixedsomemissingpackagesinthedirectinstall.

• ChangedtheconfigurationofIPythontoreflectbestpractices.

• AddedsubsectioncoveringIPythonprofiles.

• SmallsectionaboutSpyderasagoodstartingIDE.

Notestothe2nd Edition

Thiseditionincludesthefollowingchangesfromthefirstedition(March2012):

• ThepreferredinstallationmethodisnowContinuumAnalytics’Anaconda.Anacondaisacomplete scientificstackandisavailableforallmajorplatforms.

• Newchapteronpandas.pandasprovidesasimplebutpowerfultooltomanagedataandperform preliminaryanalysis.Italsogreatlysimplifiesimportingandexportingdata.

• Newchapteronadvancedselectionofelementsfromanarray.

• Numbaprovidesjust-in-timecompilationfornumericPythoncodewhichoftenproduceslargeperformancegainswhenpureNumPysolutionsarenotavailable(e.g.loopingcode).

• Dictionary,setandtuplecomprehensions

• Numeroustypos

• AllcodehasbeenverifiedworkingagainstAnaconda1.7.0.

Chapter1 Introduction

1.1 Background

ThesenotesaredesignedforsomeonenewtostatisticalcomputingwishingtodevelopasetofskillsnecessarytoperformoriginalresearchusingPython.Theyshouldalsobeusefulforstudents,researchersor practitionerswhorequireaversatileplatformforeconometrics,statisticsorgeneralnumericalanalysis (e.g.numericsolutionstoeconomicmodelsormodelsimulation).

Pythonisapopulargeneral–purposeprogramminglanguagethatiswellsuitedtoawiderangeofproblems.1 RecentdevelopmentshaveextendedPython’srangeofapplicabilitytoeconometrics,statistics,and generalnumericalanalysis.Python–withtherightsetofadd-ons–iscomparabletodomain-specific languagessuchasR,MATLABorJulia.IfyouarewonderingwhetheryoushouldbotherwithPython(or anotherlanguage),anincompletelistofconsiderationsincludes:

YoumightwanttoconsiderRif:

• Youwanttoapplystatisticalmethods.ThestatisticslibraryofRissecondtonone,andRisclearly attheforefrontofnewstatisticalalgorithmdevelopment–meaningyouaremostlikelytofindthat new(ish)procedureinR.

• Performanceisofsecondaryimportance.

• Freeisimportant.

YoumightwanttoconsiderMATLABif:

• Commercialsupportandaclearchanneltoreportissuesisimportant.

• Documentationandorganizationofmodulesaremoreimportantthanthebreadthofalgorithms available.

• Performanceisanimportantconcern.MATLABhasoptimizations,suchasJust-in-Time(JIT)compilationofloops,whichisnotautomaticallyavailableinmostotherpackages.

YoumightwanttoconsiderJuliaif:

1Accordingtotherankingon http://www.tiobe.com/tiobe-index/,Pythonisthe5th mostpopularlanguage. http:// langpop.corger.nl/ ranksPythonas4th or5th .

• Performanceinaninteractivebasedlanguageisyourmostimportantconcern.

• Youdon’tmindlearningenoughPythontointerfacewithPythonpackages.TheJuliaecosystemis initsinfancyandabridgetoPythonisusedtoprovideimportantmissingfeatures.

• Youlikelivingonthebleedingedgeandaren’tworriedaboutcodebreakingacrossnewversionsof Julia.

• Youliketodomostthingsyourself.

Havingreadthereasonstochooseanotherpackage,youmaywonderwhyyoushouldconsiderPython.

• Youneedalanguagewhichcanactasanend-to-endsolutionthatallowsaccesstoweb-basedservices,databaseservers,datamanagementandprocessingandstatisticalcomputation.Pythoncan evenbeusedtowriteserver-sideappssuchasadynamicwebsite(seee.g. http://stackoverflow. com),appsfordesktop-classoperatingsystemswithgraphicaluserinterfaces,orappsfortabletsand phonesapps(iOSandAndroid).

• Datahandlingandmanipulation–especiallycleaningandreformatting–isanimportantconcern. PythonissubstantiallymorecapableatdatasetconstructionthaneitherRorMATLAB.

• Performanceisaconcern,butnotatthetopofthelist.2

• Freeisanimportantconsideration–Pythoncanbefreelydeployed,evento100sofserversinona cloud-basedcluster(e.g.AmazonWebServices,GoogleComputeorAzure).

• KnowledgeofPython,asageneralpurposelanguage,iscomplementarytoR/MATLAB/Julia/Ox/GAUSS/Stata.

1.2 Conventions

Thesenoteswillfollowtwoconventions.

1. Codeblockswillbeusedthroughout.

"""A docstring

# Comments appear in a different color

# Reserved keywords are highlighted and as assert break class continue def del elif else except exec finally for from global if import in is lambda not or pass print raise return try while with yield

# Common functions and classes are highlighted in a # different color. Note that these are not reserved,

2PythonperformancecanbemadearbitrarilyclosetoCusingavarietyofmethods,includingNumba(purepython),Cython (C/Pythoncreolelanguage)ordirectlycallingCcode.Moreover,recentadvanceshavesubstantiallyclosedthegapwithrespect tootherJust-in-TimecompiledlanguagessuchasMATLAB.

# and can be used although best practice would be # to avoid them if possible arraymatrix range list True False None

# Long lines are indented

some_text= 'This is a very, very, very, very, very, very, very, very, very, very, very, very long line '

2. Whenacodeblockcontains >>>,thisindicatesthatthecommandisrunninganinteractiveIPython session.Outputwilloftenappearaftertheconsolecommand,andwill not beprecededbyacommandindicator.

>>>x=1.0

>>>x+2

3.0

Ifthecodeblockdoesnotcontaintheconsolesessionindicator,thecodecontainedintheblockis intendedtobeexecutedinastandalonePythonfile.

import numpy as np

x=np.array([1,2,3,4])

y=np.sum(x) print(x) print(y)

1.3 ImportantComponentsofthePythonScientificStack

1.3.1 Python

Python3.5(orlater)isrequired.ThisprovidesthecorePythoninterpreter.Mostoftheexamplesshould workwiththelatestversionofPython2.7aswell.

1.3.2 NumPy

NumPyprovidesasetofarrayandmatrixdatatypeswhichareessentialforstatistics,econometricsand dataanalysis.

1.3.3 SciPy

SciPycontainsalargenumberofroutinesneededforanalysisofdata.Themostimportantincludeawide rangeofrandomnumbergenerators,linearalgebraroutines,andoptimizers.SciPydependsonNumPy.

1.3.4 JupyterandIPython

IPythonprovidesaninteractivePythonenvironmentwhichenhancesproductivitywhendevelopingcode orperforminginteractivedataanalysis.JupyterprovidesagenericsetofinfrastructurethatenablesIPython toberuninavarietyofsettingsincludinganimprovedconsole(QtConsole)orinaninteractivewebbrowserbasednotebook.

1.3.5 matplotlibandseaborn

matplotlibprovidesaplottingenvironmentfor2Dplots,withlimitedsupportfor3Dplotting.seabornis aPythonpackagethatimprovesthedefaultappearanceofmatplotlibplotswithoutanyadditionalcode.

1.3.6 pandas pandasprovideshigh-performancedatastructures.

1.3.7 statsmodels

statsmodelsispandas-awareandprovidesmodelsusedinthestatisticalanalysisofdataincludinglinear regression,GeneralizedLinearModels(GLMs),andtime-seriesmodels(e.g.,ARIMA).

1.3.8 PerformanceModules

Anumberofmodulesareavailabletohelpwithperformance.TheseincludeCythonandNumba.Cython isaPythonmodulewhichfacilitatesusingasimplePython-derivedcreoletowritefunctionsthatcanbe compiledtonative(Ccode)Pythonextensions.Numbausesamethodofjust-in-timecompilationto translateasubsetofPythontonativecodeusingLow-LevelVirtualMachine(LLVM).

1.4 Setup

TherecommendedmethodtoinstallthePythonscientificstackistouseContinuumAnalytics’Anaconda. Appendix 1.A.3 describesamorecomplexinstallationprocedurewithinstructionsfordirectlyinstalling PythonandtherequiredmoduleswhenitisnotpossibletoinstallAnaconda.

ContinuumAnalytics’Anaconda

Anaconda,afreeproductofContinuumAnalytics(www.continuum.io),isavirtuallycompletescientific stackforPython.ItincludesboththecorePythoninterpreterandstandardlibrariesaswellasmostmodulesrequiredfordataanalysis.Anacondaisfreetouseandmodulesforacceleratingtheperformanceof linearalgebraonIntelprocessorsusingtheMathKernelLibrary(MKL)areprovided.ContinuumAnalyticsalsoprovidesotherhigh-performancemodulesforreadinglargedatafilesorusingtheGPUtofurther accelerateperformanceforanadditional,modestcharge.Mostimportantly,installationisextraordinarily easyonWindows,Linux,andOSX.Anacondaisalsosimpletoupdatetothelatestversionusing

condaupdateconda condaupdateanaconda

Windows

InstallationonWindowsrequiresdownloadingtheinstallerandrunning.AnacondacomesinbothPython 2.7and3.xflavors,andthelatestPython3.xisthepreferredchoice.TheseinstructionsuseANACONDA toindicatetheAnacondainstallationdirectory(e.g.thedefaultisC:\Anaconda).Oncethesetuphas completed,openacommandprompt(cmd.exe)andrun

cd ANACONDA\Scripts condaupdateconda condaupdateanaconda condainstallhtml5libseaborn

whichwillfirstensurethatAnacondaisup-to-date. condainstall canbeusedlatertoinstallotherpackagesthatmaybeofinterest.NotethatifAnacondaisinstalledintoadirectoryotherthanthedefault,the fullpathshouldnotcontainUnicodecharactersorspaces.

Notes

TherecommendedsettingsforinstallingAnacondaonWindowsare:

• Installforallusers,whichrequiresadminprivileges.Ifthesearenotavailable,thenchoosethe“Just forme”option,butbeawareofinstallingonapaththatcontainsnon-ASCIIcharacterswhichcan causeissues.

• AddAnacondatotheSystemPATH-ThisisimportanttoensurethatAnacondacommandscanbe runfromthecommandprompt.

• RegisterAnacondaasthesystemPythonunlessyouhaveaspecificreasonnotto(unlikely).

IfAnacondaisnotaddedtothesystempath,itisnecessarytoaddthe ANACONDA and ANACONDA\Scripts directoriestothePATHusing

set PATH=ANACONDA;ANACONDA\Scripts;%PATH% beforerunningPythonprograms.

LinuxandOSX

InstallationonLinuxrequiresexecuting bashAnaconda3-x.y.z-Linux-ISA.sh where x.y.z willdependontheversionbeinginstalledand ISA willbeeitherx86ormorelikelyx86_64. AnacondacomesinbothPython2.7and3.xflavors,andthelatestPython3.xisthepreferredchoice.The OSXinstallerisavailableeitherinaGUIinstalled(pkgformat)orasabashinstallerwhichisinstalled inanidenticalmannertotheLinuxinstallation.Itisstronglyrecommendedthattheanaconda/binis prependedtothepath.Thiscanbeperformedinasession-by-sessionbasisbyentering export PATH=ANACONDA/bin;$PATH

OnLinuxthischangecanbemadepermanentbyenteringthislinein .bashrc whichisahiddenfilelocated in ~/.OnOSX,thislinecanbeaddedto .bash_profile whichislocatedinthehomedirectory(~/).3 Afterinstallationcompletes,execute condaupdateconda condaupdateanaconda condainstallhtml5libseaborn

3Usetheappropriatesettingsfileifusingadifferentshell(e.g. .zshrc forzsh).

whichwillfirstensurethatAnacondaisup-to-dateandthentoinstalltheIntelMathKernellibrary-linked modules,whichprovidesubstantialperformanceimprovements–thispackagerequiresalicensewhich isfreetoacademicusersandlowcosttoothers.Ifacquiringalicenseisnotpossible,omitthisline. condainstall canbeusedlatertoinstallotherpackagesthatmaybeofinterest.

Notes

AllinstructionsforOSXandLinuxassumethat ANACONDA/bin hasbeenaddedtothepath.Ifthisisnot thecase,itisnecessarytorun

cd ANACONDA cd bin

andthenallcommandsmustbeprependedbya . asin

./condaupdateconda

1.5 UsingPython

PythoncanbeprogrammedusinganinteractivesessionusingIPythonorbydirectlyexecutingPython scripts–textfilesthatendwiththeextension.py–usingthePythoninterpreter.

1.5.1 PythonandIPython

Mostofthisintroductionfocusesoninteractiveprogramming,whichhassomedistinctadvantageswhen learningalanguage.ThestandardPythoninteractiveconsoleisverybasicanddoesnotsupportuseful featuressuchastabcompletion.IPython,andespeciallytheQtConsoleversionofIPython,transforms theconsoleintoahighlyproductiveenvironmentwhichsupportsanumberofusefulfeatures:

• Tabcompletion-Afterentering1ormorecharacters,pressingthetabbuttonwillbringupalistof functions,packages,andvariableswhichmatchthetypedtext.Ifthelistofmatchesislarge,pressing tabagainallowsthearrowkeyscanbeusedtobrowseandselectacompletion.

• “Magic”functionwhichmaketaskssuchasnavigatingthelocalfilesystem(using %cd~/directory/ orjust cd~/directory/ assumingthat %automagic ison)orrunningotherPythonprograms(using run program.py)simple.Entering %magic insideandIPythonsessionwillproduceadetaileddescription oftheavailablefunctions.Alternatively, %lsmagic producesasuccinctlistofavailablemagiccommands.Themostusefulmagicfunctionsare

– cd -changedirectory

– edit filename -launchaneditortoedit filename

– ls or ls pattern -listthecontentsofadirectory

– run filename-runthePythonfile filename

– timeit -timetheexecutionofapieceofcodeorfunction

.

• Integratedhelp-WhenusingtheQtConsole,callingafunctionprovidesaviewofthetopofthehelp function.Forexample,entering mean( willproduceaviewofthetop20linesofitshelptext.

• Inlinefigures-BoththeQtConsoleandthenotebookcanalsodisplayfigureinlinewhichproducesa tidy,self-containedenvironment.Thiscanbeenabledbyentering %matplotlibinline inanIPython session.

• Thespecialvariable containsthelastresultintheconsole,andsothemostrecentresultcanbe savedtoanewvariableusingthesyntax x=_

• Supportforprofiles,whichprovidefurthercustomizationofsessions.

1.5.2 LaunchingIPython

OSXandLinux

IPythoncanbestartedbyrunning ipython intheterminal.IPythonusingtheQtConsolecanbestartedusing jupyterqtconsole

AsinglelinelauncheronOSXorLinuxcanbeconstructedusing bash-c "jupyter qtconsole"

Thissinglelinelaunchercanbesavedas filename.commandwhere filename isameaningfulname(e.g. IPython-Terminal)tocreatealauncheronOSXbyenteringthecommand chmod755/FULL/PATH/TO/filename.command

ThesamecommandcantocreateaDesktoplauncheronUbuntubyrunning sudoapt-getinstall--no-install-recommendsgnome-panel gnome-desktop-item-edit~/Desktop/--create-new andthenusingthecommandastheCommandinthedialogthatappears.

Windows(Anaconda)

TorunIPythonopencmdandenter IPython inthestartmenu.StartingIPythonusingtheQtConsoleis similarandissimplycalled QtConsole inthestartmenu.Launching IPython fromthestartmenushould createawindowsimilartothatinfigure 1.1

Next,run

jupyterqtconsole--generate-config intheterminalorcommandprompttogenerateafilenamed jupyter_qtconsole_config.py.Thisfilecontains settingsthatareusefulforcustomizingtheQtConsolewindow.Afewrecommendedmodificationsare

Figure1.1:IPythonrunninginthestandardWindowsconsole(cmd.exe).

c.IPythonWidget.font_size=11

c.IPythonWidget.font_family= "Bitstream Vera Sans Mono"

c.JupyterWidget.syntax_style= 'monokai'

ThesecommandsassumethattheBitstreamVerafontshavebeenlocallyinstalled,whichareavailable from http://ftp.gnome.org/pub/GNOME/sources/ttf-bitstream-vera/1.10/.Opening QtConsole should createawindowsimilartothatinfigure 1.2 (althoughtheappearancemightdiffer)ifyoudidnotusethe recommendationconfiguration.

1.5.3

GettingHelp

HelpisavailableinIPythonsessionsusing help(function).Somefunctions(andmodules)haveverylong helpfiles.WhenusingIPython,thesecanbepagedusingthecommand ?function or function? sothatthe textcanbescrolledusingpageupanddownandqtoquit. ??function or function?? canbeusedtotype theentirefunctionincludingboththedocstringandthecode.

1.5.4 RunningPythonprograms

Whileinteractiveprogrammingisusefulforlearningalanguageorquicklydevelopingsomesimplecode, complexprojectsrequiretheuseofcompleteprograms.ProgramscanberuneitherusingtheIPython magicwork %runprogram.py orbydirectlylaunchingthePythonprogramusingthestandardinterpreter using pythonprogram.py.TheadvantageofusingtheIPythonenvironmentisthatthevariablesusedin theprogramcanbeinspectedaftertheprogramrunhascompleted.DirectlycallingPythonwillrunthe programandthenterminate,andsoitisnecessarytooutputanyimportantresultstoafilesothatthey canbeviewedlater.4

4ProgramscanalsoberuninthestandardPythoninterpreterusingthecommand: exec(compile(open(’filename.py’).read(),’filename.py’,’exec’))

Figure1.2:IPythonrunninginaQtConsolesession. TotestthatyoucansuccessfullyexecuteaPythonprogram,inputthecodeintheblockbelowintoa textfileandsaveitas firstprogram.py.

# First Python program import time

print('Welcome to your first Python program ') input('Press enter to exit the program ') print('Bye!') time.sleep(2)

Onceyouhavesavedthisfile,opentheconsole,navigatetothedirectoryyousavedthefileandenter pythonfirstprogram.py.Finally,runtheprograminIPythonbyfirstlaunchingIPython,andtheusing %cd tochangetothelocationoftheprogram,andfinallyexecutingtheprogramusing %runfirstprogram.py

1.5.5 %pylab and %matplotlib

WhenwritingPythoncode,onlyasmallsetofcorefunctionsandvariabletypesareavailableintheinterpreter.Thestandardmethodtoaccessadditionalvariabletypesorfunctionsistouse imports,which explicitlyallowaccesstospecificpackagesorfunctions.Whileitisbestpracticetoonly import required functionsorpackages,therearemanyfunctionsinmultiplepackagesthatarecommonlyencountered inthesenotes.PylabisacollectionofcommonNumPy,SciPyandMatplotlibfunctionsthatcanbeeasilyimportedusingasinglecommandinanIPythonsession, %pylab.Thisisnearlyequivalenttocalling from pylab import *,sinceitalsosetsthe backend thatisusedtodrawplots.Thebackendcanbemanuallysetusing %pylab backend where backend isoneoftheavailablebackends(e.g., qt5 or inline).Similarly %matplotlib backend canbeusedtosetjustthebackendwithoutimportingallofthemodulesandfunctionscomewith %pylab

Mostchaptersassumethat %pylab hasbeencalledsothatfunctionsprovidedbyNumPycanbecalled

Figure1.3:Asuccessfultestthatmatplotlib,IPython,NumPyandSciPywereallcorrectlyinstalled. withoutexplicitlyimportingthem.

1.5.6 TestingtheEnvironment

Tomakesurethatyouhavesuccessfullyinstalledtherequiredcomponents,runIPythonusingshortcut orbyrunning ipython or jupyterqtconsole runinaterminalwindow.Enterthefollowingcommands, oneatatime(themeaningofthecommandswillbecoveredlaterinthesenotes).

>>>%pylabqt5

>>>x= randn(100,100)

>>>y= mean(x,0)

>>> import seaborn

>>> plot(y)

>>> import scipy as sp

Ifeverythingwassuccessfullyinstalled,youshouldseesomethingsimilartofigure 1.3.

1.5.7 jupyternotebook

Ajupyternotebookisasimpleandusefulmethodtosharecodewithothers.Notebooksallowforafluid synthesisofformattedtext,typesetmathematics(usingLATEXviaMathJax)andPython.Theprimarymethod forusingnotebooksisthroughawebinterface,whichallowscreation,deletion,exportandinteractive editingofnotebooks.

Figure1.4:ThedefaultIPythonNotebookscreenshowingtwonotebooks. Tolaunchthejupyternotebookserver,openacommandpromptorterminalandenter

jupyternotebook

Thiscommandwillstarttheserverandopenthedefaultbrowserwhichshouldbeamodernversionof Chrome(preferable),ChromiumorFirefox.IfthedefaultbrowserisSafari,InternetExplorerorEdge,the URLcanbecopiedandpastedintoChrome.Thefirstscreenthatappearswilllooksimilartofigure 1.4, exceptthatthelistofnotebookswillbeempty.ClickingonNewNotebookwillcreateanewnotebook, which,afterabitoftyping,canbetransformedtoresemblefigure 1.5.Notebookscanbeimportedby dragginganddroppingandexportedfromthemenuinsideanotebook.

1.5.8 IntegratedDevelopmentEnvironments

AsyouprogressinPythonandbeginwritingmoresophisticatedprograms,youwillfindthatusinganIntegratedDevelopmentEnvironment(IDE)willincreaseyourproductivity.Mostcontainproductivityenhancementssuchasbuilt-inconsoles,codecompletion(orIntelliSense,forcompletingfunctionnames) andintegrateddebugging.DiscussionofIDEsisbeyondthescopeofthesenotes,although Spyder isa reasonablechoice(free,cross-platform). AptanaStudio isanotherfreealternative.MypreferredIDEis PyCharm,whichhasacommunityeditionthatisfreeforuse(theprofessionaleditionislowcostforacademics).

Figure1.5:AnIPythonnotebookshowingformattedmarkdown,LATEXmathandcellscontainingcode.

Spyder

SpyderisanIDEspecializedforuseinscientificapplicationsofPythonratherthanforgeneralpurpose applicationdevelopment.Thisisbothanadvantageandadisadvantagewhencomparedtoafullfeatured IDEsuchasPyCharm,PythonToolsforVisualStudio(PVTS),PyDevorAptanaStudio.Themainadvantage isthatmanypowerfulbutcomplexfeaturesarenotintegratedintoSpyder,andsothelearningcurveis muchshallower.Thedisadvantageissimilar-inmorecomplexprojects,orifdevelopingsomethingthatis notstraightscientificPython,Spyderislesscapable.However,nettingthesetwo,Spyderisalmostcertainly theIDEtousewhenstartingPython,anditisalwaysrelativelysimpletomigratetoasophisticatedIDEif needed.

Spyderisstartedbyentering spyder intheterminalorcommandprompt.Awindowsimilartothatin figure 1.6 shouldappear.Themaincomponentsaretheeditor(1),theobjectinspector(2),whichdynamicallywillshowhelpforfunctionsthatareusedintheeditor,andtheconsole(3).Bydefault,Spyderopens astandardPythonconsole,althoughitalsosupportsusingthemorepowerfulIPythonconsole.Theobject inspectorwindow,bydefault,isgroupedwithavariableexplorer,whichshowsthevariablesthatarein memoryandthefileexplorer,whichcanbeusedtonavigatethefilesystem.Theconsoleisgroupedwith anIPythonconsolewindow(needstobeactivatedfirstusingtheInterpretersmenualongthetopedge), andthehistorylogwhichcontainsalistofcommandsexecuted.Thebuttonsalongthetopedgefacilitate savingcode,runningcodeanddebugging.

1.6 Exercises

1. InstallPython.

2. Testtheinstallationusingthecodeinsection 1.5.6

3. ConfigureIPythonusingthestart-upscriptinsection ??.

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.