{"id":23991,"date":"2023-11-29T18:33:25","date_gmt":"2023-11-29T13:03:25","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=23991"},"modified":"2023-11-29T18:33:53","modified_gmt":"2023-11-29T13:03:53","slug":"numpy-float-power-python","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-float-power-python\/","title":{"rendered":"numpy.float_power() in Python: Get Element-wise Power of Array"},"content":{"rendered":"\n<p>The <strong>numpy.float_power()<\/strong> function in <a href=\"https:\/\/codeforgeek.com\/python\/\">Python<\/a> is part of the NumPy library that is used to compute the element-wise power of array elements. It allows us to raise each element of an array to a specified power, providing flexibility and efficiency for handling power operations on arrays.<\/p>\n\n\n\n<p>In this article, we will understand&nbsp;Python&nbsp;<strong>numpy.float_power()<\/strong>&nbsp;function, its syntax, and demonstrate it with various examples. Let\u2019s get started.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of numpy.float_power() Function<\/h2>\n\n\n\n<p>Let us get started by understanding the fundamental constituents of the <strong>numpy.float_power()<\/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.float_power(x1, x2, \/, out=None, *, where=True, casting=&#039;same_kind&#039;, order=&#039;K&#039;, dtype=None)\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>x1:<\/strong> This is the base array representing the base values.<\/li>\n\n\n\n<li><strong>x2:<\/strong> This is the exponent array representing the exponent values.<\/li>\n\n\n\n<li><strong>out (optional):<\/strong> This is an optional parameter that specifies the location where the result should be stored. If not provided, a new array will be created.<\/li>\n\n\n\n<li><strong>where (optional):<\/strong> This parameter is used for conditional execution. If provided, only the elements where the condition is True will be included in the result.<\/li>\n\n\n\n<li><strong>casting (optional):<\/strong> This parameter controls what kind of data casting may occur. The default is &#8216;same_kind&#8217;, which means that only casting between similar kinds (e.g., float to float) is allowed.<\/li>\n\n\n\n<li><strong>order (optional):<\/strong> Specifies the memory layout of the result. The default is &#8216;K&#8217;  which means the order is determined by the input array&#8217;s memory layout.<\/li>\n\n\n\n<li><strong>dtype (optional):<\/strong> This parameter allows us to set the data type of the output array. If not specified, it is determined by the data types of the input arrays.<\/li>\n\n\n\n<li><strong>subok (optional):<\/strong> If True, then sub-classes will be passed through. If False, the returned array will always be a base-class array.<\/li>\n<\/ul>\n\n\n\n<p><strong>Return:<\/strong><\/p>\n\n\n\n<p>An array with the same shape as the input arrays is returned by the<strong> numpy.float_power()<\/strong> method, where each element is calculated as <strong>x1^x2<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of numpy.float_power() Function<\/h2>\n\n\n\n<p>Let us now look at some examples to demonstrate the use of&nbsp;<strong>numpy.float_power()<\/strong>&nbsp;function in Python.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Get the Scalar Power Value<\/h3>\n\n\n\n<p>In this example, we will compute the element-wise power by passing scalar inputs into the <strong>numpy.float_power()<\/strong> function.<\/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\nsc1 = 2\nsc2 = 4\nresult = np.float_power(sc1, sc2)\nprint(result)\n<\/pre><\/div>\n\n\n<p>Here we imported the NumPy library as <strong>np <\/strong>then we took two scalar values: <strong>sc1<\/strong> and <strong>sc2<\/strong> and passed them into the <strong>numpy.float_power()<\/strong> function which will compute the power of <strong>sc1<\/strong> raised to the power of <strong>sc2<\/strong>, then we saved the output into the <strong>result <\/strong>variable. In last, we printed the<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=\"679\" height=\"51\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1901.png\" alt=\"Get the Scalar Power Value\" class=\"wp-image-24052\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1901.png 679w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1901-300x23.png 300w\" sizes=\"(max-width: 679px) 100vw, 679px\" \/><\/figure>\n\n\n\n<p>In the output, we can see that we got <strong>16.0<\/strong> as <strong>2^4 = 16<\/strong>. Since the <strong>numpy.float_power() <\/strong>function returns the result with a default data type of <strong>float <\/strong>that&#8217;s why we got <strong>16.0<\/strong>. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Get the Array Power Value<\/h3>\n\n\n\n<p>In this example, we will take<strong> both the inputs as a one-dimensional array<\/strong> and pass them to the <strong>numpy.float_power()<\/strong> function which will return us the array with the same shape by raising each element of the first array to the power of the corresponding element present in the second array.<\/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\nar1 = np.array(&#x5B;12, 1, 2])\nar2 = np.array(&#x5B;2, 3, 2])\nresult = np.float_power(ar1, ar2)\nprint(result)\n<\/pre><\/div>\n\n\n<p>Here by using the NumPy library, we created arrays of one-dimension and passed them into the <strong>numpy.float_power()<\/strong> function which will calculate the element-wise power, then we saved the output in the <strong>result <\/strong>variable. At last, we printed it.<\/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=\"575\" height=\"72\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1902.png\" alt=\"Get the Array Power Value\" class=\"wp-image-24053\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1902.png 575w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1902-300x38.png 300w\" sizes=\"(max-width: 575px) 100vw, 575px\" \/><\/figure>\n\n\n\n<p>In the output, we can see that we got an array where the first element is <strong>144.0<\/strong> because <strong>12 raises to the power of 2 is 144.0<\/strong>. In the same way, the second element in the output array is <strong>1^3=1.0 <\/strong>and the third element in the output array is <strong>4<\/strong> as <strong>2^2 = 4.0<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Get the Array Power Value with a Scalar Base<\/h3>\n\n\n\n<p>In this example, we will compute the <strong>scalar value raising to the power of each element from the one-dimensional array<\/strong> using <strong>numpy.float_power() <\/strong>function.<\/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 = 4\nar = np.array(&#x5B;1, 2, 3])\nresult = np.float_power(sc, ar)\nprint(result)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"792\" height=\"62\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1903.png\" alt=\"Get the Array Power Value with a Scalar Base\" class=\"wp-image-24054\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1903.png 792w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1903-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1903-768x60.png 768w\" sizes=\"(max-width: 792px) 100vw, 792px\" \/><\/figure>\n\n\n\n<p>In the output, we can see that the first element in the output array is <strong>4 <\/strong>because<strong> 4^1=4.0<\/strong>. In the same manner for the second element<strong> 4^2=16.0 <\/strong>and for the third element we got <strong>4^3 = 64.0<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Passing the &#8216;order&#8217; Parameter<\/h3>\n\n\n\n<p>In this example, we will <strong>change the memory layout<\/strong> of the resulting array. By default, it&#8217;s in<strong> &#8216;k&#8217; <\/strong>order, but we&#8217;ll switch it to <strong>&#8216;F&#8217;<\/strong> order, which is like a Fortran-style memory layout.<\/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=\"\">\nar1 = np.array(&#x5B;&#x5B;4, 5], &#x5B;2, 7]])\nar2 = np.array(&#x5B;&#x5B;4,2], &#x5B;3,1]])\nresult = np.float_power(ar1,ar2, order=&#039;F&#039;)\nprint(result)\n<\/pre><\/div>\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=\"84\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1904-1024x84.png\" alt=\"Passing the 'order' Parameter\" class=\"wp-image-24055\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1904-1024x84.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1904-300x25.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1904-768x63.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/Screenshot-1904.png 1060w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the output, we can see that <strong>numpy.float_power() <\/strong>function calculated the element-wise power of the base array <strong>ar1 <\/strong>with the exponent array <strong>ar2<\/strong> resulting in an array whose memory layout is of <strong>Fortran style<\/strong> as we passed the order as <strong>&#8216;F&#8217; <\/strong>in<strong> numpy.float_power() <\/strong>function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>Now that we have reached the end of this article, hope it has elaborated on the different ways to find the element-wise power of scalar value as well as the arrays by using the&nbsp;<strong>numpy.float_power()&nbsp;<\/strong>function from the&nbsp;<strong>NumPy<\/strong>&nbsp;library.&nbsp;For individuals who want to advance in Python, <a href=\"https:\/\/codeforgeek.com\/\">CodeforGeek <\/a>has a ton of other entertaining and equally educational articles that could be quite beneficial.<\/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.float_power.html\" target=\"_blank\" rel=\"noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.float_power.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The numpy.float_power() function in Python is part of the NumPy library that is used to compute the element-wise power of array elements. It allows us to raise each element of an array to a specified power, providing flexibility and efficiency for handling power operations on arrays. In this article, we will understand&nbsp;Python&nbsp;numpy.float_power()&nbsp;function, its syntax, and [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":24051,"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-23991","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\/11\/numpy.float_power-in-Python-A-Detailed-Guide.png",1200,800,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/numpy.float_power-in-Python-A-Detailed-Guide-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/numpy.float_power-in-Python-A-Detailed-Guide-300x200.png",300,200,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/numpy.float_power-in-Python-A-Detailed-Guide-768x512.png",768,512,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/numpy.float_power-in-Python-A-Detailed-Guide-1024x683.png",1024,683,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/numpy.float_power-in-Python-A-Detailed-Guide.png",1200,800,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/11\/numpy.float_power-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.float_power() function in Python is part of the NumPy library that is used to compute the element-wise power of array elements. It allows us to raise each element of an array to a specified power, providing flexibility and efficiency for handling power operations on arrays. In this article, we will understand&nbsp;Python&nbsp;numpy.float_power()&nbsp;function, its syntax, and&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23991","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=23991"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/23991\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/24051"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=23991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=23991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=23991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}