Wednesday, March 6, 2019

SciPy - 1 (Introduction)

SciPy is a package that utilizes NumPy arrays and manipulations to take on standard problems that scientists and engineers commonly face: integration, determining a function’s maxima or minima, finding eigenvectors for large sparse matrices, testing whether two distributions are the same, and much more.

The optimization package in SciPy allows us to solve minimization problems easily and quickly. For example, performing linear regression, finding a function’s minimum and maximum values, determining the root of a function, and finding where two functions intersect. To install SciPy use:

pip install Scipy 

The SciPy framework builds on top of the low-level NumPy framework for multidimensional arrays, and provides a large number of higher-level scienti c algorithms. Some of the topics that SciPy covers are:

• Special functions (scipy.special)
• Integration (scipy.integrate)
• Optimization (scipy.optimize)
• Interpolation (scipy.interpolate)
• Fourier Transforms (scipy. tpack)
• Signal Processing (scipy.signal)
• Linear Algebra (scipy.linalg)
• Sparse Eigenvalue Problems (scipy.sparse)
• Statistics (scipy.stats)
• Multi-dimensional image processing (scipy.ndimage)
• File IO (scipy.io)

Each of these submodules provides a number of functions and classes that can be used to solve problems in their respective topics.

In the coming posts we will look at how to use some of these subpackages. To access the SciPy package in a Python program, we start by importing everything from the scipy module.

from scipy import *

If we only need to use part of the SciPy framework we can selectively include only those modules we are interested in. For example, to include the linear algebra package under the name la, we can do:

import scipy.linalg as la

The scipy package provides information about its own structure when we use the help command:

>>> help ( scipy )

I am only showing a part of the output of this command:

stats --- Statistical Functions [*]
sparse --- Sparse matrix [*]
lib --- Python wrappers to external libraries [*]
linalg --- Linear algebra routines [*]
signal --- Signal Processing Tools [*]
misc --- Various utilities that don 't have another home .
interpolate --- Interpolation Tools [*]
optimize --- Optimization Tools [*]
cluster --- Vector Quantization / Kmeans [*]
fftpack --- Discrete Fourier Transform algorithms [*]
io --- Data input and output [*]
integrate --- Integration routines [*]
lib. lapack --- Wrappers to LAPACK library [*]
special --- Special Functions [*]
lib. blas --- Wrappers to BLAS library [*]

[*] - using a package requires explicit import (see pkgload )

If we are looking for an algorithm to integrate a function, we might explore the integrate package:

>>> import scipy . integrate
>>> help ( scipy . integrate )

SciPy provides implementations of a very extensive set of special functions which are important for many computational physics problems. Many of the numerical algorithms available through scipy and numpy are provided by established compiled libraries which are often written in Fortran or C. They will thus execute much faster than pure Python code (which is interpreted). As a rule of thumb, we expect compiled code to be two orders of magnitude faster than pure Python code.

Scipy is built on numpy. All functionality from numpy seems to be available in scipy as well. For
example, instead of

import numpy
x = numpy . arange (0, 10, 0.1)
y = numpy .sin(x)

we can therefore also use

import scipy as s
x = s. arange (0, 10, 0.1)
y = s.sin(x)

Here I am ending this introductory post regarding SciPy. Next post will explore the integrate package. Till we meet next keep learning and practicing Python as Python is easy to learn! 
Share:

0 comments:

Post a Comment