{"id":37496,"date":"2022-11-28T17:20:27","date_gmt":"2022-11-28T17:20:27","guid":{"rendered":"https:\/\/www.askpython.com\/?p=37496"},"modified":"2023-02-16T19:56:28","modified_gmt":"2023-02-16T19:56:28","slug":"numpy-exp2","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-exp2","title":{"rendered":"NumPy exp2 &#8211; A Complete Guide"},"content":{"rendered":"\n<p>Hey everyone, have you all calculated something like <strong>2 to the power 4<\/strong> or<strong> 2 to the power <\/strong>1 or something like that? For example, to calculate 2 to the power 4, we used to multiply 2 by itself 4 times. Well that was a tedious task, right? All those multiplication processes took a lot of time. Well, we can get the output within a fraction of a second without even going through the long process, sounds exciting \ud83d\ude42<\/p>\n\n\n\n<p>That&#8217;s where the <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-module\" data-type=\"post\" data-id=\"7694\"><strong>Python NumPy<\/strong><\/a> library comes into play. In this article, we will understand how can we use the NumPy exp2 function to calculate different power of 2. <\/p>\n\n\n\n<p>Without any further due, let&#8217;s get started.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-angle\" data-type=\"post\" data-id=\"37636\">NumPy angle \u2013 Returns the angle of a Complex argument<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is NumPy exp2?<\/h2>\n\n\n\n<p><strong>NumPy exp2<\/strong> is a <strong>mathematical function<\/strong> of the NumPy library that calculates 2<sup>x<\/sup>, where x is the input number passed to the function. Simple by the definition! Now, let us deep dive and see how to use this function in the Python program. Let us start by going through the syntax of the function.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-exp\" data-type=\"post\" data-id=\"37466\">NumPy exp \u2013 A Complete Guide<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of NumPy exp2<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.exp2(a)\n<\/pre><\/div>\n\n\n<p>Always pay focus to the syntax of any function of the NumPy library as it will make it easier for you to write the code. In the syntax, the input <strong><code>a<\/code><\/strong> can be a single number as well as a NumPy array of numbers. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Working with NumPy exp2<\/h2>\n\n\n\n<p>Now, let us write some code to understand it better.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy exp2 with Single Number<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nprint(&quot;2**3 is :&quot;,np.exp2(3))\n\nprint(&quot;2**7 is :&quot;,np.exp2(7))\n\nprint(&quot;2**10 is :&quot;,np.exp2(10))\n\nprint(&quot;2**(-2) is :&quot;,np.exp2(-2))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n2**3 is : 8.0\n2**7 is : 128.0\n2**10 is : 1024.0\n2**(-2) is : 0.25\n<\/pre><\/div>\n\n\n<p>In the above examples, we have passed a single number as input to the <strong><code>np.exp2()<\/code><\/strong> function. The output of the function is a <strong>floating point number<\/strong>.<\/p>\n\n\n\n<p>See how easy it is to calculate the power of 2 using the function \ud83d\ude42<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy exp2 with NumPy array<\/h3>\n\n\n\n<p>Let us now pass a NumPy array as input to the function.<\/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((-2 , 0 , 4 ))\n\nexp2_values = np.exp2(a)\n\nprint(&quot;Input Array :\\n&quot;,a)\nprint(&quot;Exp2 Values for each element of the Array :\\n&quot;,exp2_values)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nInput Array :\n &#x5B;-2  0  4]\nExp2 Values for each element of the Array :\n &#x5B; 0.25  1.   16.  ]\n<\/pre><\/div>\n\n\n<p><em>Note:<\/em> The function <strong><code>np.exp2()<\/code><\/strong> returns a NumPy array of the same dimensions as the Input array.<\/p>\n\n\n\n<p>In the above example, the NumPy array <strong>a<\/strong> is passed as an argument to the <strong><code>np.exp2()<\/code><\/strong> function. The exp2 values for each of the elements in the input array are calculated using the <code><strong>np.exp2()<\/strong><\/code> function. The output of the function is also a NumPy array which is stored in the variable <strong>exp2_values<\/strong>.<\/p>\n\n\n\n<p>In the next lines, we have used the print statements to print the input array and the output array respectively.<\/p>\n\n\n\n<p>By now, you have learned how to use the function with a single number and a NumPy array of numbers. Now, let us see how can we plot the function using the Python <a href=\"https:\/\/matplotlib.org\/\" data-type=\"URL\" data-id=\"https:\/\/matplotlib.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Matplotlib library<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Graph of the NumPy exp2<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nimport matplotlib.pyplot as plt\n\na = np.array((1 , 2 , 3 , 4 , 5))\n\nb = np.exp2(a)\n\nplt.plot(a , b , color = &quot;green&quot; , marker = &quot;o&quot;)\nplt.title(&quot;numpy.exp2()&quot;)\nplt.xlabel(&quot;X-axis&quot;)\nplt.ylabel(&quot;Y-axis&quot;)\nplt.show()\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/NumPy-exp2-Graph.png\" alt=\"NumPy Exp2 Graph\" class=\"wp-image-37597\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/NumPy-exp2-Graph.png 640w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/NumPy-exp2-Graph-300x225.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\"><strong>NumPy Exp2 Graph<\/strong><\/figcaption><\/figure>\n\n\n\n<p>In the above code, in the first two lines, we import the NumPy and Matplotlib libraries so that we can use their functionalities. <\/p>\n\n\n\n<p>Next, we created a variable <strong><code>a<\/code><\/strong> that stores the NumPy array of numbers which is passed as input to the np.exp2() function. Similarly, the variable <code><strong>b<\/strong><\/code> stores the output array of the <strong>np.exp2()<\/strong> function.<\/p>\n\n\n\n<p>From the next lines, we have used the functions of the Matplotlib library to plot the graph of the function. Let us understand each line and its purpose.<\/p>\n\n\n\n<p><code><strong>plt.plot()<\/strong><\/code>\u00a0the function is used <a href=\"https:\/\/www.askpython.com\/python\/examples\/plot-mathematical-functions\" data-type=\"post\" data-id=\"21691\">to plot<\/a> the <strong><code>np.exp2()<\/code><\/strong> function which takes four arguments.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The\u00a0<strong>first<\/strong>\u00a0argument is the\u00a0<strong><a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" data-type=\"post\" data-id=\"1070\">NumPy Array<\/a> of numbers<\/strong>, plotted on the X-axis(Horizontal Axis).<\/li>\n\n\n\n<li>The&nbsp;<strong>second<\/strong>&nbsp;argument is the output of the&nbsp;<code><strong>np.exp2()<\/strong><\/code>&nbsp;function, plotted on the Y-axis(Vertical Axis).<\/li>\n\n\n\n<li>The&nbsp;<strong>third<\/strong>&nbsp;argument is the color of the plot.<\/li>\n\n\n\n<li>The&nbsp;<strong>fourth<\/strong>&nbsp;argument is the marker value which emphasizes each point with a specified marker. There are different types of markers that can be used to denote the points on the curve.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>plt.title()<\/code><\/strong> sets the value of the <strong>title<\/strong> for the plot. Here, the title is numpy.exp2().<\/li>\n\n\n\n<li><strong><code>plt.xlabel()<\/code><\/strong> and <strong><code>plt.ylabel()<\/code><\/strong> sets the name of the Horizontal and Vertical axes respectively.<\/li>\n\n\n\n<li><strong><code>plt.show()<\/code><\/strong> is used to display the plot.<\/li>\n<\/ul>\n\n\n\n<p>That&#8217;s all, we are done with the examples as well as the graph of the NumPy exp2 function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this article, we learned how to use the NumPy exp2 function to calculate the power of 2 values. We used the function with single numbers as well as a NumPy array of numbers. We also plotted the graph of the NumPy exp2 function. <\/p>\n\n\n\n<p>Do check out the links given under the Reference section. Keep learning and keep exploring more topics <a href=\"https:\/\/www.askpython.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/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.exp2.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy Documentation &#8211; NumPy exp2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey everyone, have you all calculated something like 2 to the power 4 or 2 to the power 1 or something like that? For example, to calculate 2 to the power 4, we used to multiply 2 by itself 4 times. Well that was a tedious task, right? All those multiplication processes took a lot [&hellip;]<\/p>\n","protected":false},"author":48,"featured_media":37632,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-37496","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\/37496","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\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=37496"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37496\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/37632"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=37496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=37496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=37496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}