Scientists and researchers are likely to gather enormous amount of information and data that are scientific and technical, from their exploration, experimentation and analysis. Dealing with such huge amount of data becomes a hindrance to them. That is, calculation and computing with large data manually is not an easy task. Hence, we make use of super computers and data science for the purpose of faster computing and accurate outcomes.
Another simpler way to deal with scientific and technical computing of data is by making use of one of the python library which is solely built for this purpose. It is referred to as SciPy (pronunciation “sigh pi”).
SciPy is open-source software, therefore it can be used free of cost and many new data science features are incorporated in it.
Following is the list of all the topics that we will cover in this tutorial.
Scipy installation varies with respect to different operating systems. The following content shall be useful for you to easily figure out how to install scipy in your respective operating system.
Pip install scipy
Pip is basically a recursive acronym which stands for “Pip installs packages”. It is a standard package manager which can be installed in most operating systems.
Note: In order to installed other packages by using pip command, you need to make sure that you have installed python and pip in your system.
This command is mainly used for installing scipy in windows operating system with the help of pip. Installation of packages to local users rather than system directories by making use of –user flag.
This command denotes installing of scipy in Mac. Sudo is a command that allows one user to run programs with security privileges of another user.
This command is used to install scipy in a Linux operating system. apt-get is one of the command line tool which is necessary to work with APT (Advanced Packaging Tools) software packages.
Many dedicative software tools are necessary for python scientific computing and scipy is one such tool or library offering many modules that you can work with in order to perform complex operations.
The following list shows some of the modules or sub-packages that can be used for computing
|6.||Fast Fourier Transforms||scipy.fftpack|
|11.||Multidimensional image processing||scipy.ndimage|
|12.||Spatial data structures and algorithms||scipy.spatial|
This is a basic scipy code where the sub-package signal is being imported. We can import any sub-package in the similar manner. numpy is required for most of the sub-packages. The sub-package signal can be replaced by other modules concerned with scipy.
Numerical integration is carried out in scipy by making use of scipy.integrate sub-package. This package provides several integration techniques. Some of the integration functions are listed below.
|1||quad||To perform single integration|
|2||dblquad||To perform double integration|
|3||tplquad||To perform triple integration|
|4||nquad||Performs n-fold multiple integration|
|5||fixed_quad||for Gaussian quadrature and order n|
|6||quadrature||For Gaussian quadrature to tolerance|
|7||romberg||To perform Romberg integration|
|8||trapz||To perform trapezoidal rule|
|9||cumtrapz||For Trapezoidal rule to cumulatively compute integral|
|10||simps||For Simpson’s rule|
|12||polyint||Analytical polynomial integration (NumPy)|
|13||poly1d||Helper function for polyint (NumPy)|
It can also be called as general purpose integration. When there is only one variable present between two points, then we make use of the function quad.
The above function used is, quad with the two limits ranging between a and b.
Let us understand the function with an example
A researcher is gathering a few data and he wants to find out the integrals of those data.
In the above example, 12x is the function which lies between the intervals 0 and 1.
Example for single integration
Lambda function is made use so that any number of arguments can be used but it can have only one expression. Like here the expression is 12x. And we make use of the integrate function scipy.integrate.quad(f, 0, 1)
It is a type of integration where a function consists of at least two variables with y being the first argument and x being the second argument.
The above function used is dblquad and here the y argument lies between the limits a and b. And the x argument lies between the limits g and h. Hence, two variables are defined.
The researcher now adds another variable to the previous data and makes it into a double integral.
Hence, the above code contains the integration function used for double integrals scipy.integrate.dblquad(f, 0, 0.5, g, h) where, f is the function 12x, 0 and 0.5 are the integrals for y function and g and h are the integrals for x function.
It is a type of integration where a function consists of at least three variable.
There will be three functions for x, y and z. hence, we compute with three integrals.
We make use of tplquad function with three integrals, one with interval a and b, the other with interval g and h and the third with interval q and r.
The researcher has further analyzed to involve with three integrals so he thinks of combining the two integral values with the third one represented with a z function.
The above example denotes three integrals with a new function dz between the intervals 0 and 3
Example for triple integrals
The above code represents triple integral function integrate.tplquad(f, 0, 1, lambda x: 0, lambda x: 0.5, lambda x, y: 0, lambda x, y: 3) where we have represented three integrals with first one for z function between 0 and 3 intervals, y function between 0 and 0.5 intervals and x function between 0 and 1 intervals.
Images can be read from a file as an array by making use of scipy.misc.imread. You will be able to use this function only if you have installed python imaging library (PIL).
In the above output I have used an image called Mario.png and this image has been read from its saved file and later converted into array so it can be later used for image processing.
Optimization is the method of selecting the most effectual or best resource or situation for a given problem. The syntax of optimization can be given as
Minimize function can be used to provide a common interface to constrained and unconstrained algorithms for a multivariate scalar function in scipy.optimize sub-package.
Scipy.optimize contains various modules.
It is Applied for nonlinear optimization problems for which the derivatives will be unknown and it is a direct search method. We make use of the minimize() routine along with a Nelder- Mead simplex example.
The method used in this algorithm is (method = ‘Nelder-Mead’)
We use optimize at times to solve least squares problem with bounds on the variables.
The following example illustrates the use of a rosenblock function to implement least square problem.
This is one of the minimization methods that comes under optimization. In the following example we import a root function from the scipy.optimize sub-library in order to use it in the further calculation within the code.
This is the part of optimization where we make use of non-linear least squares to fit a function.
The following code illustrates the curve fit.
Finding the value between two points in a curve or a line can be termed as interpolation.
This sub-package contains a large number of probability distributions as well as a growing library of statistical functions.
There are various sub modules in statistics. They are listed below:
This is a type of generic continuous random variable class which is mainly meant for sub classing.
Continuous random variable is represented as rv_continuous with various parameters within the function.
A simple demonstration of rv_continuous sub module.
This is a type of generic random variable class which is mainly meant sub classing.
random variable for discrete representation is given by rv_discrete with various parameters within the function.
The following example demonstrates rv_discrete sub module under scipy.stats
from scipy import stats
In the above generated graph 7 values are plotted at various points as specified along the y axis.
generates the distribution given by a histogram
random variable histogram is represented as rv_histogram with various parameters within the function.
The following example demonstrats the representation of an rv_historgams.
A gradual increase and then a stable flow of the wave is represented in the above graph that demonstrates the random variable histogram for probability density function [PDF] and Cumulative distribution function [CDF].
Arithmetic operations such as addition, subtraction, division, matrix power and multiplication can make use of sparse matrices. We can implement sparse matrix for two matrix formats:
This enables efficient row slicing. Let us see a simple program where we generate an empty 3×3 CSR matrix using the scipy.sparse.
Representation of a 3×3 CSR matrix upon specification of the rows and columns through inputs.
This enables efficient column slicing. Let us see a simple program where we generate an empty 3×3 CSC matrix using the scipy.sparse.
Representation of a 3×3 CSC matrix upon specification of the rows and columns through inputs
Compressed Sparse Columns is more efficient at accessing column operations or column vectors as it is stored as arrays of columns and their value at each row.
Compressed Sparse Row matrix are the opposite, it is stored as arrays of rows and their values stored at each column, and are more efficient at accessing row operations or row vectors.
CSR and CSC are difficult to construct from scratch. While COO and DOK are easier to construct.
This enables efficient row slicing. Let us see a simple program where we generate an empty 3×3 COO matrix using the scipy.sparse.
Representation of a 3×3 CSR matrix upon specification of the rows and columns through inputs.
We can construct sparse matrix incrementally in an efficient manner using this module under scipy.sparse sub-package
The dictionary of keys allows access for individual values within the matrix
The method for expressing any function as a sum of periodic components, and for recovering the signal from those components can be termed as Fourier analysis. When both the Fourier transform and its respective function are replaced with some discrete counterparts then it is termed as discrete Fourier transform.
We make use of the Fourier transform sub-package scipy.fftpack.
Fourier front transform is performed on the given array of values and the above output is generated.
we can compute one dimensional Fourier transforms by making use of the following standard syntax
we make use of fft for one-dimensional discrete Fourier transform and ifft for one-dimensional inverse discrete Fourier transform.
Let me explain this sub-package with a simple example of sum of two cosines for a one-dimensional discrete Fourier transform.
By making use of two cosine functions, the sum is calculated and plotted in the graph. The waves are plotted at 70 and 90 degrees respectively along the x axis.
We can compute two-dimensional Fourier transform by making use of the following syntax.
we make use of fft for two-dimensional discrete Fourier transform and ifft for two-dimensional inverse discrete Fourier transform.
we make use of fft for N-dimensional discrete Fourier transform and ifft for N-dimensional inverse discrete Fourier transform.
Let us consider a simple example of time- domain signals by making use of two-dimensional inverse FFT
Here, we come to the end of the tutorial. I’m hoping that I have provided sufficient information about the scientific and technical library of python that is scipy. There are various computing calculations that are time consuming and stressful for the human brain. Hence, by making use of such scientific calculation library in python programming language to carry out this purpose with ease and incredible speed has proved that this library function plays a vital role in data science.
I would suggest you to practice the various examples illustrated along with each concept and try to implement your own examples in order to understand the concept in a better manner and excel in it.
If you are looking to learn more, then do check out our Python certification Training Course which is specially designed to help you get in-depth understanding of all the Python concepts.
Download Interview Questions asked by top MNCs in 2019?