Table of contents
1.
Introduction
2.
SciPy Sub-packages
3.
Special Function
4.
Discrete Fourier Transform – scipy.fftpack
5.
Interpolation
6.
Optimization
7.
Spatial Data Structures and Algorithms
8.
Multidimensional Image Processing Functions
9.
File Input and Output
10.
Frequently Asked Questions
11.
Key Takeaways
Last Updated: Mar 27, 2024

Must know Functions In SciPy

Author Mayank Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

We have already discussed the basics of SciPy in my previous article. This article will have a detailed discussion on some of the advanced functions commonly used in SciPy.

 

SciPy Sub-packages

SciPy is divided into sub-packages covering different scientific computing fields. We describe these packages in the below table:

    Sub-packages Description
        scipy.cluster Vector quantization / Kmeans(Clustering Algorithms)
        scipy.constant Mathematical and Physical Constants
        scipy.fftpack Fourier Transformation routines
        scipy.integrate Integration solvers
        scipy.interpolate Interpolation
        scipy.io Data input/ output
        scipy.linalg Linear Algebra solvers
        scipy.ndimage N-dimensional image processing
        scipy.odr Orthogonal distance regression
        scipy.optimize Optimization of functions
        scipy.signal Signal Processing
        scipy.sparse Sparse matrices
        scipy.spatial Spatial Data structures and Algorithms
        scipy.special Special mathematical functions
        scipy.stats Statistical tests and distributions.

 

Now let us look at these sub-packages one by one.

 

Special Function

The special package's universal functions follow broadcasting and automatic array looping.

Let's discuss some of the most frequently used special functions −

  • Cubic Root Function-Returns the cube root of a number x.
Syntax:scipy.special.cbrt(x)

 

  • Exponential Function-Return 10**x element-wise.
Syntax:cipy.special.exp10(x)

 

  • Relative Error Exponential Function-Returns the relative error exponential.
Syntax:scipy.special.exprel(x)

 

  • Log Sum Exponential Function-Returns the sum of the exponential of input numbers.
Syntax:scipy.special.logsumexp(x)

 

  • Lambert Function-Called as W lambert function. Return the inverse of (w*exp(w)). 
Syntax:scipy.special.lambertw(x)

 

  • Permutations and Combinations Function- Returns the permutation and combination of given numbers.
Syntax:scipy.special.comb(N,k)

 

Syntax:scipy.special.perm(N,k)

 

  • Gamma Function-It is known as Gamma function. It is the generalized factorial since z*gamma(z) = gamma(z+1) and gamma(n+1) = n!, for a natural number ‘n’.
Syntax:scipy.special.gamma(x)

 

Example:

from scipy.special import cbrt
ans = cbrt([ 90.1254234])
print ans

 

Output

[  2.08008382 0.50053277 6.16224015]

from scipy.special import exp10
res = exp10([29])
print res

 

Output

[1.00000000e+02  1.00000000e+09]

from scipy.special import exprel
  
