

{"id":21964,"date":"2018-07-20T03:30:13","date_gmt":"2018-07-19T22:00:13","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=21964"},"modified":"2026-04-27T16:07:40","modified_gmt":"2026-04-27T10:37:40","slug":"scipy-tutorial","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/","title":{"rendered":"Python SciPy Tutorial &#8211; What is SciPy &amp; How to Install SciPy"},"content":{"rendered":"<p>In our previous Python Library\u00a0tutorial, we saw\u00a0Python Matplotlib.\u00a0Today, we bring you a tutorial on Python SciPy.<\/p>\n<p>Here in this SciPy Tutorial, we will learn the benefits of Linear Algebra, the working of Polynomials, and how to install SciPy. Moreover, we will cover the Processing Signals with SciPy and Processing Images with SciPy.<\/p>\n<p>So, let&#8217;s start the\u00a0Python SciPy Tutorial.<\/p>\n<div id=\"attachment_21999\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/SciPy-Tutorial-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21999\" class=\"wp-image-21999 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/SciPy-Tutorial-01.jpg\" alt=\"SciPy Tutorial - Linear Algebra, Benefits, Special Functions\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/SciPy-Tutorial-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/SciPy-Tutorial-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/SciPy-Tutorial-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/SciPy-Tutorial-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/SciPy-Tutorial-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-21999\" class=\"wp-caption-text\">Python SciPy Tutorial &#8211; Linear Algebra, Benefits, Special Functions<\/p><\/div>\n<h3 class=\"western\">What is SciPy in Python?<\/h3>\n<p>Python SciPy is open-source and BSD-licensed. You can pronounce it as Sigh Pie. Here are some shorts on it:<\/p>\n<ul>\n<li><strong>Author:<\/strong> Travis Oliphant, Pearu Peterson, Eric Jones.<\/li>\n<li><strong>First Release:<\/strong> 2001<\/li>\n<li><strong>Stable Release:<\/strong>\u00a0Version 1.1.0; May, 2018.<\/li>\n<li><strong>Written in:<\/strong>\u00a0Fortran, C, C++, Python Programming Language.<\/li>\n<\/ul>\n<p><strong>Python SciPy has modules for the following tasks:<\/strong><\/p>\n<ul>\n<li>Optimization<\/li>\n<li>Linear algebra<\/li>\n<li>Integration<\/li>\n<li>Interpolation<\/li>\n<li>Special functions<\/li>\n<li>FFT<\/li>\n<li>Signal and Image processing<\/li>\n<li>ODE solvers<\/li>\n<\/ul>\n<p>And as we\u2019ve seen, an important feature of the NumPy module is multidimensional arrays. This is what SciPy uses, too; it will work with NumPy arrays.<\/p>\n<p><strong>Use Python SciPy when:<\/strong><\/p>\n<ul>\n<li>You need to solve complex equations.<\/li>\n<li>You want to optimize performance or integration<\/li>\n<li>If you want to conduct an advanced signal processing or a strategic analysis.<\/li>\n<\/ul>\n<h3>SciPy Subpackages<\/h3>\n<p>In this Python SciPy Tutorial, we will study the following sub-packages of SciPy:<\/p>\n<ul>\n<li><strong>cluster-<\/strong> Hierarchical clustering.<\/li>\n<li><strong>constants-<\/strong> Physical constants and factors of conversion.<\/li>\n<li><strong>fftpack- <\/strong>Algorithms for Discrete Fourier Transform.<\/li>\n<li><strong>integrate-<\/strong> Routines for numerical integration.<\/li>\n<li><strong>interpolate-<\/strong> Tools for interpolation.<\/li>\n<li><strong>io-<\/strong> Input and Output tools for data.<\/li>\n<li><strong>lib-<\/strong> Wrappers to external libraries.<\/li>\n<li><strong>linalg-<\/strong> Routines for linear algebra.<\/li>\n<li><strong>misc-<\/strong> Miscellaneous utilities like image reading and writing.<\/li>\n<li><strong>ndimage-<\/strong> Functions for processing multidimensional images.<\/li>\n<li><strong>optimize-<\/strong> Algorithms for optimization.<\/li>\n<li><strong>signal-<\/strong> Tools for processing signals.<\/li>\n<li><strong>sparse-<\/strong> Algorithms for sparse matrices.<\/li>\n<li><strong>spatial-<\/strong> KD-trees, distance functions, nearest neighbors.<\/li>\n<li><strong>special-<\/strong> Special functions.<\/li>\n<li><strong>stats-<\/strong> Functions to perform statistics.<\/li>\n<li><strong>weave-<\/strong> Functionality that lets you write C++\/C code in the form of multiline strings.<\/li>\n<\/ul>\n<h3 class=\"western\">Benefits of SciPy<\/h3>\n<ul>\n<li>High-level commands and classes for visualizing and manipulating data.<\/li>\n<li>Powerful and interactive sessions with Python.<\/li>\n<li>Classes, web, and database routines for parallel programming.<\/li>\n<li>Easy and fast.<\/li>\n<li>Open-source.<\/li>\n<\/ul>\n<h3 class=\"western\">How to Install SciPy?<\/h3>\n<p>You can use pip to install SciPy-<\/p>\n<pre class=\"EnlighterJSRAW\">pip install scipy<\/pre>\n<p>You can also use conda for the same-<\/p>\n<pre class=\"EnlighterJSRAW\">conda install \u2013c anaconda scipy<\/pre>\n<p>Then, you can import SciPy as:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import scipy<\/pre>\n<p>You will also want to interact with numpy here. Let\u2019s import that too.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import numpy\r\n<\/pre>\n<p>Finally, in some places, we will want to plot our results. We will use matplotlib for that; let\u2019s import it.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import matplotlib<\/pre>\n<h3>Linear Algebra in SciPy<\/h3>\n<p>For performing operations of linear algebra in SciPy, we will need to import linalg from scipy-<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from scipy import linalg<\/pre>\n<div id=\"attachment_21995\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Performing-Operations-of-Linear-Algebra-in-SciPy-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21995\" class=\"wp-image-21995 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Performing-Operations-of-Linear-Algebra-in-SciPy-01.jpg\" alt=\"SciPy Tutorial - Linear Algebra\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Performing-Operations-of-Linear-Algebra-in-SciPy-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Performing-Operations-of-Linear-Algebra-in-SciPy-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Performing-Operations-of-Linear-Algebra-in-SciPy-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Performing-Operations-of-Linear-Algebra-in-SciPy-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Performing-Operations-of-Linear-Algebra-in-SciPy-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-21995\" class=\"wp-caption-text\">SciPy Tutorial &#8211; Linear Algebra<\/p><\/div>\n<h4>1. Solving a set of equations<\/h4>\n<p>Let\u2019s suppose this is the set of equations we want to solve-<br \/>\n2x+3y=7<br \/>\n3x+4y=10<br \/>\nLet\u2019s create input and solution arrays for this.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; A=numpy.array([[2,3],[3,4]])\r\n&gt;&gt;&gt; B=numpy.array([[7],[10]])\r\n&gt;&gt;&gt; linalg.solve(A,B)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>array([[ 2.], [ 1.]])<\/p>\n<\/div>\n<p>This tells us that both equations work for x=2 and y=1. To check the results, we run the following command-<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; A.dot(linalg.solve(A,B))-B<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>array([[ 0.], [ 0.]])<\/p>\n<\/div>\n<p>The output confirms the correctness of the computation.<\/p>\n<h4 class=\"western\">2. Determinants of Square Matrices<\/h4>\n<p>To calculate the determinant for a square matrix, we can use the det() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt;mat=numpy.array([[8,2],[1,4]])\r\n&gt;&gt;&gt;linalg.det(mat)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>30.0<\/p>\n<\/div>\n<h4 class=\"western\">3. An inverse of a Square Matrix<\/h4>\n<p>For this, we use the inv() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; linalg.inv(mat)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">array([[ 0.13333333, -0.06666667],<br \/>\n[-0.03333333, 0.26666667]])<\/div>\n<h4 class=\"western\">4. Singular Value Decomposition<\/h4>\n<p>This uses the method svd().<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; linalg.svd(mat)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(array([[-0.9610057 , -0.27652857],<br \/>\n[-0.27652857, 0.9610057 ]]),<br \/>\narray([ 8.52079729, 3.52079729]),<br \/>\narray([[-0.9347217 , -0.35538056],<br \/>\n[-0.35538056, 0.9347217 ]]))<\/div>\n<h4 class=\"western\">5. LU Decomposition<\/h4>\n<p>To carry out LU decomposition, we can use the lu() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; linalg.lu(mat)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(array([[ 1., 0.],<br \/>\n[ 0., 1.]]), array([[ 1. , 0. ],<br \/>\n[ 0.125, 1. ]]), array([[ 8. , 2. ],<br \/>\n[ 0. , 3.75]]))<\/div>\n<h3 class=\"western\">Working with Polynomials in SciPy<\/h3>\n<p>SciPy will let us work with polynomials using the poly1d type from numpy:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from numpy import poly1d\r\n&gt;&gt;&gt; p=poly1d([3,2,4])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<br \/>\n3 x + 2 x + 4<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; p*p<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">4\u00a0 \u00a0 \u00a0 \u00a0 3\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a02<br \/>\n9 x + 12 x + 28 x + 16 x + 16<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; p(5) #Value of polynomial for x=5<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>89<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; p.integ(k=6) #Integration<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>poly1d([ 1., 1., 4., 6.])<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; p.deriv() #Finding derivatives<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>poly1d([6, 2])<\/p>\n<\/div>\n<h3 class=\"western\">Integration with Python SciPy<\/h3>\n<p>To be able to perform integration, we will import the <i>integrate<\/i> subpackage from scipy.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from scipy import integrate<\/pre>\n<p>The quad() function will let us integrate a function; let\u2019s use a lambda for this.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; integrate.quad(lambda x:x**2,0,4) #Last two arguments are lower and upper limits<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>(21.333333333333336, 2.368475785867001e-13)<\/p>\n<\/div>\n<p>You can use help() function to find out what else this subpackage can do for you. Here are some more functions:<\/p>\n<ul>\n<li><strong>dblquad-<\/strong> Double integration<\/li>\n<li><strong>tplquad-<\/strong> Triple integration<\/li>\n<li><strong>nquad-<\/strong> n-dimensional integration<\/li>\n<li><strong>fixed_quad-<\/strong> Use Gaussian quadrature of order n to integrate func(x)<\/li>\n<li><strong>quadrature-<\/strong> Use Gaussian quadrature to integrate with certain tolerance<\/li>\n<li><strong>romberg-<\/strong> Romberg integration<\/li>\n<li><strong>quad_explain-<\/strong> Find out about quad<\/li>\n<li><strong>newton_cotes-<\/strong> Weights and coefficient of error with Newton-Cotes integration<\/li>\n<li><strong>IntegrationWarning-<\/strong> Warning for issues in integration<\/li>\n<\/ul>\n<h3 class=\"western\">Vectorizing Functions in SciPy<\/h3>\n<p>The class vectorizes will let us turn a regular Python function into a vectorized function. Let\u2019s try this out.<\/p>\n<p>We\u2019ll define a function first. If the first argument is greater than the second, it returns the sum of the two.<\/p>\n<p>Otherwise, it returns the product of the two.<\/p>\n<pre class=\"EnlighterJSRAW\">def addmul(a,b):\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0if a&gt;b:\r\n             return a+b\r\nelse:\r\n             return a*b<\/pre>\n<p>Now, let\u2019s vectorize this.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; numpy.vectorize(addmul)([1,4,3,9],[2,3,7,8])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>array([ 2, 7, 21, 17])<\/p>\n<\/div>\n<h3 class=\"western\">Fast Fourier Transforms in Python SciPy<\/h3>\n<p>If you\u2019re familiar with calculus, then there are chances are you may be familiar with Fourier Transforms. This analysis lets you express a function as a sum of periodic components. It is possible to recover the signal from this sum.<\/p>\n<p>Let\u2019s take an example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from scipy.fftpack import fft\r\n&gt;&gt;&gt; N=600\r\n&gt;&gt;&gt; T=1.0\/800.0\r\n&gt;&gt;&gt; x=np.linspace(0.0,N*T,N)\r\n&gt;&gt;&gt; y=numpy.sin(50.0*2.0*numpy.pi*x)+0.5*numpy.sin(80.0*2.0*numpy.pi*x)\r\n&gt;&gt;&gt; yf=fft(y)\r\n&gt;&gt;&gt; xf=numpy.linspace(0.0,1.0\/(2.0*T),N\/\/2)\r\n&gt;&gt;&gt; import matplotlib.pyplot as plt\r\n&gt;&gt;&gt; plt.plot(xf, 2.0\/N * numpy.abs(yf[0:N\/\/2]))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&lt;matplotlib.lines.Line2D at 0x7fbb78d65210&gt;]<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; plt.grid()\r\n&gt;&gt;&gt; plt.show()<\/pre>\n<div id=\"attachment_22000\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/fourier.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-22000\" class=\"wp-image-22000 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/fourier.png\" alt=\"Python SciPy\" width=\"640\" height=\"480\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/fourier.png 640w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/fourier-150x113.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/fourier-300x225.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><p id=\"caption-attachment-22000\" class=\"wp-caption-text\">SciPy Tutorial &#8211; Fast Fourier Transforms<\/p><\/div>\n<h3 class=\"western\">Special Functions of Python SciPy<\/h3>\n<p>The <i>special<\/i> module holds various transcendental functions that will help us with multiple tasks in mathematics.<\/p>\n<p>To import it, use the following command-<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from scipy import special<\/pre>\n<p>We make use of the various functions:<\/p>\n<div id=\"attachment_21998\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Special-Functions-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21998\" class=\"wp-image-21998 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Special-Functions-01.jpg\" alt=\"SciPy Tutorial - Special Functions of SciPy\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Special-Functions-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Special-Functions-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Special-Functions-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Special-Functions-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Special-Functions-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-21998\" class=\"wp-caption-text\">SciPy Tutorial &#8211; Special Functions of SciPy<\/p><\/div>\n<h4>1. ERF in SciPy<\/h4>\n<p>This function calculates the area under a Gaussian curve. We use the erf() function.<\/p>\n<h4>2. Gamma in SciPy<\/h4>\n<p>This calculates the Gamma. The function for this is gamma().<\/p>\n<h4>3. Gammaln in SciPy<\/h4>\n<p>This calculates the log of Gamma. We use the function gammaln().<\/p>\n<h4>4. eppilj in SciPy<\/h4>\n<p>The elliptic function we have is eppilj().<\/p>\n<h4>5. jn in SciPy<\/h4>\n<p>This is the N<sup>th<\/sup> order Bessel function.<\/p>\n<h3 class=\"western\">Processing Signals with Python SciPy<\/h3>\n<p>SciPy will also help you with signal processing. Let\u2019s take an example.<\/p>\n<div id=\"attachment_21996\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Processing-Signals-with-SciPy-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21996\" class=\"wp-image-21996 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Processing-Signals-with-SciPy-01.jpg\" alt=\"SciPy Tutorial - Processing Signals with SciPy\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Processing-Signals-with-SciPy-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Processing-Signals-with-SciPy-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Processing-Signals-with-SciPy-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Processing-Signals-with-SciPy-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Processing-Signals-with-SciPy-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-21996\" class=\"wp-caption-text\">SciPy Tutorial &#8211; Processing Signals with SciPy<\/p><\/div>\n<h4 class=\"western\">1. Using FFT to resample<\/h4>\n<p>We can resample a function to n points in a time domain interval. We can use the function resample() for this.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; t=numpy.linspace(-10,10,200)\r\n&gt;&gt;&gt; y=numpy.sin(t)\r\n&gt;&gt;&gt; from scipy import signal\r\n&gt;&gt;&gt; signal.resample(y,100)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">array([ 0.25070765, 0.44580125, 0.12797317, 0.00253488, -0.24973473,<br \/>\n-0.39831149, -0.60510157, -0.72607055, -0.8701493 , -0.9336745 ,<br \/>\n-0.99979334, -0.98973944, -0.97222903, -0.88612051, -0.79126725,<br \/>\n-0.63978568, -0.48541023, -0.29029666, -0.10319111, 0.10642378,<br \/>\n0.29461537, 0.48697311, 0.64470867, 0.79055976, 0.89136943,<br \/>\n#More output here<\/div>\n<h4 class=\"western\">2. Removing Linear Trends<\/h4>\n<p>The detrend() function will remove the linear element from the signal; this gives us a transient solution.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; t=numpy.linspace(-10,10,200)\r\n&gt;&gt;&gt; y=numpy.sin(t)+t\r\n&gt;&gt;&gt; signal.detrend(y)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">array([ 0.76761293, 0.67843367, 0.58464135, 0.48715987, 0.38695036,<br \/>\n0.28500148, 0.18231944, 0.07991785, -0.02119249, -0.12001383,<br \/>\n-0.21557153, -0.30692387, -0.39317157, -0.47346689, -0.54702214,<br \/>\n-0.61311766, -0.67110907, -0.7204338 , -0.76061672, -0.79127497,<br \/>\n-0.81212183, -0.82296958, -0.82373141, -0.81442233, -0.79515896,<br \/>\n#More output here<\/div>\n<h3 class=\"western\">Processing Images with Python SciPy<\/h3>\n<p>With SciPy, you can use ndimage to process images.<\/p>\n<p>Some of the possible transitions are opening and closing images, geometrical transformation(shape, resolution, orientation), image filtering, and filters like erosion and dilation.<\/p>\n<p>You can import it as:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from scipy import ndimage\r\n<\/pre>\n<p><strong>Let\u2019s look at some functions we have:<\/strong><\/p>\n<h4 class=\"western\">1. Shift in SciPy<\/h4>\n<p>This function will shift the image along the x and y coordinates. The function is shift(image,(x,y)).<\/p>\n<h4>2. Rotate in SciPy<\/h4>\n<p>When you want to rotate your image to an angle, use the rotate(image, angle) function.<\/p>\n<h4>3. Zoom in SciPy<\/h4>\n<p>With Zoom (image, magnitude), you can zoom in or out on an image.<\/p>\n<h4>4. Median Filter in SciPy<\/h4>\n<p>To filter an image with a Median filter, you can use median_filter(image, argument).<\/p>\n<h4>5. Gaussian Filter in SciPy<\/h4>\n<p>And to filter with a Gaussian filter, you use gaussian_filter(image, argument).<\/p>\n<h4>6. Opening an Image in Binary<\/h4>\n<p>For this, you use the function binary_opening(image)<\/p>\n<h4>7. Closing an Image in SciPy<\/h4>\n<p>To close this image, make a call to binary_closing(opened_image)<\/p>\n<h4>8. Binary Erosion in SciPy<\/h4>\n<p>For this task, we use the function binary_erosion(image)<\/p>\n<h4>9. Binary Dilation in SciPy<\/h4>\n<p>And to perform dilation, call binary_dilation(image)<\/p>\n<h3 class=\"western\">Optimizing in Python SciPy<\/h3>\n<p>For algorithms that will optimize, we need the <i>optimize<\/i> package. Import it this way-<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from scipy import optimize<\/pre>\n<p>Let\u2019s take a demo piece of code to explain this.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from scipy.optimize import least_squares\r\n&gt;&gt;&gt; def func1(x):\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return numpy.array([10 * (x[1] - x[0]**2), (1 - x[0])])\r\n&gt;&gt;&gt; input=numpy.array([2, 2])\r\n&gt;&gt;&gt; res=least_squares(func1, input)\r\n&gt;&gt;&gt; res<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">active_mask: array([ 0., 0.])<br \/>\ncost: 0.0<br \/>\nfun: array([ 0., 0.])<br \/>\ngrad: array([ 0., 0.])<br \/>\njac: array([[-20.00000015, 10. ],<br \/>\n[ -1. , 0. ]])<br \/>\nmessage: &#8216;`gtol` termination condition is satisfied.&#8217;<br \/>\nnfev: 4<br \/>\nnjev: 4<br \/>\noptimality: 0.0<br \/>\nstatus: 1<br \/>\nsuccess: True<br \/>\nx: array([ 1., 1.])<\/div>\n<h3 class=\"western\">Working with Stats sub-packages of Python SciPy<\/h3>\n<p>The <i>stats<\/i> subpackage lets us work with statistics. Let\u2019s take a few examples.<br \/>\nfrom scipy.stats import norm.<\/p>\n<div id=\"attachment_21997\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Working-with-Stats-subpackages-of-SciPy-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-21997\" class=\"wp-image-21997 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Working-with-Stats-subpackages-of-SciPy-01.jpg\" alt=\"SciPy Tutorial - Working with Stats sub-packages of SciPy\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Working-with-Stats-subpackages-of-SciPy-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Working-with-Stats-subpackages-of-SciPy-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Working-with-Stats-subpackages-of-SciPy-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Working-with-Stats-subpackages-of-SciPy-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/Working-with-Stats-subpackages-of-SciPy-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-21997\" class=\"wp-caption-text\">SciPy Tutorial &#8211; Working with Stats sub-packages of SciPy<\/p><\/div>\n<h4 class=\"western\">1. CDF Removing Linear Trends<\/h4>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; norm.cdf(numpy.array([1,-1.,3,1,0,4,-6,2]))<\/pre>\n<p>This function computes the cumulative distribution at the points we mention.<\/p>\n<h4 class=\"western\">2. PPF in SciPy<\/h4>\n<p>For the Percent Point Function, we use the function ppf().<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; norm.ppf(0.5)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">0.0<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; norm.ppf(2.3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">nan<\/div>\n<h4 class=\"western\">3. RVS in SciPy<\/h4>\n<p>For a random variate sequence, use rvs().<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; norm.rvs(size=7)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">array([-1.42745064, -0.07120524, -0.88890304, 1.34584415, 1.24426647,<br \/>\n-1.57490259, 0.24552959])<\/div>\n<h4 class=\"western\">4. Binomial Distribution in SciPy<\/h4>\n<p>For this, we use the cdf() function from uniform.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from scipy.stats import uniform\r\n&gt;&gt;&gt; uniform.cdf([0,1,2,3,4,5,6,7],loc=1,scale=3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">array([ 0. , 0. , 0.33333333, 0.66666667, 1. ,<br \/>\n1. , 1. , 1. ])<\/div>\n<p>So, this was all Python SciPy Tutorial. Hope you like our explanation.<\/p>\n<h3>Python Interview Questions on SciPy<\/h3>\n<ol>\n<li>What is SciPy in Python?<\/li>\n<li>What is SciPy in Python used for?<\/li>\n<li>How to import a SciPy module into Python?<\/li>\n<li>What is SciPy optimize?<\/li>\n<li>What is the difference between NumPy and SciPy in Python?<\/li>\n<\/ol>\n<h3 class=\"western\">Conclusion<\/h3>\n<p>Hence, in this SciPy tutorial, we studied the introduction to SciPy with all its benefits and the installation process.<\/p>\n<p>At last, we discussed several operations used by Python SciPy like Integration, Vectorizing Functions, Fast Fourier Transforms, Special Functions, Processing Signals, Processing Images, Optimize package in SciPy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our previous Python Library\u00a0tutorial, we saw\u00a0Python Matplotlib.\u00a0Today, we bring you a tutorial on Python SciPy. Here in this SciPy Tutorial, we will learn the benefits of Linear Algebra, the working of Polynomials, and&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":21999,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[375,1792,6228,6979,9298,10159,12636,13181,13956,15360],"class_list":["post-21964","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-advantages-of-scipy","tag-benefits-of-scipy","tag-how-to-install-scipy","tag-intreoduction-to-scipy","tag-optimize-package-in-scipy","tag-processing-images-with-scipy","tag-scipy-advantages","tag-special-functions-of-scipy","tag-subpackages-of-scipy","tag-vectorizing-functions-of-scipy"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python SciPy Tutorial - What is SciPy &amp; How to Install SciPy - DataFlair<\/title>\n<meta name=\"description\" content=\"Python SciPy is open-source and BSD-licensed. You can pronounce it as Sigh Pie. Let&#039;s see how to install SciPy and process images with SciPy.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python SciPy Tutorial - What is SciPy &amp; How to Install SciPy - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Python SciPy is open-source and BSD-licensed. You can pronounce it as Sigh Pie. Let&#039;s see how to install SciPy and process images with SciPy.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-19T22:00:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-27T10:37:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/SciPy-Tutorial-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python SciPy Tutorial - What is SciPy &amp; How to Install SciPy - DataFlair","description":"Python SciPy is open-source and BSD-licensed. You can pronounce it as Sigh Pie. Let's see how to install SciPy and process images with SciPy.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"Python SciPy Tutorial - What is SciPy &amp; How to Install SciPy - DataFlair","og_description":"Python SciPy is open-source and BSD-licensed. You can pronounce it as Sigh Pie. Let's see how to install SciPy and process images with SciPy.","og_url":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-07-19T22:00:13+00:00","article_modified_time":"2026-04-27T10:37:40+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/SciPy-Tutorial-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python SciPy Tutorial &#8211; What is SciPy &amp; How to Install SciPy","datePublished":"2018-07-19T22:00:13+00:00","dateModified":"2026-04-27T10:37:40+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/"},"wordCount":1558,"commentCount":4,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/SciPy-Tutorial-01.jpg","keywords":["Advantages of SciPy","Benefits of SciPy","How to Install SciPy","Intreoduction to SciPy","Optimize package in SciPy","Processing Images with SciPy","SciPy Advantages","Special Functions of SciPy","Subpackages of SciPy","Vectorizing Functions of SciPy"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/scipy-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/","url":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/","name":"Python SciPy Tutorial - What is SciPy &amp; How to Install SciPy - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/SciPy-Tutorial-01.jpg","datePublished":"2018-07-19T22:00:13+00:00","dateModified":"2026-04-27T10:37:40+00:00","description":"Python SciPy is open-source and BSD-licensed. You can pronounce it as Sigh Pie. Let's see how to install SciPy and process images with SciPy.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/scipy-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/SciPy-Tutorial-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/07\/SciPy-Tutorial-01.jpg","width":1200,"height":628,"caption":"SciPy Tutorial - Linear Algebra, Benefits, Special Functions"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/scipy-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python SciPy Tutorial &#8211; What is SciPy &amp; How to Install SciPy"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/21964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=21964"}],"version-history":[{"count":15,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/21964\/revisions"}],"predecessor-version":[{"id":147933,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/21964\/revisions\/147933"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/21999"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=21964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=21964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=21964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}