# Python SciPy Tutorial – What is SciPy & How to Install SciPy

Keeping you updated with latest technology trends, Join DataFlair on Telegram

## 1. Python SciPy Tutorial – Objective

In our previous Python Library tutorial, we saw Python Matplotlib. Today, we bring you a tutorial on Python SciPy. Here in this SciPy Tutorial, we will learn the benefits of Linear Algebra, Working of Polynomials, and how to install SciPy. Moreover, we will cover the Processing Signals with SciPy, and Processing Images with SciPy.
So, let’s start the Python SciPy Tutorial.

## 2. What is SciPy in Python?

Python SciPy is open-source and BSD-licensed. You can pronounce it as Sigh Pie. Here are some shorts on it:

• Author: Travis Oliphant, Pearu Peterson, Eric Jones.
• First Release: 2001
• Stable Release: Version 1.1.0; May, 2018.
• Written in: Fortran, C, C++, Python Programming Language.

Python SciPy has modules for the following tasks:

• Optimization
• Linear algebra
• Integration
• Interpolation
• Special functions
• FFT
• Signal and Image processing
• ODE solvers

And as we’ve seen, an important feature of the NumPy module is multidimensional arrays. This is what SciPy uses too; it will work with NumPy arrays.
Do you know about Python NumPy?

## 3. Python SciPy Tutorial – SciPy Subpackages

In this Python SciPy Tutorial, we will study these following sub-packages of SciPy:

• cluster- Hierarchical clustering.
• constants- Physical constants and factors of conversion.
• fftpack- Algorithms for Discrete Fourier Transform.
• integrate- Routines for numerical integration.
• interpolate- Tools for interpolation.
• io- Input and Output tools for data.
• lib- Wrappers to external libraries.
• linalg- Routines for linear algebra.
• misc- Miscellaneous utilities like image reading and writing.
• ndimage- Functions for processing multidimensional images.
• optimize- Algorithms for optimization.
• signal- Tools for processing signal.
• sparse- Algorithms for sparse matrices.
• spatial- KD-trees, distance functions, nearest neighbors.
• special- Special functions.
• stats- Functions to perform statistics.
• weave- Functionality that lets you write C++/C code in the form of multiline strings.

## 4. Python SciPy Tutorial – Benefits of SciPy

• High-level commands and classes for visualizing and manipulating data.
• Powerful and interactive sessions with Python.
• Classes, and web and database routines for parallel programming.
• Easy and fast.
• Open-source.

## 5. Python SciPy Tutorial – How to Install SciPy?

You can use pip to install SciPy-

`pip install scipy`

You can also use conda for the same-

`conda install –c anaconda scipy`

Then, you can import SciPy as:

`>>> import scipy`

You will also want to interact with numpy here. Let’s import that too.

```>>> import numpy
```

Let’s Revise Python Operators with Syntax and Examples
Finally, in some places, we will want to plot our results. We will use matplotlib for that; let’s import it.

`>>> import matplotlib`

## 6. SciPy Tutorial – Linear Algebra

For performing operations of linear algebra in SciPy, we will need to import linalg from scipy-

`>>> from scipy import linalg`

### a. Solving a set of equations

Let’s suppose this is the set of equations we want to solve-
2x+3y=7
3x+4y=10
Let’s create input and solution arrays for this.

```>>> A=numpy.array([[2,3],[3,4]])
>>> B=numpy.array([,])
>>> linalg.solve(A,B)```

array([[ 2.], [ 1.]])
This tells us that both equations work for x=2 and y=1. To check the results, we run the following command-

`>>> A.dot(linalg.solve(A,B))-B`

array([[ 0.], [ 0.]])
The output confirms the correctness of the computation.

### b. Determinants of Square Matrices

To calculate the determinant for a square matrix, we can use the det() method.

```>>>mat=numpy.array([[8,2],[1,4]])
>>>linalg.det(mat)```

30.0

### c. An inverse of a Square Matrix

For this, we use the inv() method.

`>>> linalg.inv(mat)`

array([[ 0.13333333, -0.06666667],
[-0.03333333, 0.26666667]])
Do you know the Steps to Install Python on Windows

### d. Singular Value Decomposition

This uses the method svd().

`>>> linalg.svd(mat)`

(array([[-0.9610057 , -0.27652857],
[-0.27652857, 0.9610057 ]]),
array([ 8.52079729, 3.52079729]),
array([[-0.9347217 , -0.35538056],
[-0.35538056, 0.9347217 ]]))

### e. LU Decomposition

