{"id":23683,"date":"2023-10-26T16:04:02","date_gmt":"2023-10-26T10:34:02","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=23683"},"modified":"2023-10-26T16:04:07","modified_gmt":"2023-10-26T10:34:07","slug":"numpy-einsum-path-function","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-einsum-path-function\/","title":{"rendered":"How to Use numpy.einsum_path() Function?"},"content":{"rendered":"\n<p>The <em>numpy<\/em> library within Python contains a wide range of functions to modify or manipulate numerical data. Amongst those is a category of functions under <strong><em>einsum<\/em> <\/strong>which are noteworthy for their manipulative capabilities in handling the N-dimensional arrays. They can seamlessly support adding, multiplying or rearranging any given input arrays in very short time periods thereby catapulting the computational capabilities.<\/p>\n\n\n\n<p>However, the name of this function might be a bit deceiving since it has nothing to do with neither the scientist, Einstein nor the mathematical operation of summing.<\/p>\n\n\n\n<p>This article shall focus on a particular variant of <em>einsum \u2013 <\/em>the <em><strong>einsum_path( ) <\/strong><\/em>function which is used for creating intermediary arrays for evaluation of the lowest cost contraction order for the given <em>einsum <\/em>expression. This function is not limited to contracting the given input arrays into those with similar dimensions. Rather, it goes the extra mile to perform the contraction for a different dimension of the resulting array.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of <em>einsum_path( )<\/em> Function<\/h2>\n\n\n\n<p>Given below is the syntax of the <em>einsum_path( ) <\/em>function detailed with its basic components that are required for its effective functioning.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.einsum_path(subscripts, *operands, optimize = \u2018greedy\u2019)\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>subscripts \u2013 <\/em><\/strong>used to specify the input subscripts<\/li>\n\n\n\n<li><strong><em>*operands \u2013<\/em><\/strong> used to list the input arrays<\/li>\n\n\n\n<li><strong><em>optimize \u2013 <\/em><\/strong>set to \u2018greedy\u2019 by default and is used to specify how to deduce the desired path type. One can also choose to set this with the alternate option, \u2018optimal\u2019.<\/li>\n<\/ul>\n\n\n\n<p>The difference between the two available options for the <em>optimize <\/em>is that the <strong>\u2018greedy\u2019 <\/strong>setting shall analyse each step before selecting the best-suited contraction pair. Whereas, the<strong> \u2018optimal\u2019<\/strong> algorithm on the other hand analyses all the existing combinations in order to identify every possible path for contracting the inputs and then select the order which best fits the least cost approach. The result of the <em>einsum_path( ) <\/em>function is always in the form of a list.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using <em>einsum_path( ) <\/em>Function in Python<\/h2>\n\n\n\n<p>We shall get things started by importing the <em>numpy <\/em>library using the following code before deploying the <em>einsum_path( ) <\/em>function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n<\/pre><\/div>\n\n\n<p>Once done, it is time to construct the input arrays that are to be subjected to the contraction as shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar1 = np.array(&#x5B;&#x5B;10, -9], &#x5B;5, -7]])\nar2 = np.array(&#x5B;&#x5B;0, 4, 6], &#x5B;7, 5, -3]])\nar3 = np.array(&#x5B;&#x5B;-1, 4], &#x5B;6, 8], &#x5B;-2, 4]])\n<\/pre><\/div>\n\n\n<p>Following the inputs declaration, one shall now summon the <em>einsum_path( ) <\/em>function using the code below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npt = np.einsum_path(&#039;ij, jk, ki -&gt; ii&#039;, ar1, ar2, ar3, optimize = &#039;greedy&#039;)\n<\/pre><\/div>\n\n\n<p>The logic behind the execution of the above code is to contract the inputs <strong>ar2<\/strong> and <strong>ar3 <\/strong>initially and then use the corresponding results for deducing the final contraction.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(pt&#x5B;0])\nprint(pt&#x5B;1])\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"678\" height=\"256\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Results-of-einsum_path-set-with-greedy-algorithm.jpg\" alt=\"Results of einsum_path( ) set with greedy algorithm\" class=\"wp-image-23689\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Results-of-einsum_path-set-with-greedy-algorithm.jpg 678w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Results-of-einsum_path-set-with-greedy-algorithm-300x113.jpg 300w\" sizes=\"(max-width: 678px) 100vw, 678px\" \/><figcaption class=\"wp-element-caption\">Results of <em>einsum_path( ) <\/em>set with greedy algorithm<\/figcaption><\/figure>\n\n\n\n<p><span style=\"font-size:11.0pt;line-height:107%\">The above code demonstrates the working of the<strong> \u2018greedy\u2019 <\/strong>setting. So, let us now have a look at what <strong>\u2018optimal\u2019<\/strong> setting has got to offer.<\/span><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npt = np.einsum_path(&#039;ij, jk, ki -&gt; ii&#039;, ar1, ar2, ar3, optimize = &#039;optimal&#039;)\nprint(pt&#x5B;0])\nprint(pt&#x5B;1])\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"680\" height=\"277\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Results-of-einsum_path-set-with-optimal-algorithm.jpg\" alt=\"Results of einsum_path( ) set with optimal algorithm\" class=\"wp-image-23691\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Results-of-einsum_path-set-with-optimal-algorithm.jpg 680w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Results-of-einsum_path-set-with-optimal-algorithm-300x122.jpg 300w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><figcaption class=\"wp-element-caption\">Results of <em>einsum_path( ) <\/em>set with optimal algorithm<\/figcaption><\/figure>\n\n\n\n<p>It could be observed that the results returned by both settings are similar. This could be explained by the simplicity of the inputs that we have used for the demonstration purpose. Nevertheless, one has to bear in mind that when the inputs fed become complex, then the computation time with the <strong>\u2018optimal\u2019<\/strong> algorithm would always be greater than that incurred by the<strong> \u2018greedy\u2019 <\/strong>algorithm. So, know your requirements and choose wisely.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Now that we have reached the end of this article, hope it has elaborated on the procedure to put into use, the <em>einsum_path( ) <\/em>function in Python. Here\u2019s another article that elaborates on calculating the outer product of vectors <a href=\"https:\/\/codeforgeek.com\/numpy-outer-function\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/numpy-outer-function\/\" target=\"_blank\" rel=\"noreferrer noopener\">using the <em>outer( ) <\/em>function<\/a> from the <em>numpy<\/em> library in Python. There are numerous other enjoyable and equally informative articles in <em><a href=\"https:\/\/codeforgeek.com\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">CodeforGeek<\/a><\/em> that might be of great help to those who are looking to level up in Python.\u00a0<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.einsum_path.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.einsum_path.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The numpy library within Python contains a wide range of functions to modify or manipulate numerical data. Amongst those is a category of functions under einsum which are noteworthy for their manipulative capabilities in handling the N-dimensional arrays. They can seamlessly support adding, multiplying or rearranging any given input arrays in very short time periods [&hellip;]<\/p>\n","protected":false},"author":90,"featured_media":23752,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[134],"tags":[],"class_list":["post-23683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/How-to-Use-numpy.einsum_path-Function.png",1000,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/How-to-Use-numpy.einsum_path-Function-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/How-to-Use-numpy.einsum_path-Function-300x180.png",300,180,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/How-to-Use-numpy.einsum_path-Function-768x461.png",768,461,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/How-to-Use-numpy.einsum_path-Function.png",1000,600,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/How-to-Use-numpy.einsum_path-Function.png",1000,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/How-to-Use-numpy.einsum_path-Function.png",1000,600,false]},"uagb_author_info":{"display_name":"Arulius Savio","author_link":"https:\/\/codeforgeek.com\/author\/arulius\/"},"uagb_comment_info":0,"uagb_excerpt":"The numpy library within Python contains a wide range of functions to modify or manipulate numerical data. Amongst those is a category of functions under einsum which are noteworthy for their manipulative capabilities in handling the N-dimensional arrays. They can seamlessly support adding, multiplying or rearranging any given input arrays in very short time periods&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/90"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=23683"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23683\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/23752"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=23683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=23683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=23683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}