{"id":9524,"date":"2019-02-10T17:25:53","date_gmt":"2019-02-10T17:25:53","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=9524"},"modified":"2019-02-11T00:12:12","modified_gmt":"2019-02-11T00:12:12","slug":"python-scipy-tutorial-linux-hint","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/02\/10\/python-scipy-tutorial-linux-hint\/","title":{"rendered":"Python SciPy Tutorial \u2013 Linux Hint"},"content":{"rendered":"<p>We will see what is the use of SciPy library in Python and how it helps us to work with mathematical equations and algorithms in an interactive manner. The good thing about SciPy Python package is that if we want classes or construct web pages, SciPy is fully compatible with the system as a whole and can provide seamless integration.<\/p>\n<div class=\"qX6eMJHT\"><span id=\"ezoic-pub-ad-placeholder-134\" class=\"ezoic-adpicker-ad\"><\/span><\/p>\n<div id=\"google_ads_iframe_\/1254144\/linuxhint_com-medrectangle-3_0__container__\">\n<p>As\u00a0<strong>SciPy is open source<\/strong>, it has a very active and vibrant community of developers due to which there are enormous number of modules present for a vast amount of scientific applications and calculations available with SciPy. Some of the complex mathematical operations which can be performed with SciPy are:<\/p>\n<div class=\"4Mx9IfJ1\"><\/div>\n<ul>\n<li>Interpolation<\/li>\n<li>Integration<\/li>\n<li>Optimization<\/li>\n<li>Image processing<\/li>\n<li>Statistics<\/li>\n<li>Special function calculations etc.<\/li>\n<\/ul>\n<p>SciPy can be compared to most command and standard libraries like GSL library for C++ and Matlab. As SciPy is built on top of NumPy package, these two packages can be integrated completely as well. If you can think of a mathematical operation which needs to be done, make sure you check SciPy library before you implement that module on your own because in most cases, SciPy have all the operations for you fully implemented already.<\/p>\n<div class=\"5aY9FjjJ\"><\/div>\n<h3><strong>Install SciPy Library<\/strong><\/h3>\n<p>Let\u2019s install SciPy library before we move to the actual examples and concepts. There are two ways to install this package. First one includes using the Python package manager, pip:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">pip install scipy<\/div>\n<div><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">The second way relates to Anaconda, we can install the package as:<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">conda install -c anaconda scipy<\/div>\n<div><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Once the library is installed, we can import it as:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">import<\/span>\u00a0scipy<\/div>\n<div><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Finally, as we will be using\u00a0<a href=\"https:\/\/linuxhint.com\/python_numpy_tutorial\/\">NumPy<\/a>\u00a0as well (It is recommended that for all\u00a0<a href=\"https:\/\/linuxhint.com\/python_numpy_tutorial\/\">NumPy<\/a>\u00a0operations, we use NumPy directly instead of going through the SciPy package):<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">import<\/span>\u00a0numpy<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">It is possible that in some cases, we will also like to plot our results for which we will use the\u00a0<a href=\"https:\/\/linuxhint.com\/python_matplotlib_tutorial\/\">Matplotlib<\/a>\u00a0library. Perform the following import for that library:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">import<\/span>\u00a0matplotlib<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I will be using the Anaconda manager for all the examples in this lesson. I will launch a Jupyter Notebook for the same:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-36341\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/1-8.png\" sizes=\"auto, (max-width: 773px) 100vw, 773px\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/1-8.png 773w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/1-8-300x179.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/1-8-768x458.png 768w\" alt=\"\" width=\"773\" height=\"461\" \/><\/p>\n<p>Now that we are ready with all the import statements to write some code, let\u2019s start diving into SciPy package with some practical examples.<\/p>\n<h3><strong>Working with Polynomial Equations<\/strong><\/h3>\n<p>We will start by looking at simple Polynomial equations. There are two ways with which we can integrate Polynomial functions into our program. We can make use of\u00a0<em>poly1d<\/em>\u00a0class which makes use of coefficients or the roots of a polynomial for initialising a polynomial. Let\u2019s look at an example:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">from<\/span>\u00a0numpy\u00a0<span class=\"kw1\">import<\/span>\u00a0poly1d<br \/>\nfirst_polynomial\u00a0<span class=\"sy0\">=<\/span>\u00a0poly1d<span class=\"br0\">(<\/span><span class=\"br0\">[<\/span><span class=\"nu0\">3<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">4<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">7<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>first_polynomial<span class=\"br0\">)<\/span><\/div>\n<div><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">When we run this example, we will see the following output:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-36342\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/2-10.png\" sizes=\"auto, (max-width: 336px) 100vw, 336px\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/2-10.png 336w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/2-10-300x120.png 300w\" alt=\"\" width=\"336\" height=\"134\" \/><span id=\"ezoic-pub-ad-placeholder-122\" class=\"ezoic-adpicker-ad\"><\/span><\/p>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Clearly, the polynomial representation of the equation is printed as the output so that the result is pretty easy to understand. We can perform various operations on this polynomial as well, like square it, find its derivative or even solve it for a value of x. Let\u2019s try doing all of these in the next example:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span><span class=\"st0\">&#8220;Polynomial Square:\u00a0<span class=\"es0\">\\n<\/span>&#8220;<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>first_polynomial * first_polynomial<span class=\"br0\">)<\/span><\/p>\n<p><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span><span class=\"st0\">&#8220;Derivative of Polynomial:\u00a0<span class=\"es0\">\\n<\/span>&#8220;<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>first_polynomial.<span class=\"me1\">deriv<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/p>\n<p><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span><span class=\"st0\">&#8220;Solving the Polynomial:\u00a0<span class=\"es0\">\\n<\/span>&#8220;<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>first_polynomial<span class=\"br0\">(<\/span><span class=\"nu0\">3<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/div>\n<div><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">When we run this example, we will see the following output:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-36343\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/3-8.png\" sizes=\"auto, (max-width: 563px) 100vw, 563px\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/3-8.png 563w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/3-8-300x215.png 300w\" alt=\"\" width=\"563\" height=\"404\" \/><\/p>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Just when I was thinking that this is all we could do with SciPy, I remembered that we can integrate a Polynomial as well. Let\u2019s run a final example with Polynomials:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span><span class=\"st0\">&#8220;Integrating the Polynomial:\u00a0<span class=\"es0\">\\n<\/span>&#8220;<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>first_polynomial.<span class=\"me1\">integ<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/div>\n<div><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">The integer we pass tells the package how many times to integrate the polynomial:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-36344\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/4-7.png\" sizes=\"auto, (max-width: 471px) 100vw, 471px\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/4-7.png 471w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/4-7-300x92.png 300w\" alt=\"\" width=\"471\" height=\"144\" \/><\/p>\n<p>We can simply pass another integer which tells the package how many times to integrate this polynomial.<\/p>\n<h3><strong>Solving Linear Equations<\/strong><\/h3>\n<p>It is even possible to solve linear equations with SciPy and find their roots, if they exist. To solve linear equations, we represent the set of equations as NumPy arrays and their solution as a separate NumPy arrays. Let\u2019s visualise it with an example where we do the same and make use of\u00a0<em>linalg<\/em>\u00a0package to find the roots of the equations, here are the equations we will be solving:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">1x + 5y\u00a0<span class=\"sy0\">=<\/span>\u00a0<span class=\"nu0\">6<\/span><br \/>\n3x + 7y\u00a0<span class=\"sy0\">=<\/span>\u00a0<span class=\"nu0\">9<\/span><\/div>\n<div><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Let\u2019s solve the above equations:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">from<\/span>\u00a0scipy\u00a0<span class=\"kw1\">import<\/span>\u00a0linalg<\/p>\n<p>equation\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"kw3\">array<\/span><span class=\"br0\">(<\/span><span class=\"br0\">[<\/span><span class=\"br0\">[<\/span><span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">5<\/span><span class=\"br0\">]<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"br0\">[<\/span><span class=\"nu0\">3<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">7<\/span><span class=\"br0\">]<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br \/>\nsolution\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"kw3\">array<\/span><span class=\"br0\">(<\/span><span class=\"br0\">[<\/span><span class=\"br0\">[<\/span><span class=\"nu0\">6<\/span><span class=\"br0\">]<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"br0\">[<\/span><span class=\"nu0\">9<\/span><span class=\"br0\">]<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><\/p>\n<p>roots\u00a0<span class=\"sy0\">=<\/span>\u00a0linalg.<span class=\"me1\">solve<\/span><span class=\"br0\">(<\/span>equation<span class=\"sy0\">,<\/span>\u00a0solution<span class=\"br0\">)<\/span><\/p>\n<p><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span><span class=\"st0\">&#8220;Found the roots:&#8221;<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>roots<span class=\"br0\">)<\/span><\/p>\n<p><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span><span class=\"st0\">&#8220;<span class=\"es0\">\\n<\/span>\u00a0Dot product should be zero if the solutions are correct:&#8221;<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>equation.<span class=\"me1\">dot<\/span><span class=\"br0\">(<\/span>roots<span class=\"br0\">)<\/span>\u00a0&#8211; solution<span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">When we run the above program, we will see that the dot product equation gives zero result, which means that the roots which the program found were correct:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-36345\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/5-8.png\" sizes=\"auto, (max-width: 703px) 100vw, 703px\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/5-8.png 703w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/5-8-300x157.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/5-8-375x195.png 375w\" alt=\"\" width=\"703\" height=\"367\" \/><\/p>\n<h3><strong>Fourier Transformations with SciPy<\/strong><\/h3>\n<p>Fourier Transformations helps us to express a function as separate components that make up that function and guides us about the way through which we can recombine those components to get the original function back.<\/p>\n<p>Let\u2019s look at a simple example of Fourier Transformations where we plot the sum of two cosines using the\u00a0<a href=\"https:\/\/linuxhint.com\/python_matplotlib_tutorial\/\">Matplotlib<\/a>\u00a0library:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">from<\/span>\u00a0scipy.<span class=\"me1\">fftpack<\/span>\u00a0<span class=\"kw1\">import<\/span>\u00a0fft<\/p>\n<p><span class=\"co1\"># Number of sample points<\/span><br \/>\nN\u00a0<span class=\"sy0\">=<\/span>\u00a0<span class=\"nu0\">500<\/span><\/p>\n<p><span class=\"co1\"># sample spacing<\/span><br \/>\nT\u00a0<span class=\"sy0\">=<\/span>\u00a0<span class=\"nu0\">1.0<\/span>\u00a0\/\u00a0<span class=\"nu0\">800.0<\/span><br \/>\nx\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"me1\">linspace<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">0.0<\/span><span class=\"sy0\">,<\/span>\u00a0N*T<span class=\"sy0\">,<\/span>\u00a0N<span class=\"br0\">)<\/span><br \/>\ny\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"me1\">cos<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">50.0<\/span>\u00a0*\u00a0<span class=\"nu0\">2.0<\/span>* np.<span class=\"me1\">pi<\/span>\u00a0* x<span class=\"br0\">)<\/span>\u00a0+\u00a0<span class=\"nu0\">0.5<\/span>\u00a0* np.<span class=\"me1\">cos<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">80.0<\/span>\u00a0*\u00a0<span class=\"nu0\">2.0<\/span>\u00a0* np.<span class=\"me1\">pi<\/span>\u00a0* x<span class=\"br0\">)<\/span><br \/>\nyf\u00a0<span class=\"sy0\">=<\/span>\u00a0fft<span class=\"br0\">(<\/span>y<span class=\"br0\">)<\/span><br \/>\nxf\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"me1\">linspace<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">0.0<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">1.0<\/span>\/<span class=\"br0\">(<\/span><span class=\"nu0\">2.0<\/span>\u00a0* T<span class=\"br0\">)<\/span><span class=\"sy0\">,<\/span>\u00a0N\/\/<span class=\"nu0\">2<\/span><span class=\"br0\">)<\/span><\/p>\n<p><span class=\"co1\"># matplotlib for plotting purposes<\/span><br \/>\n<span class=\"kw1\">import<\/span>\u00a0matplotlib.<span class=\"me1\">pyplot<\/span>\u00a0<span class=\"kw1\">as<\/span>\u00a0plt<br \/>\nplt.<span class=\"me1\">plot<\/span><span class=\"br0\">(<\/span>xf<span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">2.0<\/span>\/N * np.<span class=\"kw2\">abs<\/span><span class=\"br0\">(<\/span>yf<span class=\"br0\">[<\/span><span class=\"nu0\">0<\/span>:N\/\/<span class=\"nu0\">2<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/p>\n<p>plt.<span class=\"me1\">title<\/span><span class=\"br0\">(<\/span><span class=\"st0\">&#8216;Info&#8217;<\/span><span class=\"br0\">)<\/span><br \/>\nplt.<span class=\"me1\">ylabel<\/span><span class=\"br0\">(<\/span><span class=\"st0\">&#8216;Y axis&#8217;<\/span><span class=\"br0\">)<\/span><br \/>\nplt.<span class=\"me1\">xlabel<\/span><span class=\"br0\">(<\/span><span class=\"st0\">&#8216;X axis&#8217;<\/span><span class=\"br0\">)<\/span><\/p>\n<p>plt.<span class=\"me1\">grid<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><br \/>\nplt.<span class=\"me1\">show<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><\/div>\n<div><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Here, we started by constructing a sample space and cosine equation which we then transformed and plotted. Here is the output of the above program:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-36346\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/6-7.png\" sizes=\"auto, (max-width: 389px) 100vw, 389px\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/6-7.png 389w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/6-7-300x214.png 300w\" alt=\"\" width=\"389\" height=\"278\" \/><\/p>\n<p>This is one of the good example where we see SciPy being used in a complex mathematical equation to visualise things easily.<\/p>\n<h3><strong>Vectors and Matrix with SciPy<\/strong><\/h3>\n<p>Now that we know a lot of things which SciPy is capable of, we can be sure that SciPy can also work with Vectors and Matrix. The matrices are an important part of linear algebra as matrices is something we use to represent Vector mappings as well.<\/p>\n<p>Just like we looked at solving linear equations with SciPy, we can represent vectors with\u00a0<em>np.array()<\/em>\u00a0functions. Let\u2019s start by constructing a matrix:<span id=\"ezoic-pub-ad-placeholder-160\" class=\"ezoic-adpicker-ad\"><\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">my_matrix\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"me1\">matrix<\/span><span class=\"br0\">(<\/span>np.<span class=\"kw3\">random<\/span>.<span class=\"kw3\">random<\/span><span class=\"br0\">(<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">3<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">3<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>my_matrix<span class=\"br0\">)<\/span><\/div>\n<div><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Here is the output of the above snippet:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-36347\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/7-7.png\" sizes=\"auto, (max-width: 535px) 100vw, 535px\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/7-7.png 535w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/7-7-300x73.png 300w\" alt=\"\" width=\"535\" height=\"131\" \/><\/p>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Whenever we talk about matrices, we always talk about Eigenvalues and Eigenvectors. To put in simple words, Eigenvectors are the vectors which, when multiplied with a matrix, do not change their direction, as opposed to most of the vectors. This means that even when you multiply an Eigenvectors with a matrix, there exists a value (or eigenvalue) which is one of the factor of the multiplication. This means:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">Ax\u00a0<span class=\"sy0\">=<\/span>\u00a0\u03bbx.<\/div>\n<div><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">In above equation, A is the matrix, \u03bb is the Eigenvalue and x is the Vector. Let\u2019s write a simple code snippet to find the Eigenvalues for a given Vector:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">la<span class=\"sy0\">,<\/span>\u00a0vector\u00a0<span class=\"sy0\">=<\/span>\u00a0linalg.<span class=\"me1\">eig<\/span><span class=\"br0\">(<\/span>my_matrix<span class=\"br0\">)<\/span><\/p>\n<p><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>vector<span class=\"br0\">[<\/span>:<span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">0<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>vector<span class=\"br0\">[<\/span>:<span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">1<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><\/p>\n<p><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>linalg.<span class=\"me1\">eigvals<\/span><span class=\"br0\">(<\/span>my_matrix<span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/div>\n<div><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">When we run this example, we will see the following output:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-36348\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/8-7.png\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/8-7.png 580w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/8-7-300x99.png 300w\" alt=\"\" width=\"580\" height=\"191\" \/><\/p>\n<h3><strong>Calculating Matrix Determinant<\/strong><\/h3>\n<p>The next operation we will carry out with SciPy is to calculate the determinant of a 2-dimensional matrix. We will reuse the matrix we used in the last code snippet here:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">linalg.<span class=\"me1\">det<\/span><span class=\"br0\">(<\/span>\u00a0my_matrix\u00a0<span class=\"br0\">)<\/span><\/div>\n<div><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">When we run this example, we will see the following output:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-36349\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/9-7.png\" sizes=\"auto, (max-width: 462px) 100vw, 462px\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/9-7.png 462w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/02\/9-7-300x66.png 300w\" alt=\"\" width=\"462\" height=\"102\" \/><\/p>\n<h4><strong>Conclusion<\/strong><\/h4>\n<p>In this lesson, we looked at a lot of good examples where SciPy can help us by carrying out complex mathematical computations for us with an easy to use API and packages.<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/python_scipy\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We will see what is the use of SciPy library in Python and how it helps us to work with mathematical equations and algorithms in an interactive manner. The good thing about SciPy Python package is that if we want classes or construct web pages, SciPy is fully compatible with the system as a whole &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/02\/10\/python-scipy-tutorial-linux-hint\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python SciPy Tutorial \u2013 Linux Hint&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9524","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/9524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=9524"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/9524\/revisions"}],"predecessor-version":[{"id":9553,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/9524\/revisions\/9553"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=9524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=9524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=9524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}