To carry out LU decomposition, we can use the lu() method.

`>>> linalg.lu(mat)`

(array([[ 1., 0.],
[ 0., 1.]]), array([[ 1. , 0. ],
[ 0.125, 1. ]]), array([[ 8. , 2. ],
[ 0. , 3.75]]))

## 7. SciPy Tutorial – Working with Polynomials in SciPy

SciPy will let us work with polynomials using the poly1d type from numpy:

```>>> from numpy import poly1d
>>> p=poly1d([3,2,4])```

2
3 x + 2 x + 4

`>>> p*p`

4        3           2
9 x + 12 x + 28 x + 16 x + 16

`>>> p(5) #Value of polynomial for x=5`

89

`>>> p.integ(k=6) #Integration`

poly1d([ 1., 1., 4., 6.])

`>>> p.deriv() #Finding derivatives`

poly1d([6, 2])

## 8. SciPy Tutorial – Integration with SciPy

To be able to perform integration, we will import the integrate subpackage from scipy.

`>>> from scipy import integrate`

The quad() function will let us integrate a function; let’s use a lambda for this.

`>>> integrate.quad(lambda x:x**2,0,4) #Last two arguments are lower and upper limits`

(21.333333333333336, 2.368475785867001e-13)
You can use help() function to find out what else this subpackage can do for you. Here are some more functions:

• romberg- Romberg integration
• newton_cotes- Weights and coefficient of error with Newton-Cotes integration
• IntegrationWarning- Warning for issues in integration

## 9. SciPy Tutorial – Vectorizing Functions

The class vectorizes will let us turn a regular Python function into a vectorized function. Let’s try this out.
We’ll define a function first. If the first argument is greater than the second, it returns the sum of the two. Otherwise, it returns the product of the two.

```def addmul(a,b):
if a>b:
return a+b
else:
return a*b```

Now, let’s vectorize this.

`>>> numpy.vectorize(addmul)([1,4,3,9],[2,3,7,8])`

array([ 2, 7, 21, 17])

## 10. SciPy Tutorial – Fast Fourier Transforms

If you’re familiar with calculus, then there are chances are you may be familiar with Fourier Transforms. This analysis lets you express a function as a sum of periodic components. It is possible to recover the signal from this sum. Let’s take an example.

```>>> from scipy.fftpack import fft
>>> N=600
>>> T=1.0/800.0
>>> x=np.linspace(0.0,N*T,N)
>>> y=numpy.sin(50.0*2.0*numpy.pi*x)+0.5*numpy.sin(80.0*2.0*numpy.pi*x)
>>> yf=fft(y)
>>> xf=numpy.linspace(0.0,1.0/(2.0*T),N//2)
>>> import matplotlib.pyplot as plt
>>> plt.plot(xf, 2.0/N * numpy.abs(yf[0:N//2]))```

[<matplotlib.lines.Line2D at 0x7fbb78d65210>]

```>>> plt.grid()
>>> plt.show()```

Let’s have a look at Python Function Arguments

## 11. SciPy Tutorial – Special Functions of SciPy

The special module holds various transcendental functions that will help us with multiple tasks in mathematics. To import it, use the following command-

`>>> from scipy import special`

We make use of the various functions:

a. ERF
This function calculates the area under a Gaussean curve. We use the erf() function.
b. Gamma
This calculates the Gamma. The function for this is gamma().
c. Gammaln
This calculates the log of Gamma. We use the function gammaln().
d. eppilj
The elliptic function we have is eppilj().
e. jn
This is the Nth order Bessel function.
Let’s Explore Python Functions with Syntax and Examples

## 12. SciPy Tutorial – Processing Signals with SciPy

SciPy will also help you with signal processing. Let’s take an example.

### a. Using FFT to resample

We can resample a function to n points in a time domain interval. We can use the function resample() for this.

```>>> t=numpy.linspace(-10,10,200)
>>> y=numpy.sin(t)
>>> from scipy import signal
>>> signal.resample(y,100)```

