![Page 1: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/1.jpg)
Scipy
MingYe(2015)andPeterBeerli(2017update)
![Page 2: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/2.jpg)
SciPy• Is a collection of mathematical algorithms andconveniencefunctionsbuiltontheNumpyextensionofPython.
• It adds significant power to the interactive Pythonsession by providing the user with high-levelcommands and classes for manipulating andvisualizingdata.
![Page 3: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/3.jpg)
• IsthecorepackageforscientificroutinesinPython
• Operate efficiently on numpy arrays, so that numpyandscipyworkhandinhand.
• Contains various toolboxes dedicated to commonissuesinscientificcomputingsuchas:
interpolation, integration, optimization, imageprocessing,statistics,specialfunctions,etc.
![Page 4: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/4.jpg)
TheadditionalbenefitofbasingSciPy:
• Making a powerful programming language availablefor use in developing sophisticated programs andspecializedapplications.
• Everything from parallel programming to web anddata-base subroutines and classes have been madeavailabletothePythonprogrammer.
![Page 5: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/5.jpg)
• scipy.cluster:Vectorquantization/Kmeans• scipy.constants:Physicalandmathematicalconstants• scipy.fftpack:Fouriertransform• scipy.integrate:Integrationroutines• scipy.interpolate:Interpolation• scipy.io:Datainputandoutput• scipy.linalg:Linearalgebraroutines• scipy.ndimage:n-dimensionalimagepackage• scipy.odr:Orthogonaldistanceregression• scipy.optimize:Optimization• scipy.signal:Signalprocessing• scipy.sparse:Sparsematrices• scipy.spatial:Spatialdatastructuresandalgorithms• scipy.special:Anyspecialmathematicalfunctions• scipy.stats:Statistics
![Page 6: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/6.jpg)
Fileinput/output:scipy.io matlabfiles:sio.loadmat sio.savemat sio.whosmat
Savemat:SaveadictionaryofnamesandarraysintoaMATLAB-style.matfile.
![Page 7: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/7.jpg)
• ReadingImage:
![Page 8: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/8.jpg)
LinearAlgebra:scipy.linalg• Thescipy.linalg.det()functioncomputesthedeterminantofasquarematrix
![Page 9: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/9.jpg)
![Page 10: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/10.jpg)
Thescipy.linalg.inv()functioncomputestheinverseofasquarematrix:
![Page 11: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/11.jpg)
![Page 12: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/12.jpg)
SVD:SingularValueDecomposition
![Page 13: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/13.jpg)
• Theoriginalmatrixcanbere-composedbymatrixmultiplicationoftheoutputsofsvdwithnp.dot:
![Page 14: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/14.jpg)
Optimizationandfit:scipy.optimize
Optimizationistheproblemoffindinganumericalsolutiontoaminimizationorequality.Thescipy.optimizemoduleprovidesusefulalgorithmsforfunctionminimization(scalarormultidimensional),curvefittingandrootfinding.
![Page 15: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/15.jpg)
Thisfunctionhasaglobalminimumaround-1.3andalocalminimumaround3.8
![Page 16: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/16.jpg)
• Thegeneralandefficientwaytofindaminimumforthisfunctionistoconductagradientdescentstartingfromagiveninitialpoint.TheBFGSalgorithmisagoodwayofdoingthis:
![Page 17: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/17.jpg)
![Page 18: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/18.jpg)
Apossibleissuewiththisapproachisthat,ifthefunctionhaslocalminimathealgorithmmayfindtheselocalminimainsteadoftheglobalminimumdependingontheinitialpoint:
![Page 19: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/19.jpg)
If we don’t know the neighborhood of the globalminimumtochoosetheinitialpoint,weneedtoresortto costlier global optimization. To find the globalminimum, the simplest algorithm is the brute forcealgorithm, inwhich the function isevaluatedoneachpointofagivengrid:
![Page 20: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/20.jpg)
BruteForcealgorithmbecomesquiteslowforlargergridsizes.Simulatedannealingcanbeagoodalternative:
Scipy.optimize.anneal()
Forlocalminimum,wecanconstraintthevariabletotheintervalanduse:
• Let s = s0
• For k = 0 through kmax (exclusive): • T ← temperature(k ⁄ kmax)
• Pick a random neighbour, snew ← neighbour(s)• If P(E(s), E(snew), T) ≥ random(0, 1), move to the new state:
• s ← snew
• Output: the final state s
![Page 21: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/21.jpg)
Rootfinding
Tofindaroot,apointwheref(x)=0
![Page 22: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/22.jpg)
Onlyonerootisfound.Butthereisasecondrootaround-2.5.Wefindtheexactvalueofitbyadjustingourinitialguess:
scipy.optimize.fsolve()
![Page 23: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/23.jpg)
Othermodules:
![Page 24: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/24.jpg)
Curvefitting:Supposewehavedatasampledfromfwithsomenoises.ifweknowthefunctionalformofthefunctionfromwhichthesample,werebutnottheamplitudesoftheterms,wecanfindthosebyleastsquarescurvefitting.1. wehavetodefinethefunctiontofit:2. Wehavedatasampledfromfwithsomenoise:3. canusescipy.optimize.curve_fit()tofindaandb
![Page 25: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/25.jpg)
Plotthecurveandfittedpoints:
![Page 26: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/26.jpg)
HistogramandprobabilitydensityfunctionGiven observations of a random process, their histogram is anestimatoroftherandomprocess’sPDF(probabilitydensityfunction):
![Page 27: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/27.jpg)
![Page 28: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/28.jpg)
Scipystatistic
![Page 29: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/29.jpg)
![Page 30: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/30.jpg)
cdf:CumulativeDistributionFunction
![Page 31: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/31.jpg)
Interpolation:scipy.interpolate Thescipy.interpolateisusefulforfittingafunctionfromexperimentaldataandthusevaluatingpointswherenomeasureexists.
Thescipy.interpolate.interp1dclasscanbuildalinearinterpolationfunction:
Thenthescipy.interpolate.linear_interpinstanceneedstobeevaluatedatthetimeofinterest:
![Page 32: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/32.jpg)
Acubicinterpolationcanalsobeselectedbyprovidingthekindoptionalkeywordargument:
![Page 33: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/33.jpg)
![Page 34: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/34.jpg)
Geometricaltransformationsonimages
![Page 35: Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. • It adds significant power to the](https://reader030.vdocuments.site/reader030/viewer/2022040522/5e81efcf9f9e151ba6551f37/html5/thumbnails/35.jpg)