Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

SCIPY in Data Science:-

Scipy is the special library of python which is used to perform an advanced mathematical operation using the different predefined methods.
It is the top layer of NumPy because NumPy is used to perform the basic mathematical operation, scipy mostly focus on linear algebra and other advanced mathematical function.

SCIPY was written as a SIGH PY word.

How we install scipy in the machine?

if you want to install scipy with core python or python shell then you can use the command

python -m pip install scipy.

Q)Create Script to write File in Matlab format and load it using scipy library?

import numpy as np
from scipy import io as scs
array = np.zeros((4, 4))
scs.savemat('exm.mat', {'key': array}) 
data = scs.loadmat('exm.mat', struct_as_record=True)

Note:- Numpy support only CSV, txt format data but scipy can write data in Matlab format which is used in many Matlab device systems.

Scipy, I/O package, has a wide range of functions for work with different files format which are Matlab, Arff, Wave, Matrix Market, IDL, NetCDF, TXT, CSV, and binary format.

Special Function package:-

It provides multiple predefined methods to perform a mathematical operation using scipy.special.

SciPy special function includes Cubic Root, Exponential, Log sum Exponential, Lambert, Permutation and Combinations, Gamma, Bessel, hypergeometric, Kelvin, beta, parabolic cylinder, Relative Error Exponential, etc..

Cubic root function in python:-

Script to calculate cube using python:-

from scipy.special import cbrt
#Find cubic root of 27 & 64 using cbrt() function
cb = cbrt([27, 64])
#print value of cb
Exponential function:-

from scipy.special import exp10
#define exp10 function and pass value in its
exp = exp10([1,10])

Permutations & Combinations:

SciPy also gives functionality to calculate Permutations and Combinations.
Combinations - scipy.special.comb(N,k)

from scipy.special import comb
#find combinations of 5, 2 values using comb(N, k)
com = comb(5, 2, exact = False, repetition=True)
Permutations –
from scipy.special import perm
#find permutation of 5, 2 using perm (N, k) function
per = perm(5, 2, exact = True)

Log Sum Exponential Function

Log Sum Exponential computes the log of the sum exponential input element.
Syntax :

from scipy.special import logsumexp
#define exp10 function and pass value in its
exp = logsumexp([2,3,4])

Bessel Function

Nth integer order calculation function
Syntax :
it uses this formula internally

from scipy.special import Jn
#define exp10 function and pass value in its
exp = jn(2,3)


Linear Algebra with SciPy:-

  • Linear Algebra of SciPy is an implementation of BLAS and ATLAS LAPACK libraries.
  • Performance of Linear Algebra is very fast compared to BLAS and LAPACK.
  • Linear algebra routine accepts two-dimensional array object and output is also a two-dimensional array.
from scipy import linalg
import numpy as np
#define square matrix
two_d_array = np.array([ [4,5], [3,2] ])
#pass values to det() function
linalg.det( two_d_array )

Discrete Fourier Transform – scipy.fftpack

  • DFT is a mathematical technique which is used in converting spatial data into frequency data.
  • FFT (Fast Fourier Transformation) is an algorithm for computing DFT
  • FFT is applied to a multidimensional array.
  • Frequency defines the number of signals or wavelengths in a particular time period.
Example: Take a wave and show using the Matplotlib library. we take a simple periodic function example of sin(20 × 2πt)

%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np 

#Frequency in terms of Hertz
fre  = 5 
#Sample rate
fre_samp = 50
t = np.linspace(0, 2, 2 * fre_samp, endpoint = False )
a = np.sin(fre  * 2 * np.pi * t)
figure, axis = plt.subplots()
axis.plot(t, a)
axis.set_xlabel ('Time (s)')
axis.set_ylabel ('Signal amplitude')
You can see this. Frequency is 5 Hz and its signal repeats in 1/5 seconds – it's call as a particular time period.
Now let us use this sinusoid wave with the help of DFT application.
%matplotlib inline
from matplotlib import pyplot as plt
from scipy import fftpack
A = fftpack.fft(a)
frequency = fftpack.fftfreq(len(a)) * fre_samp
figure, axis = plt.subplots()
axis.stem(frequency, np.abs(A))
axis.set_xlabel('Frequency in Hz')
axis.set_ylabel('Frequency Spectrum Magnitude')
axis.set_xlim(-fre_samp / 2, fre_samp/ 2)
axis.set_ylim(-5, 110)

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post