array([ 0.25070765, 0.44580125, 0.12797317, 0.00253488, -0.24973473,
-0.39831149, -0.60510157, -0.72607055, -0.8701493 , -0.9336745 ,
-0.99979334, -0.98973944, -0.97222903, -0.88612051, -0.79126725,
-0.63978568, -0.48541023, -0.29029666, -0.10319111, 0.10642378,
0.29461537, 0.48697311, 0.64470867, 0.79055976, 0.89136943,
#More output here

### b. Removing Linear Trends

The detrend() function will remove the linear element from the signal; this gives us a transient solution.

```>>> t=numpy.linspace(-10,10,200)
>>> y=numpy.sin(t)+t
>>> signal.detrend(y)```

array([ 0.76761293, 0.67843367, 0.58464135, 0.48715987, 0.38695036,
0.28500148, 0.18231944, 0.07991785, -0.02119249, -0.12001383,
-0.21557153, -0.30692387, -0.39317157, -0.47346689, -0.54702214,
-0.61311766, -0.67110907, -0.7204338 , -0.76061672, -0.79127497,
-0.81212183, -0.82296958, -0.82373141, -0.81442233, -0.79515896,
#More output here

## 13. SciPy Tutorial – Processing Images with SciPy

With SciPy, you can use ndimage to process images. Some of the possible transitions are opening and closing images, geometrical transformation(shape, resolution, orientation), image filtering, and filters like erosion and dilation. You can import it as:

```>>> from scipy import ndimage
```

Let’s look at some functions we have:

a. Shift

This function will shift the image along the x and y coordinates. The function is shift(image,(x,y)).
b. Rotate
When you want to rotate your image to an angle, use the rotate(image, angle) function.
c. Zoom
With zoom(image, magnitude), you can zoom in or out on an image.
d. Median Filter
To filter an image with a Median filter, you can use median_filter(image, argument).
e. Gaussian Filter
And to filter with a Gaussian filter, you use gaussian_filter(image, argument).
f. Opening an Image in Binary
For this, you use the function binary_opening(image)
g. Closing an Image
To close this image, make a call to binary_closing(opened_image)
h. Binary Erosion
For this task, we use the function binary_erosion(image)
i. Binary Dilation
And to perform dilation, call binary_dilation(image)

## 14. SciPy Tutorial – Optimizing

For algorithms that will optimize, we need the optimize package. Import it this way-

`>>> from scipy import optimize`

Let’s take a demo piece of code to explain this.

```>>> from scipy.optimize import least_squares
>>> def func1(x):
return numpy.array([10 * (x - x**2), (1 - x)])
>>> input=numpy.array([2, 2])
>>> res=least_squares(func1, input)
>>> res```

cost: 0.0
fun: array([ 0., 0.])
jac: array([[-20.00000015, 10. ],
[ -1. , 0. ]])
message: ‘`gtol` termination condition is satisfied.’
nfev: 4
njev: 4
optimality: 0.0
status: 1
success: True
x: array([ 1., 1.])

## 15. SciPy Tutorial – Working with Stats sub-packages of SciPy

The stats subpackage lets us work with statistics. Let’s take a few examples.
from scipy.stats import norm.

### a. CDF Removing Linear Trends

`>>> norm.cdf(numpy.array([1,-1.,3,1,0,4,-6,2]))`

This function computes the cumulative distribution at the points we mention.
Have a Look at Python Packages Comprehensive Guide

### b. PPF

For the Percent Point Function, we use the function ppf().

`>>> norm.ppf(0.5)`

0.0

`>>> norm.ppf(2.3)`

nan

### c. RVS

For a random variate sequence, use rvs().

```>>> norm.rvs(size=7)
```

array([-1.42745064, -0.07120524, -0.88890304, 1.34584415, 1.24426647,
-1.57490259, 0.24552959])

### d. Binomial Distribution

For this, we use the cdf() function from uniform.

```>>> from scipy.stats import uniform
>>> uniform.cdf([0,1,2,3,4,5,6,7],loc=1,scale=3)```

array([ 0. , 0. , 0.33333333, 0.66666667, 1. ,
1. , 1. , 1. ])
So, this was all Python SciPy Tutorial. Hope you like our explanation.

## 16. Conclusion

Hence, in this SciPy tutorial, we studied introduction to Scipy with all its benefits and Installation process. At last, we discussed several operations used by Python SciPy like Integration, Vectorizing Functions, Fast Fourier Transforms, Special Functions, Processing Signals, Processing Images, Optimize package in SciPy. Still, you have a query, feel free to share with us.
Related Topic- Top 35 Python Interview Questions and Answers in 2018
For reference

### 3 Responses

1. rakesh kumar says:

instead of trying to teach everything, you could have gone into deeper details of any one thing or two thing so that whatever people read would have learnt. this scipy tutorial is a little disappointing for me.

2. Ron Deep says:

Nice review for numpy polynomials and scipy. Relatively hard to find scipy and panda to install on Python

• DataFlair Team says:

Hey Ron Deep,