Post on 23-Mar-2020
transcript
Scipy
MingYe(2015)andPeterBeerli(2017update)
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.
• IsthecorepackageforscientificroutinesinPython
• Operate efficiently on numpy arrays, so that numpyandscipyworkhandinhand.
• Contains various toolboxes dedicated to commonissuesinscientificcomputingsuchas:
interpolation, integration, optimization, imageprocessing,statistics,specialfunctions,etc.
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.
• 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
Fileinput/output:scipy.io matlabfiles:sio.loadmat sio.savemat sio.whosmat
Savemat:SaveadictionaryofnamesandarraysintoaMATLAB-style.matfile.
• ReadingImage:
LinearAlgebra:scipy.linalg• Thescipy.linalg.det()functioncomputesthedeterminantofasquarematrix
Thescipy.linalg.inv()functioncomputestheinverseofasquarematrix:
SVD:SingularValueDecomposition
• Theoriginalmatrixcanbere-composedbymatrixmultiplicationoftheoutputsofsvdwithnp.dot:
Optimizationandfit:scipy.optimize
Optimizationistheproblemoffindinganumericalsolutiontoaminimizationorequality.Thescipy.optimizemoduleprovidesusefulalgorithmsforfunctionminimization(scalarormultidimensional),curvefittingandrootfinding.
Thisfunctionhasaglobalminimumaround-1.3andalocalminimumaround3.8
• Thegeneralandefficientwaytofindaminimumforthisfunctionistoconductagradientdescentstartingfromagiveninitialpoint.TheBFGSalgorithmisagoodwayofdoingthis:
Apossibleissuewiththisapproachisthat,ifthefunctionhaslocalminimathealgorithmmayfindtheselocalminimainsteadoftheglobalminimumdependingontheinitialpoint:
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:
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
Rootfinding
Tofindaroot,apointwheref(x)=0
Onlyonerootisfound.Butthereisasecondrootaround-2.5.Wefindtheexactvalueofitbyadjustingourinitialguess:
scipy.optimize.fsolve()
Othermodules:
Curvefitting:Supposewehavedatasampledfromfwithsomenoises.ifweknowthefunctionalformofthefunctionfromwhichthesample,werebutnottheamplitudesoftheterms,wecanfindthosebyleastsquarescurvefitting.1. wehavetodefinethefunctiontofit:2. Wehavedatasampledfromfwithsomenoise:3. canusescipy.optimize.curve_fit()tofindaandb
Plotthecurveandfittedpoints:
HistogramandprobabilitydensityfunctionGiven observations of a random process, their histogram is anestimatoroftherandomprocess’sPDF(probabilitydensityfunction):
Scipystatistic
cdf:CumulativeDistributionFunction
Interpolation:scipy.interpolate Thescipy.interpolateisusefulforfittingafunctionfromexperimentaldataandthusevaluatingpointswherenomeasureexists.
Thescipy.interpolate.interp1dclasscanbuildalinearinterpolationfunction:
Thenthescipy.interpolate.linear_interpinstanceneedstobeevaluatedatthetimeofinterest:
Acubicinterpolationcanalsobeselectedbyprovidingthekindoptionalkeywordargument:
Geometricaltransformationsonimages