Basic Python | Scientific computing with Python


The scipy package contains various toolboxes dedicated to common issues in scientific computing. Its different submodules correspond to different applications, such as interpolation, integration, optimization, image processing, statistics, special functions, etc.
Scipy can be compared to other standard scientific-computing libraries, such as the GSL (GNU Scientific Library for C and C++), or Matlab’s toolboxes. scipy is the core package for scientific routines in Python; it is meant to operate efficiently on numpy arrays, so that numpy and scipy work hand in hand.
Before implementing a routine, it is worth checking if the desired data processing is not already implemented in Scipy. As non-professional programmers, scientists often tend to re-invent the wheel, which leads to buggy, non-optimal, difficult-to-share and unmaintainable code. By contrast, Scipy‘s routines are optimized and tested, and should therefore be used when possible.
This tutorial is far from an introduction to numerical computing. As enumerating the different submodules and functions in scipy would be very boring, we concentrate instead on a few examples to give a general idea of how to use scipy for scientific computing.
This tutorial is far from an introduction to numerical computing or the mathematics related with it. As enumerating the different submodules and functions in scipy would be very boring, we concentrate instead on a few examples to give a general idea of how to use scipy for scientific computing.
Scipy Modules
Scipy is composed of task-specific sub-modules:
scipy.clusterVector quantization / Kmeans
scipy.constantsPhysical and mathematical constants
scipy.fftpackFourier transform
scipy.integrateIntegration routines
scipy.interpolateInterpolation
scipy.ioData input and output
scipy.linalgLinear algebra routines
scipy.ndimagen-dimensional image package
scipy.odrOrthogonal distance regression
scipy.optimizeOptimization
scipy.signalSignal processing
scipy.sparseSparse matrices
scipy.spatialSpatial data structures and algorithms
scipy.specialAny special mathematical functions
scipy.statsStatistics

They all depend on numpy, but are mostly independent of each other.
Numerical integration: scipy.integrate
SciPy provides integration techniques that solve mathematical sequences and series, or perform function approximation.

Optimization

Optimization is the problem of finding a numerical solution to a minimization or equality. The scipy.optimize module provides useful algorithms for function minimization (scalar or multidimensional), curve fitting and root finding. SciPy provides several optimization algorithms such as bfgs, Nelder-Mead simplex, Newton Conjugate Gradient, COBYLA, or SLSQP.
Finding the minimum of a scalar function
Let’s define the following function:
The general and efficient way to find a minimum for this function is to conduct a gradient descent starting from a given initial point. The BFGS algorithm is a good way of doing this.
A possible issue with this approach is that, if the function has local minima the algorithm may find these local minima instead of the global minimum depending on the initial point.
If we don’t know the neighborhood of the global minimum to choose the initial point, we need to resort to costlier global optimization. To find the global minimum, we use scipy.optimize.basinhopping() (which combines a local optimizer with stochastic sampling of starting points for the local optimizer).
Linear Algebra
The scipy.linalg module provides standard linear algebra operations, relying on an underlying efficient implementation. SciPy provides very rapid linear algebra capabilities and contains advanced algebraic functions

No comments:

Post a Comment

Note: only a member of this blog may post a comment.