Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience...

35
Scipy Ming Ye (2015) and Peter Beerli (2017 update)

Transcript of Scipy - Peter Beerli · SciPy • Is a collection of mathematical algorithms and convenience...

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

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

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

• 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

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

• 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

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

• 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

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
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

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
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

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

• 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

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

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

• 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
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

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

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

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

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

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

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

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

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

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
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

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
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

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

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

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
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

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