Scientific Python (Scipy) for Machine Learning


Scientific Python, well known as Scipy is a numerical processing library in Python. It is well known for several pre-built functions for various mathematical tasks that show up often in Machine Learning. is the official website of Scipy. You can download/install Scipy by following the instructions present here:

Scipy is open source. is the main Scipy repository. Scipy comes with a BSD License, which is a free and a permissive license.

Let us now talk about some important applications of Scipy that are often used in Machine Learning and Data Science.

Mathematical Constants

Scipy provides several commonly used Mathematical and Physical Constants. For instance, Scipy provides constants like pi, the speed of light in vacuum, Planck’s Constant, Newton’s Gravitational Constant, Mass of Electron, Avogadro’s Number, etc. Take a look at the following code:

The following is the output of the above code:

Besides the above constants, Scipy also provides several unit values in terms of Standard International (SI) units. For instance, take a look at the code below:

The output of this code will be as follows:

Importance in Machine Learning: such constants, particularly the mathematical constants often come up in several preprocessing steps in Machine Learning applications.

Fast Fourier Transformation

Fast Fourier Transformation (FFT) is widely used in several Signal Processing applications as a data preprocessing step.

The output of the above code will be as follows:
[ 6.1+0. j 3.3-1.2j -4.3+0. j 3.3+1.2j]

Numerical Integration

Scipy provides several built-in functions for Numerical Integration as well. For instance, Scipy has functions for Romberg Integration, Simpson’s Rule of Integration, etc. Let us take a look at some examples of how we can perform numerical integration in Scipy:

We get the following output:

(0.33333333333333337, 3.700743415417189e-15)
The first parameter above indicates the value of the integral that Scipy calculated. The second parameter indicates the possible error. As can be seen, the error is of the order of 10-15 which is pretty good given that the actual value of integral is ⅓.

Scipy also provides built-in functions for double integration as well.

Matrices and Determinants

Scipy provides excellent support for solving multivariate linear equations. As an example, take a look at the following:

The output of the above code is as follows:

The Solution is: [ 1. -1. 2.]

The above code represents how Scipy can solve a system of the following 3 linear equations:

  • x – 2y + 3z = 9
  • -x + 3y – z = -16
  • 2x – 5y + 5z = 17

As can be seen, Scipy solves it beautifully and is able to get the correct answers.

Similarly, Scipy can find the determinant of several matrices. Take a look at the following code:

The output is as follows:

The Determinant is: 1.0

Image Processing

Scipy can perform powerful image manipulations that are often used in the preprocessing step of several Machine Learning applications. As an example, Gaussian blur is one of the most commonly used filters when dealing with Machine Learning applications. Take a look at the code below that demonstrates how Gaussian Filter can be applied to an image in no time using Scipy.

The output of the above code will be the following image:

image processing

Finding roots of Mathematical Equations

Scipy provides built-in functions that use the Fixed-point iteration mechanism to find roots of Mathematical Equations. Take a look at the following code:

The output of the above code will be:

from scipy.optimize import root

def my_function(x):
return x**3 + 2

root_of_function = root(my_function, 0.01)
print root_of_function

The output of the above code will be:

fjac: array([[-1.]])
fun: array([0.])
message: ‘The solution converged.’
nfev: 11
qtf: array([-1.04385389e-10])
r: array([-4.76220423])
status: 1
success: True
x: array([-1.25992105])


Scipy is a powerful tool and has wide applicability in various applications. You should master this library in order to know how to preprocess data in the best possible way since the preprocessing step often improves the performance of a particular algorithm significantly.

Scipy is most frequently used in combination with other Python libraries like Numpy, Pandas, etc. Numpy is used as an internal “data structure” for holding data of various types in N-dimensional arrays. Pandas, on the other hand, forms a wrapper around Numpy to provide ease of access. You should also have a basic understanding of these libraries in order to get the most out of Scipy.



Please enter your comment!
Please enter your name here