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:

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([ 9, 0.1254, 234]) print ans

Output

[ 2.08008382 0.50053277 6.16224015]

from scipy.special import exp10 res = exp10([2, 9]) 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)))

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)

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(0, 4, 30) y = np.cos(x**2/3+8)

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.

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

defequation(x): return x + tan(x)

roots = root(equation, 0)

print(roots.x)

Output:

[0.]

from scipy.optimize import root from math import cos

defequation(x): return x + cos(x)

roots = root(equation, 0)

print(roots.x)

Output

[-0.73908513]

import numpy as np from scipy.optimize import root deffunc(x): return x*2 + 2 * 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

deffunction(a): return a*4 + 20 * 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.

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.7, 3.9, 0.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

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 = (12, 6)

res = cosine(p1, p2)

print(res)

Output

0.007722123286332261

Hamming Distance

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

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.

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

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.

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.

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.