{"id":38155,"date":"2022-12-16T18:29:33","date_gmt":"2022-12-16T18:29:33","guid":{"rendered":"https:\/\/www.askpython.com\/?p=38155"},"modified":"2023-02-16T19:56:27","modified_gmt":"2023-02-16T19:56:27","slug":"numpy-float_power","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-float_power","title":{"rendered":"NumPy float_power"},"content":{"rendered":"\n<p>Hello and welcome to this tutorial on&nbsp;<strong>Numpy float_power<\/strong>. In this tutorial, we will be learning about the&nbsp;<strong>NumPy float_power()&nbsp;<\/strong>method and also seeing a lot of examples regarding the same. So let us begin!<br><br><strong><em>Also Read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-power\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy Power \u2013 Raising another number to the power of<\/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 float_power?<\/h2>\n\n\n\n<p>The\u00a0<code>float_power()<\/code>\u00a0method in NumPy is a function that returns an array calculated by raising the elements in one array to the power corresponding to the value in the second array. <\/p>\n\n\n\n<p>If <em>x1<\/em> and <em>x2<\/em> are two arrays, then <code>float_power(x1, x2)<\/code> computes the output element-wise, i.e. by raising each value in <em>x1<\/em> to the value in <em>x2<\/em> at the corresponding position. As the name of the function itself suggests, its default return type is float. We will see the examples for this function 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 float_power<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.float_power(x1, x2, \/, 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<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>x1 (array_like)<\/td><td>Base array.<\/td><td>Required<\/td><\/tr><tr><td>x2 (array_like)<\/td><td>Power\/Exponent array.<\/td><td>Required<\/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>where<\/td><td>Takes an array-like object. At locations where it is True, the&nbsp;<code>out<\/code>&nbsp;array will be set to the&nbsp;<code>ufunc<\/code>&nbsp;result. Elsewhere, the&nbsp;<code>out<\/code>&nbsp;array will retain its original value.<\/td><td>Optional<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Returns:<\/strong>&nbsp;<br>An array containing the result of&nbsp;<em>x<\/em>1 raised to <em>x2<\/em>, element-wise. If&nbsp;<em>x1<\/em> and <em>x2&nbsp;<\/em>are scalar, then the result is also a scalar.<\/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 float_power()<\/h2>\n\n\n\n<p>Lets now get right into the examples and understand how the&nbsp;<code>float_power<\/code>&nbsp;method actually works.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">When both inputs are scalars<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = 5\nb = 3\n# using the float_power method \nans = np.float_power(a, b)\n\nprint(&quot;a = &quot;, a, &quot;\\nb =&quot;, b)\nprint(&quot;Result = &quot;, ans)\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 =  5 \nb = 3\nResult =  125.0\n<\/pre><\/div>\n\n\n<p>A simple example where the result is calculated as<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n5^3 = 125\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">When one input is a scalar and the other a 1-dimensional array<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;0, -2, 4, -6, 8]\nb = 3\n# using the float_power method \nans = np.float_power(a, b)\n\nprint(&quot;a = &quot;, a, &quot;\\nb =&quot;, b)\nprint(&quot;Result = &quot;, ans)\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;0, -2, 4, -6, 8] \nb = 3\nResult =  &#x5B;   0.   -8.   64. -216.  512.]\n<\/pre><\/div>\n\n\n<p>Here, each element in the array <em>a<\/em> is raised to the power <em>b<\/em> and the output is calculated as<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nans&#x5B;0] = a&#x5B;0] ^ b = 0 ^ 3 = 0\nans&#x5B;1] = a&#x5B;1] ^ b = -2 ^ 3 = -8\nans&#x5B;2] = a&#x5B;2] ^ b = 4 ^ 3 = 64\nans&#x5B;3] = a&#x5B;3] ^ b = -6 ^ 3 = -216\nans&#x5B;4] = a&#x5B;4] ^ b = 8 ^ 3 = 512\n<\/pre><\/div>\n\n\n<p>From the output, we can see that the function also handles negative values.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">When both the input arrays are 1-dimensional<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;3, 1, 4, 2.5]\nb = &#x5B;0, 2, 2.7, 4]\n# using the float_power method \nans = np.float_power(a, b)\n\nprint(&quot;a = &quot;, a, &quot;\\nb =&quot;, b)\nprint(&quot;Result = &quot;, ans)\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;3, 1, 4, 2.5] \nb = &#x5B;0, 2, 2.7, 4]\nResult =  &#x5B; 1.          1.         42.22425314 39.0625    ]\n<\/pre><\/div>\n\n\n<p>Here, each element in&nbsp;<em>a<\/em>&nbsp;is raised to the power of the corresponding element in&nbsp;<em>b<\/em>&nbsp;and the output is calculated as:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nans&#x5B;0] = a&#x5B;0] ^ b&#x5B;0] = 3 ^ 0 = 1\nans&#x5B;1] = a&#x5B;1] ^ b&#x5B;1] = 1 ^ 2 = 1\nans&#x5B;2] = a&#x5B;2] ^ b&#x5B;2] = 4 ^ 2.7 = 42.22425314\nans&#x5B;3] = a&#x5B;3] ^ b&#x5B;3] = 2.5 ^ 4 = 39.0625\n<\/pre><\/div>\n\n\n<p>Observe that even floating point numbers are handled by the <code>float_power()<\/code> method.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">When both the input arrays are 2-dimensional<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;&#x5B;1, 2], &#x5B;6, 3]]\nb = &#x5B;&#x5B;2, 5], &#x5B;2, 3]]\n# using the float_power method \nans = np.float_power(a, b)\n\nprint(&quot;a = &quot;, a, &quot;\\nb =&quot;, b)\nprint(&quot;Result = \\n&quot;, ans)\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;1, 2], &#x5B;6, 3]] \nb = &#x5B;&#x5B;2, 5], &#x5B;2, 3]]\nResult = \n &#x5B;&#x5B; 1. 32.]\n &#x5B;36. 27.]]\n<\/pre><\/div>\n\n\n<p>Similar to the above example,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nans&#x5B;0]&#x5B;0] = a&#x5B;0]&#x5B;0] ^ b&#x5B;0]&#x5B;0] = 1 ^ 2 = 1\nans&#x5B;0]&#x5B;1] = a&#x5B;0]&#x5B;1] ^ b&#x5B;0]&#x5B;1] = 2 ^ 5 = 32\n \nans&#x5B;1]&#x5B;0] = a&#x5B;1]&#x5B;0] ^ b&#x5B;1]&#x5B;0] = 6 ^ 2 = 36\nans&#x5B;1]&#x5B;1] = a&#x5B;1]&#x5B;1] ^ b&#x5B;1]&#x5B;1] = 3 ^ 3 = 27\n<\/pre><\/div>\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 float_power\u00a0<\/strong>method and practiced different types of examples using the same. \u00a0<\/p>\n\n\n\n<p>If you want to learn more about NumPy, feel free to go through our\u00a0<a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy tutorials<\/a>.<\/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.float_power.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy float_power Official Documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hello and welcome to this tutorial on&nbsp;Numpy float_power. In this tutorial, we will be learning about the&nbsp;NumPy float_power()&nbsp;method and also seeing a lot of examples regarding the same. So let us begin! Also Read: NumPy Power \u2013 Raising another number to the power of What is NumPy float_power? The\u00a0float_power()\u00a0method in NumPy is a function that [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":38200,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-38155","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\/38155","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=38155"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38155\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/38200"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=38155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=38155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=38155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}