{"id":23754,"date":"2023-11-27T18:14:33","date_gmt":"2023-11-27T12:44:33","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=23754"},"modified":"2023-11-27T18:14:35","modified_gmt":"2023-11-27T12:44:35","slug":"python-numpy-reciprocal-function","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/python-numpy-reciprocal-function\/","title":{"rendered":"numpy.reciprocal() in Python: Get Reciprocal of Array Elements"},"content":{"rendered":"\n<p>The<strong> numpy.reciprocal() <\/strong>function in Python is used to compute the reciprocal of each element in an array. The reciprocal of a number is simply <strong>1 divided by that number<\/strong>, which means let&#8217;s say we have a number &#8216;a&#8217; so the reciprocal of &#8216;a&#8217; will be &#8216;1\/a&#8217;. This function is part of the <strong>NumPy <\/strong>library, which is widely used for numerical and mathematical operations in Python.<\/p>\n\n\n\n<p>In this article, we will understand&nbsp;Python&nbsp;<strong>numpy.reciprocal()<\/strong>&nbsp;function, its syntax, and demonstrate it with various examples. Let\u2019s get started.<\/p>\n\n\n\n<p><strong><em>Also Read: <a href=\"https:\/\/codeforgeek.com\/numpy-min-in-python\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/numpy-min-in-python\/\">numpy.min() in Python &#8211; Get Minimum Value in Array<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of numpy.reciprocal() Function<\/h2>\n\n\n\n<p>Let us get started by understanding the fundamental constituents of the<strong><em>&nbsp;<\/em>numpy.reciprocal()<\/strong> function with the help of its syntax given below.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.reciprocal(array, \/, out=None, *, where=True, casting=&#039;same_kind&#039;, order=&#039;K&#039;, dtype=None, subok=True&#x5B;, signature, extobj])\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>array:<\/strong> The input array for which we want to compute the reciprocals.<\/li>\n\n\n\n<li><strong>out (optional):<\/strong>&nbsp;The output array in which the result is placed.<\/li>\n\n\n\n<li><strong>where (optional):&nbsp;<\/strong>A boolean array indicating where the operation should occur.<\/li>\n\n\n\n<li><strong>casting (optional):<\/strong>&nbsp;Controls what kind of data casting may occur.<\/li>\n\n\n\n<li><strong>order (optional):<\/strong>&nbsp;\u2018K\u2019, \u2018A\u2019, \u2018C\u2019, or \u2018F\u2019 to control the memory layout of the output.<\/li>\n\n\n\n<li><strong>dtype (optional):<\/strong>&nbsp;Desired data type for the output.<\/li>\n\n\n\n<li><strong>subok (optional):<\/strong>&nbsp;If True, then the input arrays are allowed to be subclasses.<\/li>\n<\/ul>\n\n\n\n<p><strong>Return:<\/strong><\/p>\n\n\n\n<p>It returns the array with the same shape as the input array which will contain the reciprocal of each element of the input array.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of numpy.reciprocal() Function<\/h2>\n\n\n\n<p>Let us now look at some examples to demonstrate the use of&nbsp;<strong>numpy.reciprocal()<\/strong>&nbsp;function in Python.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reciprocal of a Scalar Value<\/h3>\n\n\n\n<p>First, let&#8217;s start by simply calculating the reciprocal of a scalar value using<strong> numpy.reciprocal().<\/strong><\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nsc=21.1\nresult = np.reciprocal(sc)\nprint(result)\n<\/pre><\/div>\n\n\n<p>In the above code, we first imported <a href=\"https:\/\/codeforgeek.com\/python-top-libraries-for-machine-learning-and-data-science\/\">NumPy<\/a> library as <strong>np<\/strong> then we took a <strong>scaler value<\/strong> and named it as <strong>sc<\/strong>. After that, we passed that scaler value into the <strong>numpy.reciprocal()<\/strong> function and we got the reciprocal of our number as an outcome. So it&#8217;s that simple to compute the reciprocal of any value using numpy.reciprocal() function.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"672\" height=\"51\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1891.png\" alt=\"Reciprocal of a Scalar Value\" class=\"wp-image-23774\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1891.png 672w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1891-300x23.png 300w\" sizes=\"(max-width: 672px) 100vw, 672px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Reciprocal of a 1D Array<\/h3>\n\n\n\n<p>In this example, we will calculate the reciprocal of each element of a one-dimensional array using <strong>numpy.reciprocal()<\/strong>.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nar= np.array(&#x5B;1, 15, 33, 0.5,47])\nresult = np.reciprocal(ar)\nprint(result)\n<\/pre><\/div>\n\n\n<p>By using <strong>NumPy <\/strong>we created a <strong>1D array<\/strong> and stored it into a variable called <strong>ar<\/strong>. Then to get the reciprocal of each element from the array we passed our array into <strong>numpy.reciprocal()<\/strong> function and stored the outcome into a <strong>result <\/strong>variable. In last, we printed our <strong>result<\/strong> variable.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"962\" height=\"72\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1892.png\" alt=\"Reciprocal of a 1D Array\" class=\"wp-image-23775\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1892.png 962w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1892-300x22.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1892-768x57.png 768w\" sizes=\"(max-width: 962px) 100vw, 962px\" \/><\/figure>\n\n\n\n<p>We can clearly see that we got the reciprocal of each element very easily by just passing the array into the function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reciprocal of a 2D Array<\/h3>\n\n\n\n<p>This time the only difference will be that we will have a two-dimensional array and we will compute the reciprocal of each and every element of a 2D array like we did for the 1D array in the last example. <\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nar = np.array(&#x5B;&#x5B;14,3],&#x5B;2,3.2],&#x5B;8,1]])\nresult = np.reciprocal(ar)\nprint(result)\n<\/pre><\/div>\n\n\n<p>Here we created a <strong>2D array<\/strong> using a <strong>NumPy <\/strong>library and then passed it to the <strong>numpy.reciprocal() <\/strong>function to get the reciprocal of every element from the array.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"97\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1893-1024x97.png\" alt=\"Reciprocal of a 2D Array\" class=\"wp-image-23776\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1893-1024x97.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1893-300x28.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1893-768x73.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1893.png 1098w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Like the last example here we also got the reciprocal of every element from our 2D array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reciprocal of an Array Containing Zeros<\/h3>\n\n\n\n<p>As we know in mathematics if we reciprocate <strong>0 <\/strong>then we get infinite. So let&#8217;s see that when our array contains zeros and we compute the reciprocal of this array using numpy.reciprocal() then what will happen.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nar = np.array(&#x5B;11,60,2,0,4.5,0,2])\nresult = np.reciprocal(ar)\nprint(result)\n<\/pre><\/div>\n\n\n<p>In the above code, we created an <strong>array containing zeros <\/strong>and then we passed that array into the <strong>numpy.reciprocal()<\/strong> function. After we ran the above code, we got infinite as a reciprocal.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"955\" height=\"72\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1894.png\" alt=\"Reciprocal of an Array Containing Zeros\" class=\"wp-image-23777\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1894.png 955w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1894-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/Screenshot-1894-768x58.png 768w\" sizes=\"(max-width: 955px) 100vw, 955px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this tutorial, we have discussed<strong>&nbsp;numpy.reciprocal()<\/strong>&nbsp;function provided by <strong>Python\u2019s NumPy library<\/strong>, its syntax, and parameters, and also explored a few examples to get an understanding of it. After reading this tutorial, we hope you easily can do the reciprocal of each element of a NumPy array.<\/p>\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.reciprocal.html\" target=\"_blank\" rel=\"noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.reciprocal.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The numpy.reciprocal() function in Python is used to compute the reciprocal of each element in an array. The reciprocal of a number is simply 1 divided by that number, which means let&#8217;s say we have a number &#8216;a&#8217; so the reciprocal of &#8216;a&#8217; will be &#8216;1\/a&#8217;. This function is part of the NumPy library, which [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":23778,"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-23754","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.reciprocal-Function-in-Python-A-Detailed-Guide.png",1200,800,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy.reciprocal-Function-in-Python-A-Detailed-Guide-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy.reciprocal-Function-in-Python-A-Detailed-Guide-300x200.png",300,200,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy.reciprocal-Function-in-Python-A-Detailed-Guide-768x512.png",768,512,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy.reciprocal-Function-in-Python-A-Detailed-Guide-1024x683.png",1024,683,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy.reciprocal-Function-in-Python-A-Detailed-Guide.png",1200,800,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/10\/numpy.reciprocal-Function-in-Python-A-Detailed-Guide.png",1200,800,false]},"uagb_author_info":{"display_name":"Priyanshu Singh","author_link":"https:\/\/codeforgeek.com\/author\/priyanshu\/"},"uagb_comment_info":0,"uagb_excerpt":"The numpy.reciprocal() function in Python is used to compute the reciprocal of each element in an array. The reciprocal of a number is simply 1 divided by that number, which means let&#8217;s say we have a number &#8216;a&#8217; so the reciprocal of &#8216;a&#8217; will be &#8216;1\/a&#8217;. This function is part of the NumPy library, which&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23754","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\/95"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=23754"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23754\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/23778"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=23754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=23754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=23754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}