{"id":23396,"date":"2023-10-25T10:26:33","date_gmt":"2023-10-25T04:56:33","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=23396"},"modified":"2023-10-25T10:26:34","modified_gmt":"2023-10-25T04:56:34","slug":"numpy-positive-in-python","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-positive-in-python\/","title":{"rendered":"Introduction to numpy.positive() in Python"},"content":{"rendered":"\n<p>It is time for us to delve into the vast library of Python packed with numerical functions \u2013 the <em>numpy <\/em>library and this time, we will explore a rather strange function. It is very strange that at first sight, one might mistake it to be an abomination within Python.<\/p>\n\n\n\n<p>The function of interest would be the <em><strong>numpy.positive( ) <\/strong><\/em>function which returns the element-wise numerical positives for any given input array. <\/p>\n\n\n\n<p>That might seem a bit no-brainer, but there is a silver lining! This function has nothing to do with the conversion of negative numbers in the input array into their positive equivalents. So, what exactly, is it destined to do? Let us have a closer look to find it out through each of the following sections:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Syntax of <em>positive( )<\/em> Function<\/strong><\/li>\n\n\n\n<li><strong>Use Cases of the<em> numpy.<\/em><\/strong><strong><em>positive( )<\/em><\/strong><\/li>\n\n\n\n<li><em><strong>numpy.<\/strong><\/em><strong><em>positive( ) <\/em>on N-Dimensional Arrays<\/strong><\/li>\n<\/ul>\n\n\n\n<p>As always, let us get things started by importing the <em>numpy <\/em>library using the following code:<\/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<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of <em>positive( )<\/em> Function<\/h2>\n\n\n\n<p>Given below is the syntax of the <em>positive( ) <\/em>function containing all the basic constituents 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.positive(x, out=None, *, where=True, dtype=None)\n<\/pre><\/div>\n\n\n<p>where,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>x \u2013 <\/em><\/strong>Input scalar or N-dimensional array whose numerical positives are to be returned<\/li>\n\n\n\n<li><strong><em>out \u2013<\/em><\/strong> an optional construct set to <em>none <\/em>by default and could be used to store the results in the desired array which is similar in size to that of the output<\/li>\n\n\n\n<li><strong><em>* <\/em>\u2013 <\/strong>kwargs or keyword argument is an optional construct that is used to pass keyword variable length of argument to a function<\/li>\n\n\n\n<li><strong><em>where \u2013 <\/em><\/strong>an optional construct which is used to apply the function only at the specified positions when set to <em>True <\/em>(default setting) or skip the positions when set to <em>False<\/em><\/li>\n\n\n\n<li><strong><em>dtype \u2013 <\/em><\/strong>an optional construct used to specify the data type that is to be returned<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Use Cases of the<em> numpy.positive( )<\/em><\/h2>\n\n\n\n<p>In this section, we shall construct an array &amp; try running it through the <em>positive( )<\/em> function to have a look at the outcome.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar1 = np.array(&#x5B;&#x5B;-8, 4, 2, -9],\n                &#x5B;-5, 0, 2, -3]])\nr1 = np.positive (ar1)\nprint (r1)\n<\/pre><\/div>\n\n\n<p>Given below are the results for the above code:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"331\" height=\"132\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Numerical-Positives-for-ar1.jpg\" alt=\"Numerical Positives for ar1\" class=\"wp-image-23400\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Numerical-Positives-for-ar1.jpg 331w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Numerical-Positives-for-ar1-300x120.jpg 300w\" sizes=\"(max-width: 331px) 100vw, 331px\" \/><figcaption class=\"wp-element-caption\">Numerical Positives for ar1<\/figcaption><\/figure>\n\n\n\n<p>One might think that nothing has happened &amp; the function has just returned whatever has been fed as its input. Let\u2019s have a look from a different perspective to really get to know what has happened.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nad1 = print (id(ar1))\nad2 = print (id(r1))\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"263\" height=\"88\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Comparing-Address-for-Input-Result.jpg\" alt=\"Comparing Addresses of Input &amp; Result\" class=\"wp-image-23402\"\/><figcaption class=\"wp-element-caption\">Comparing Addresses of Input &amp; Result<\/figcaption><\/figure>\n\n\n\n<p>Now that the results are stored in a different location, one can argue further that this might as well be done by using a <em>copy( )<\/em> function. But the <em>positive( )<\/em> function offers flexibility to bend the data type which does not happen in the case of the <em>copy( ) <\/em>function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nr2 = np.positive (ar1, dtype = float)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"352\" height=\"132\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Converting-Data-Type-of-the-Result.jpg\" alt=\"Converting Data Type of the Result\" class=\"wp-image-23404\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Converting-Data-Type-of-the-Result.jpg 352w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Converting-Data-Type-of-the-Result-300x113.jpg 300w\" sizes=\"(max-width: 352px) 100vw, 352px\" \/><figcaption class=\"wp-element-caption\">Converting Data Type of the Result<\/figcaption><\/figure>\n\n\n\n<p>This function works well with every data type except for <em>Boolean<\/em> which might return the following error.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nUFuncNoLoopError: ufunc &#039;positive&#039; did not contain a loop with signature matching types\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><em>numpy.positive( ) <\/em>on N-Dimensional Arrays<\/h2>\n\n\n\n<p>Let us now apply the <em>positive( ) <\/em>function to an N-dimensional array, but this time we shall store its result in another array that is to be created to be of the same size.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar1 = np.array(&#x5B;&#x5B;-8, 4, 2, -9],\n                &#x5B;-5, 0, 2, -3]])\nr2 = np.zeros((2,4))\nprint(r2)\nnp.positive (ar1, out=r2, dtype=float)\nprint(r2)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"415\" height=\"212\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Results-for-ar1-2.jpg\" alt=\"Results for ar1\" class=\"wp-image-23406\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Results-for-ar1-2.jpg 415w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Results-for-ar1-2-300x153.jpg 300w\" sizes=\"(max-width: 415px) 100vw, 415px\" \/><figcaption class=\"wp-element-caption\">Results for ar1<\/figcaption><\/figure>\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 how to use the&nbsp;<em>positive( )&nbsp;<\/em>function from the&nbsp;<em>numpy&nbsp;<\/em>library with some use cases. Here is another article that details and emphasizes the importance of <a href=\"https:\/\/codeforgeek.com\/indentation-in-python\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/indentation-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">using indentations in Python<\/a> programming. There are numerous other enjoyable and equally informative articles in <a href=\"https:\/\/codeforgeek.com\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>CodeforGeek<\/em> <\/a>that might be of great help to those who are looking to level up in Python.&nbsp;<\/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.positive.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.positive.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is time for us to delve into the vast library of Python packed with numerical functions \u2013 the numpy library and this time, we will explore a rather strange function. It is very strange that at first sight, one might mistake it to be an abomination within Python. The function of interest would be [&hellip;]<\/p>\n","protected":false},"author":90,"featured_media":23565,"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-23396","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\/numpy.positive-in-Python.png",1000,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy.positive-in-Python-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy.positive-in-Python-300x180.png",300,180,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy.positive-in-Python-768x461.png",768,461,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy.positive-in-Python.png",1000,600,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy.positive-in-Python.png",1000,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy.positive-in-Python.png",1000,600,false]},"uagb_author_info":{"display_name":"Arulius Savio","author_link":"https:\/\/codeforgeek.com\/author\/arulius\/"},"uagb_comment_info":0,"uagb_excerpt":"It is time for us to delve into the vast library of Python packed with numerical functions \u2013 the numpy library and this time, we will explore a rather strange function. It is very strange that at first sight, one might mistake it to be an abomination within Python. The function of interest would be&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23396","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=23396"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23396\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/23565"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=23396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=23396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=23396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}