# list of elements 
arr = [0,2,4,6
print(list(map(exprel,arr)))

 

Output

[1.0, 3.194528049465325, 13.399537508286059, 67.07146558212251]

from scipy.special import gamma
print([ gamma(0),
      gamma(1), gamma(5)])

 

Output

[inf, 1.0, 24.0]

from scipy.special import perm
print([perm(81), perm(82), perm(83), 
      perm(84), perm(85),perm(86),perm(87),perm(88),perm(89)])

 

Output:

[8.0, 56.0, 336.0, 1680.0, 6720.0, 20160.0, 40320.0, 40320.0, 0.0]

 

Discrete Fourier Transform – scipy.fftpack

Fourier analysis deals with expressing a function as a sum of periodic components and recovering the signal from those components. Fourier transformation finds application in signal and noise processing, image processing, audio signal processing, etc. SciPy offers the fftpack module, letting the user compute fast Fourier transforms.

 

  • DFT is a technique used to convert spatial data into frequency data.
  • FFT is an algorithm for computing DFT
  • We apply FFT to a multidimensional array.
  • Frequency defines the number of signals or wavelengths in a particular period.

 

Example

from scipy.fftpack import fft, ifft
import numpy as np
x = np.array([0,1,2,3,4])
y = fft(x)
y1=ifft(x)
print(y)
print(y1)

 

Output

[10. -0.j         -2.5+3.4409548j  -2.5+0.81229924j -2.5-0.81229924j

 -2.5-3.4409548j ]

[ 2. -0.j         -0.5-0.68819096j -0.5-0.16245985j -0.5+0.16245985j

 -0.5+0.68819096j]

 

Interpolation

Interpolation refers to finding a relationship for a value between two points on a line or curve. Interpolate means to look inside the data which we originally had. This tool, interpolation, is not only valuable for statistics but is also helpful in science, business, or when we need to predict values that fall within two existing data marks.

 

SciPy library provides us a subpackage named scipy.interpolate. It consists of spline functions and classes, one-dimensional and multidimensional interpolation classes, etc.

 

Univariate Interpolation

We use interp1d to plot a univariate interpolation. We use univariate interpolation to find a curve for the exact fit to a series of you-dimensional data points.

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
x = np.linspace(0430)
y = np.cos(x**2/3+8)

f1 = interpolate.interp1d(x, y,kind = 'linear')

f2 = interpolate.interp1d(x, y, kind = 'cubic')
xnew = np.linspace(04,30)

plt.plot(x, y, 'o', xnew, f1(xnew), '-', xnew, f2(xnew), '--')

plt.legend(['data''linear''cubic','nearest'], loc = 'best')

plt.show()

 

Output

Optimization

In SciPy, optimizers find the function's minimum value or the root of an equation. We can see some commonly used optimization algorithms with the help function provided by scipy.optimize.

 

It consists of the following:

  • Unconstrained minimization of multivariate scalar functions, i.e., minimize (method=' Nelder-Mead," BFGS,' 'Newton-CG,' 'trust-ncg')

 

  • constrained minimization of multivariate scalar functions(method=’trust-constr’,’ SLSQP’)

 

  • Global optimization routines (e.g., basin_shopping, dual_annealing)

 

  • Least-squares minimization (method=’ least_squares’)

 

  • Scalar univariate functions minimizers and root finders (e.g., minimize_scalar and root_scalar)

 

  • Univariate function minimizers(method=’brent’,’ bounded’)

 

  • Multivariate equation system solvers using algorithms such as hybrid Powell, Levenberg-Marquardt.

 

The roots of an Equation

SciPy can find the roots of linear, non-linear, and polynomial equations, whereas numpy cannot find the roots of non-linear equations. We can use optimize.root method. This function takes two required arguments:

fun - Represents an equation.

X - an initial guess for the root.

The function returns an object with information regarding the solution.

from scipy.optimize import root
from math import tan

def equation(x):
  return x + tan(x)

roots = root(equation, 0)

print(roots.x)

Output:

[0.]

from scipy.optimize import root
from math import cos

def equation(x):
  return x + cos(x)

roots = root(equation, 0)

print(roots.x)

Output

[-0.73908513]

import numpy as np
from scipy.optimize import root
def func(x):
  return x*22 * np.cos(x)
sol = root(func, 0.6)
print(sol)

Output

fjac: array([[-1.]])

     fun: array([2.44249065e-14])

 message: 'The solution converged.'

    nfev: 10

     qtf: array([-1.12716221e-08])

       r: array([-3.34723125])

  status: 1

 success: True

       x: array([-0.73908513]

 

Minimization Of a Scalar function

from scipy import optimize
import numpy as np

def function(a):
      return   a*420 * np.sin(a)

#use BFGS algorithm for optimization
optimize.fmin_bfgs(function, 0)

 

Output

Optimization terminated successfully.

         Current function value: -26.684535

         Iterations: 4

         Function evaluations: 12

         Gradient evaluations: 6

Out[27]:array([-1.77215427])

Using bfgs, minimal can be local, so we use basin hopping. It always returns global minimal.

from scipy import optimize
import numpy as np

def function(a):
      return   a*420 * np.sin(a)
optimize.basinhopping(function, 0)

 

Output

            fun: -26.684534932606336

 lowest_optimization_result:       fun: -26.684534932606336

 hess_inv: array([[0.05102345]])

      jac: array([2.38418579e-07])

  message: 'Optimization terminated successfully.'

     nfev: 10

      nit: 3

     njev: 5

   status: 0

  success: True

        x: array([-1.77215425])

                    message: ['requested number of basinhopping iterations completed successfully']

      minimization_failures: 0

                       nfev: 1026

                        nit: 100

                       njev: 513

                          x: array([-1.77215425])

 

 

Nelder-Mead

The Nelder-Mead is used to find the minimum or maximum in a multidimensional space. In the below example, the minimize method is used along with the Nelder-Mead algorithm.

 

The simplex algorithm is the simplest way to minimize a function. It requires only function evaluations and is a good choice for simple minimization problems. However, it may take longer to find the minimum because it does not use gradient evaluations.

from scipy import optimize
a = [2.73.90.2]
b = optimize.minimize(optimize.rosen, a, method='Nelder-Mead')
b.x

Output

array([0.99999837, 0.99999849, 0.99999426])

 

Spatial Data Structures and Algorithms

Spatial data refers to objects made up of lines, points, surfaces, etc.,i.e.,  refers to data represented in a geometric space. scipy.spatial is a module that works with spatial data. Examples are triangulations, convex hulls, and KD trees for nearest neighbors.

 

Triangulation

It means to divide a polygon into multiple triangles so that we can calculate the area of the given polygon.

 

A Triangulation with points means creating surface-composed triangles so that all the given points at least lie on one of the vertices of any triangle on the surface.

Delaunay() Triangulation is one of the methods to create triangulation through points.

import numpy as np
from scipy.spatial import Delaunay
import matplotlib.pyplot as plt

points = np.array([ [14], [24],[10], [22], [31]])

deltri = Delaunay(points).simplices

plt.triplot(points[:, 0], points[:, 1], deltri)
plt.scatter(points[:, 0], points[:, 1],color='g')

plt.show()

 

Output

There are various Distance Metrics used to find types of distances between two points in data science, Euclidean distance, cosine distance, manhattan distance, etc.

 

It's not compulsory that the distance between two vectors can only be the length of the straight line between them, but it can also be the angle between them from origin or number of unit steps required, etc.

 

Cosine Distance

It is the value of the cosine angle between the two points.

from scipy.spatial.distance import cosine

p1 = (6,4)
p2 = (126)

res = cosine(p1, p2)

print(res)

Output

0.007722123286332261

 

Hamming Distance

It is a way to measure the distance between two binary sequences.

from scipy.spatial.distance import hamming
p1 = (TrueFalseTrue)
p2 = (TrueTrueFalse)

res = hamming(p1, p2)

print(res)

Output

0.6666666666666666

 

Multidimensional Image Processing Functions

scipy.ndimage is a submodule of SciPy for performing image processing.

ndimage refers to the "n" dimensional image. SciPy Image Processing provides Geometrics transformation, image filtering (sharp and de nosing), display image, image segmentation, classification, and features extraction. MISC Package in SciPy contains prebuilt photos that can be used to perform an image manipulation task.

from scipy import misc
import matplotlib.pyplot as plt
pic = misc.face()
plt.imshow(pic)
plt.show()

Output

 

Any image combines colors that numbers in a matrix can represent.

 

Let us see some information about our image.

from scipy import misc
pic = misc.face(gray = False)
print(pic.mean(), pic.max(), pic.min())

Output

110.16274388631184 255 0

 

Flipping

flip_ud_face = np.flipud(pic)
plt.imshow(flip_ud_face)
plt.show()

Output

 

Filtering

from scipy import misc,ndimage
blurred_face = ndimage.gaussian_filter(pic, sigma=3)
plt.imshow(blurred_face)
plt.show()

Output

 

Rotating

rotate_face = ndimage.rotate(blurred_face, 45)

import matplotlib.pyplot as plt
plt.imshow(rotate_face)
plt.show()

Output

 

File Input and Output

The Scipy.io package provides various functions to work around the different file formats. Matlab, TXt, CSV, binary format, and many more.

 

To make use of this package, you will need to import it as follows:

import scipy.io as sio

Example:

import scipy.io as sio
import numpy as np

#Save a mat file
array = np.ones((5,7))
sio.savemat('input.mat', {'ar': array})

#Now Load the File
data = sio.loadmat('input.mat')
data['ar']

Output

array([[1., 1., 1., 1., 1., 1., 1.],

       [1., 1., 1., 1., 1., 1., 1.],

       [1., 1., 1., 1., 1., 1., 1.],

       [1., 1., 1., 1., 1., 1., 1.],

       [1., 1., 1., 1., 1., 1., 1.]])

 

 

Frequently Asked Questions

  1. Is Scipy faster than numpy?
    NumPy is faster than SciPy in all aspects of execution. It is suitable for computing data and statistics and basic mathematical calculation. SciPy is ideal for the complex computing of numerical data.
     
  2. What can we do with SciPy?
    SciPy is an open-source library used for solving mathematical, scientific, engineering, and technical problems. It helps manipulate and visualize the data using a wide range of high-level Python commands.
     
  3. Why is Scipy so fast?
    Because the Numpy array is densely packed in memory due to its homogeneous type, it also frees the memory faster. 

Key Takeaways

Let us brief the article.

Firstly we saw what scipy, specific differences between scipy and numpy are. Furthermore, we looked into the sub-packages present in scipy and had detailed discussions on some of them. Scipy is computationally cheaper and has made scientific and mathematical calculations easy.

I hope you all like this article.

Happy Learning Ninjas!

Live masterclass