Q1. What is the difference between list and tuples in Python? LIST vs TUPLES LIST TUPLES Lists are mutable i.e they can be edited. Tuples are immutable (tuples are lists which can’t be edited). Lists are slower than tuples. Tuples are faster than list. Syntax: list_1 = [10, ‘Chelsea’, 20] Syntax: tup_1 = (10, ‘Chelsea’ , 20) Q2. What are the key features of Python? Python is an interpreted language. That means that, unlike languages like C and its variants, Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby. Python is dynamically typed, this means that you don’t need…
-
-
Image Processing with SciPy – scipy.ndimage scipy.ndimage is a submodule of SciPy which is mostly used for performing an image related operation ndimage means the “n” dimensional image. SciPy Image Processing provides Geometrics transformation (rotate, crop, flip), image filtering (sharp and de nosing), display image, image segmentation, classification and features extraction. MISC Package in SciPy contains prebuilt images which can be used to perform image manipulation task Example: Let’s take a geometric transformation example of images from scipy import misc from matplotlib import pyplot as plt import numpy as np #get face image of panda from misc package panda = misc.face()…
-
Nelder –Mead Algorithm: Nelder-Mead algorithm selects through method parameter. It provides the most straightforward way of minimization for fair behaved function. Nelder – Mead algorithm is not used for gradient evaluations because it may take a longer time to find the solution. import numpy as np from scipy.optimize import minimize #define function f(x) def f(x): return .4*(1 - x[0])**2 optimize.minimize(f, [2, -1], method="Nelder-Mead") Output: final_simplex: (array([[ 1. , -1.27109375], [ 1. , -1.27118835], [ 1. , -1.27113762]]), array([0., 0., 0.])) fun: 0.0 message: 'Optimization terminated successfully.' nfev: 147 nit: 69 status: 0 success: True x: array([ 1. , -1.27109375])
-
Optimization and Fit in SciPy – scipy.optimize Optimization provides a useful algorithm for minimization of curve fitting, multidimensional or scalar and root fitting. Let’s take an example of a Scalar Function, to find minimum scalar function %matplotlib inline import matplotlib.pyplot as plt from scipy import optimize import numpy as np def function(a): return a*2 + 20 * np.sin(a) plt.plot(a, function(a)) plt.show() #use BFGS algorithm for optimization optimize.fmin_bfgs(function, 0) Output: Optimization terminated successfully. Current function value: -23.241676 Iterations: 4 Function evaluations: 18 Gradient evaluations: 6 array([-1.67096375]) In this example, optimization is done with the help of the gradient descent algorithm from the initial…
-
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 signal or wavelength in particular time period. Example: Take a wave and show using Matplotlib library. we take 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,…
-
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. Now let’s do some test with scipy.linalg, Calculating determinant of a two-dimensional matrix, 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 ) Output: -7.0 Inverse Matrix – scipy.linalg.inv() Inverse Matrix of Scipy calculates the inverse of any square matrix. Let’s see, from scipy import…
-
Special Function package scipy.special package contains numerous functions of mathematical physics. 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.. For one line description all of these function, type in Python console: help(scipy.special) Output : NAME scipy.special DESCRIPTION ======================================== Special functions (:mod:`scipy.special`) ======================================== .. module:: scipy.special Nearly all of the functions below are universal functions and follow broadcasting and automatic array-looping rules. Exceptions are noted. Cubic Root Function: Cubic Root function finds the cube root of values. Syntax: scipy.special.cbrt(x) Example: from scipy.special import cbrt #Find…
-
File Input / Output package: 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. Let’s we take one file format example as which are regularly use of MatLab: import numpy as np from scipy import io as sio array = np.ones((4, 4)) sio.savemat('example.mat', {'ar': array}) data = sio.loadmat(‘example.mat', struct_as_record=True) data['ar'] Output: array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]]) Code Explanation Line 1 & 2: Import the essential library…
-
Numpy VS SciPy Numpy: Numpy is written in C and use for mathematical or numeric calculation. It is faster than other Python Libraries Numpy is the most useful library for Data Science to perform basic calculations. Numpy contains nothing but array data type which performs the most basic operation like sorting, shaping, indexing, etc. SciPy: SciPy is built in top of the NumPy SciPy is a fully-featured version of Linear Algebra while Numpy contains only a few features. Most new Data Science features are available in Scipy rather than Numpy. Functions Ideally speaking, NumPy is basically for basic operations…
-
SciPy, pronounced as Sigh Pi, is a scientific python open source, distributed under the BSD licensed library to perform Mathematical, Scientific and Engineering Computations. SciPy is built in top of the NumPy SciPy is a fully-featured version of Linear Algebra while Numpy contains only a few features. Most new Data Science features are available in Scipy rather than Numpy. SciPy works efficiently on NumPy arrays and is standard scientific computing library in Python. SciPy library is composed of sub-modules designed for specific tasks. File input/output – scipy.io Special Function – scipy.special Linear Algebra Operation – scipy.linalg Interpolation – scipy.interpolate Optimization and fit – scipy.optimize Statistics…