{"id":36995,"date":"2022-11-19T10:54:56","date_gmt":"2022-11-19T10:54:56","guid":{"rendered":"https:\/\/www.askpython.com\/?p=36995"},"modified":"2022-11-19T10:54:57","modified_gmt":"2022-11-19T10:54:57","slug":"numpy-prod","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-prod","title":{"rendered":"NumPy Prod &#8211; A Complete Guide"},"content":{"rendered":"\n<p>Hello and welcome to this tutorial on Numpy prod. In this tutorial, we will be learning about the NumPy prod() method and also seeing a lot of examples regarding the same. So let us begin!<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-zeros\" data-type=\"post\" data-id=\"36933\">NumPy Zeros \u2013 A Complete Guide<\/a><\/em><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is NumPy prod?<\/h2>\n\n\n\n<p>The prod method in NumPy is a function that returns the product of the array elements. It can be the product of all the array elements, the product of the array elements along the rows, or the product of the array elements along the columns.\u00a0<br>We will see the examples for each of these in the upcoming section of this tutorial.<\/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 NumPy prod<\/h2>\n\n\n\n<p>Let us first have a look at the syntax of the NumPy prod function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.prod(a, axis=None, dtype=None, out=None, keepdims=&lt;no value&gt;, initial=&lt;no value&gt;, where=&lt;no value&gt;)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Parameter<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Required\/Optional<\/strong><\/td><\/tr><tr><td>a (array_like)<\/td><td>Input data.<\/td><td>Required<\/td><\/tr><tr><td>axis<\/td><td>Axis along which the product of the array is to be calculated. It can be axis=0 i.e. along columns or axis=1 i.e. along rows or axis=None which implies that the product of the entire array is to be returned. If axis is a tuple of ints, a product is performed on all of the axes specified in the tuple instead of a single axis or all the axes as before.<\/td><td>Optional<\/td><\/tr><tr><td>dtype (data type)<\/td><td>The data type of the array to be returned.<\/td><td>Optional<\/td><\/tr><tr><td>out<\/td><td>An alternative output array in which to place the result. It must have the same shape as the expected output.<\/td><td>Optional<\/td><\/tr><tr><td>keepdims (bool)<\/td><td>If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.<\/td><td>Optional<\/td><\/tr><tr><td>initial<\/td><td>Starting value for the product.<\/td><td>Optional<\/td><\/tr><tr><td>where<\/td><td>Elements to include in the product.<\/td><td>Optional<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Returns:<\/strong><br>An array with the same shape as&nbsp;<em>a<\/em>&nbsp;which contains the product of the elements of <em>a<\/em> along the axis given and the specified axis removed. If the axis=None, a scalar is returned which is the product of the whole array.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of numpy.prod() <\/h2>\n\n\n\n<p>Let&#8217;s get right into the different examples of using numpy.prod() function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Product of the entire array<\/h3>\n\n\n\n<p><strong>1-dimensional array<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n \na = &#x5B;5, 3, 1]\n \nproduct = np.prod(a)\nprint(&quot;a =&quot;, a)\nprint(&quot;Product of the array =&quot;, product)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;5, 3, 1]\nProduct of the array = 15\n<\/pre><\/div>\n\n\n<p>Product of the array = 5*3*1 = 15<\/p>\n\n\n\n<p><strong>2-dimensional array<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n \na = &#x5B;&#x5B;5, 3, 1], &#x5B;1, 2, 4]]\n \nproduct = np.prod(a)\nprint(&quot;a =&quot;, a)\nprint(&quot;Product of the array =&quot;, product)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;&#x5B;5, 3, 1], &#x5B;1, 2, 4]]\nProduct of the array = 120\n<\/pre><\/div>\n\n\n<p>Product of the array = 5*3*1*1*2*4 = 120<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Product along the axis<\/h3>\n\n\n\n<p><strong>Column-wise product<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;&#x5B;5, 3, 1], \n     &#x5B;1, 2, 4]]\n\n# product along axis=0 i.e. columns\nproduct = np.sum(a, axis=0)\nprint(&quot;a =&quot;, a)\nprint(&quot;Product of the array along the columns =&quot;, product)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;&#x5B;5, 3, 1], &#x5B;1, 2, 4]]\nProduct of the array along the columns = &#x5B;5 6 4]\n<\/pre><\/div>\n\n\n<p>Column 0 product = 5*1 = 5<br>Column 1 product= 3*2 = 6<br>Column 2 product = 1*4 = 4<\/p>\n\n\n\n<p><strong>Row-wise product<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;&#x5B;5, 3, 1], \n     &#x5B;1, 2, 4]]\n\n# product along axis=1 i.e. rows\nproduct = np.prod(a, axis=1)\nprint(&quot;a =&quot;, a)\nprint(&quot;Product of the array along the rows =&quot;, product)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;&#x5B;5, 3, 1], &#x5B;1, 2, 4]]\nProduct of the array along the rows = &#x5B;15  8]\n<\/pre><\/div>\n\n\n<p>Row 0 product = 5*3*1 = 15<br>Row 1 product = 1*2*4 = 8<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Return the product of the array as float data type<\/h3>\n\n\n\n<p>This is the same as the above examples except that here the returned values are of float data type.<\/p>\n\n\n\n<p><strong>Product of the entire array<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;&#x5B;2, 3 ,6], \n     &#x5B;1, 5, 4]]\n\nproduct = np.prod(a, dtype=float)\nprint(&quot;a =&quot;, a)\nprint(&quot;Product of the array along the columns =&quot;, product)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;&#x5B;2, 3, 6], &#x5B;1, 5, 4]]\nProduct of the array along the columns = 720.0\n<\/pre><\/div>\n\n\n<p><strong>Column-wise product<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;&#x5B;2, 3 ,6], \n     &#x5B;1, 5, 4]]\n\n# product along axis=0 i.e. columns\nproduct = np.prod(a, axis=0, dtype=float)\nprint(&quot;a =&quot;, a)\nprint(&quot;Product of the array along the columns =&quot;, product)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;&#x5B;2, 3, 6], &#x5B;1, 5, 4]]\nProduct of the array along the columns = &#x5B; 2. 15. 24.]\n<\/pre><\/div>\n\n\n<p><strong>Row-wise product<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;&#x5B;2, 3 ,6], \n     &#x5B;1, 5, 4]]\n\n# product along axis=1 i.e. rows\nproduct = np.prod(a, axis=1, dtype=float)\nprint(&quot;a =&quot;, a)\nprint(&quot;Product of the array along the rows =&quot;, product)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;&#x5B;2, 3, 6], &#x5B;1, 5, 4]]\nProduct of the array along the rows = &#x5B;36. 20.]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Product of only specific elements in the array<\/h3>\n\n\n\n<p><strong>1-dimensional array<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;2, 9, 3, 4, 1]\n\nproduct = np.prod(a, where=&#x5B;True, False, False, True, True])\nprint(&quot;a =&quot;, a)\nprint(&quot;Product of the array =&quot;, product)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;2, 9, 3, 4, 1]\nProduct of the array = 8\n<\/pre><\/div>\n\n\n<p>In the above code, the <strong>&#8216;where&#8217;<\/strong> clause specifies which elements to include in the product. &#8216;True&#8217; implies that include this value in the product and &#8216;False&#8217; implies that do not include this value in the product calculation.<br>Here, where=[True, False, False, True, True] denotes that only include the elements at positions 0, 3 and 4 of the array in the product. Hence, product =  a[0]*a[3]*a[4] = 2*4*1 = 8.<\/p>\n\n\n\n<p><strong>2-dimensional array<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;&#x5B;2, 9], \n    &#x5B;7, 10]]\n\nproduct = np.prod(a, where=&#x5B;&#x5B;True, False], &#x5B;False, True]])\nprint(&quot;a =&quot;, a)\nprint(&quot;Product of the array =&quot;, product)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;&#x5B;2, 9], &#x5B;7, 10]]\nProduct of the array = 20\n<\/pre><\/div>\n\n\n<p>Here, from the 0th row, only a[0][0] i.e. 2 and from the 1st row, only a[1][1] i.e. 10 is included in the product. Hence, product = 2*10 = 20.<\/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>That\u2019s all! In this tutorial, we learned about the\u00a0<strong>Numpy prod<\/strong>\u00a0method and practiced different types of examples using the same.<\/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<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.prod.html#numpy.prod\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy prod Official Documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hello and welcome to this tutorial on Numpy prod. In this tutorial, we will be learning about the NumPy prod() method and also seeing a lot of examples regarding the same. So let us begin! Also read: NumPy Zeros \u2013 A Complete Guide What is NumPy prod? The prod method in NumPy is a function [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":36996,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-36995","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/36995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=36995"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/36995\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/36996"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=36995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=36995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=36995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}