{"id":37047,"date":"2022-11-19T11:04:02","date_gmt":"2022-11-19T11:04:02","guid":{"rendered":"https:\/\/www.askpython.com\/?p=37047"},"modified":"2022-11-19T11:04:03","modified_gmt":"2022-11-19T11:04:03","slug":"numpy-nanprod","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-nanprod","title":{"rendered":"NumPy nanprod &#8211; A Complete Guide"},"content":{"rendered":"\n<p>Hello and welcome to this tutorial on Numpy nanprod. In this tutorial, we will be learning about the NumPy nanprod() method and also seeing a lot of examples regarding the same. So let us begin!<\/p>\n\n\n\n<p><em><strong>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-trunc\" data-type=\"post\" data-id=\"37017\">Numpy trunc() \u2013 Return the truncated value of the input, element-wise<\/a><\/strong><\/em><\/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 nanprod?<\/h2>\n\n\n\n<p>In Python, NaN denotes Not a Number. If we have an array that contains some NaN values and want to find its product, we can use the <code>nanprod()<\/code> method from NumPy.<\/p>\n\n\n\n<p>The <code>nanprod()<\/code> method in NumPy is a function that returns the product of the array elements calculated by treating the NaN values in the array as equal to 1. 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<\/p>\n\n\n\n<p>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 nanprod<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.nanprod(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 array whose product is desired.<\/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.<\/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&nbsp;<em>a<\/em>&nbsp;by treating NaN values as 1, 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.nanprod()<\/h2>\n\n\n\n<p>The <code>numpy.nanprod()<\/code> function is used to compute the product of array elements over a given axis, ignoring NaNs. Let&#8217;s take a look at the usage of <code>numpy.nanprod()<\/code> along with some examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Product of the whole array using numpy.nanprod()<\/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 = np.array(&#x5B;6, np.nan, 7])\nproduct = np.nanprod(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; 6. nan  7.]\nProduct of the array = 42.0\n<\/pre><\/div>\n\n\n<p>In the above code, the array contains one NaN value. While calculating the product, the <code>nanprod()<\/code> function treats that NaN value as 1 and calculates the product as 6*1*7 = 42.<\/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 = np.array(&#x5B;&#x5B;6, np.nan, 7], &#x5B;np.nan, np.nan, 3]])\nproduct = np.nanprod(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; 6. nan  7.]\n &#x5B;nan nan  3.]]\nProduct of the array = 126.0\n<\/pre><\/div>\n\n\n<p>Treating all the NaN values as 1, product  = 6*1*7*1*1*3 = 126.<\/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 = np.array(&#x5B;&#x5B;np.nan, np.nan, 4],\n              &#x5B;5, np.nan, 10]])\n\n# product along axis=0 i.e. columns\nproduct = np.nanprod(a, axis=0)\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;nan nan  4.]\n &#x5B; 5. nan 10.]]\nProduct of the array = &#x5B; 5.  1. 40.]\n<\/pre><\/div>\n\n\n<p>Treating NaN values as 1, <br>Column 0 product = 1*5 = 5<br>Column 1 product= 1*1 = 1<br>Column 2 product = 4*10 = 40<\/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 = np.array(&#x5B;&#x5B;np.nan, np.nan, 4],\n              &#x5B;5, np.nan, 10]])\n\n# product along axis=1 i.e. rows\nproduct = np.nanprod(a, axis=1)\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;nan nan  4.]\n &#x5B; 5. nan 10.]]\nProduct of the array = &#x5B; 4. 50.]\n<\/pre><\/div>\n\n\n<p>Treating NaN values as 1, <br>Row 0 product = 1*1*4 = 4<br>Row 1 product = 5*1*10 = 50<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Product of an empty array and an all NaN array<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# empty arrays\na = &#x5B;]\nb = &#x5B;&#x5B;]]\n\nproduct_a = np.nanprod(a)\nprint(&quot;a =&quot;, a)\nprint(&quot;Product of the 1-d empty array =&quot;, product_a)\n\nproduct_b = np.nanprod(b)\nprint(&quot;b =&quot;, b)\nprint(&quot;Product of the 2-d empty array =&quot;, product_b)\n\n# all NaN array\nc = &#x5B;np.nan, np.nan, np.nan]\nproduct_c = np.nanprod(c)\nprint(&quot;c =&quot;, c)\nprint(&quot;Product of the all NaN array =&quot;, product_c)\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;]\nProduct of the 1-d empty array = 1.0\nb = &#x5B;&#x5B;]]\nProduct of the 2-d empty array = 1.0\nc = &#x5B;nan, nan, nan]\nProduct of the all NaN array = 1.0\n<\/pre><\/div>\n\n\n<p>All the empty arrays and the arrays containing only NaN values return 1 when the <code>nanprod()<\/code> method is applied to them.<\/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\u00a0Numpy nanprod\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.nanprod.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy nanprod Official Documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hello and welcome to this tutorial on Numpy nanprod. In this tutorial, we will be learning about the NumPy nanprod() method and also seeing a lot of examples regarding the same. So let us begin! Also read: Numpy trunc() \u2013 Return the truncated value of the input, element-wise What is NumPy nanprod? In Python, NaN [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":37054,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-37047","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\/37047","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=37047"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37047\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/37054"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=37047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=37047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=37047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}