{"id":37466,"date":"2022-11-28T07:22:52","date_gmt":"2022-11-28T07:22:52","guid":{"rendered":"https:\/\/www.askpython.com\/?p=37466"},"modified":"2023-02-16T19:56:31","modified_gmt":"2023-02-16T19:56:31","slug":"numpy-exp","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-exp","title":{"rendered":"NumPy exp &#8211; A Complete Guide"},"content":{"rendered":"\n<p>Hello and welcome to this tutorial on Numpy exp. In this tutorial, we will be learning about the NumPy exp() method and also seeing a lot of examples regarding the same. So let us begin!<\/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 exp?<\/h2>\n\n\n\n<p>The <code>exp<\/code> method in NumPy is a function that returns the exponential of all the elements of the input array. This means that it calculates <em>e^x <\/em>for each <em>x <\/em>in the input array. Here, <em><strong>e<\/strong><\/em> is the Euler&#8217;s constant and has a value of approximately 2.718281.  <\/p>\n\n\n\n<p>It can be said that <strong>np.exp(i)<\/strong> is approximately equal to <strong>e**i<\/strong>, where &#8216;**&#8217; is the power operator. 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 exp method<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.exp(x, \/, 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>Required\/Optional<\/td><\/tr><tr><td>x<\/td><td>Input array\/value.<\/td><td>Required<\/td><\/tr><tr><td>out<\/td><td>An alternative output array in which to place the result. By default, a new array is created.<\/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><tr><td>**kwargs<\/td><td>For other keyword-only arguments<\/td><td>Optional<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Returns:<\/strong> An array containing the element-wise exponential of <em>x<\/em>. If <em>x<\/em> is a 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\">Using the Numpy exp() method <\/h2>\n\n\n\n<p>Let&#8217;s check out how to use the numpy exp method through different examples. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Exponential of a scalar value using numpy exp()<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# positive scalar\na = 6\nans = np.exp(a)\nprint(&quot;a =&quot;, a)\nprint(&quot;Exponential =&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 = 6\nExponential = 403.4287934927351\n<\/pre><\/div>\n\n\n<p>The answer is calculated as e^6 i.e. (2.718281)^6 = 403.4287934927351.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# negative scalar\na = -6\nans = np.exp(a)\nprint(&quot;a =&quot;, a)\nprint(&quot;Exponential of the array =&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 = -6\nExponential of the array = 0.0024787521766663585\n<\/pre><\/div>\n\n\n<p>In this case, since a is a negative number, the exponential of a is (e)^(-6) i.e. 1\/(e)^6 = 1\/(2.718281)^6 = 0.0024787521766663585.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Exponential of a 1-dimensional array using numpy exp()<\/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, 3, -2, 1]\nans = np.exp(a)\nprint(&quot;a =&quot;, a)\nprint(&quot;Exponential of the array =&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, 3, -2, 1]\nExponential of the array = &#x5B; 1.         20.08553692  0.13533528  2.71828183]\n<\/pre><\/div>\n\n\n<p>Here, the result array contains the exponential of e for each value in the input array. That is, the and contains the values, e^0, e^3, e^-2 and e^1 in order of the input values.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Exponential of a 2-dimensional array using numpy exp()<\/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;2, -4, 1], \n     &#x5B;0, 1, 5]]\nans = np.exp(a)\nprint(&quot;a =\\n&quot;, a)\nprint(&quot;Exponential of the array =\\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 =\n &#x5B;&#x5B;2, -4, 1], &#x5B;0, 1, 5]]\nExponential of the array =\n &#x5B;&#x5B;7.38905610e+00 1.83156389e-02 2.71828183e+00]\n &#x5B;1.00000000e+00 2.71828183e+00 1.48413159e+02]]\n<\/pre><\/div>\n\n\n<p>Similar to the above example, the resulting array contains an exponential of e for each value in the input array in order.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. Plotting the graph of np.exp() using numpy exp()<\/h3>\n\n\n\n<p>Let us now plot the graph of the <code>np.exp()<\/code> function against some input values using the <a href=\"https:\/\/www.askpython.com\/python-modules\/matplotlib\" target=\"_blank\" rel=\"noreferrer noopener\">Matplotlib <\/a>library in Python.<\/p>\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\n# input\nx = np.linspace(0, 5, 100)\n# output\ny = np.exp(x)\n\n\n# changing the size of figure to 8x8\nplt.figure(figsize=(8, 8))\ndisplay(plt.plot(x, y))\nplt.grid()\n# title of the graph\nplt.title(&quot;Graph of e^x&quot;)\nplt.xlabel(&quot;x&quot;)\nplt.ylabel(&quot;e^x&quot;)\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=\"663\" height=\"631\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/graph-of-exp.png\" alt=\"Graph Of Exponential\" class=\"wp-image-37471\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/graph-of-exp.png 663w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/graph-of-exp-300x286.png 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><figcaption class=\"wp-element-caption\">Graph Of Exponential<\/figcaption><\/figure>\n\n\n\n<p>In this example, we have created an evenly spaced array of numbers (<em>x<\/em>) from 0 to 5 having 100 values in total. Then this array is passed to the <code>np.exp()<\/code> function and stored in the result in y. At last, we plot the graph of <strong>y v\/s x<\/strong> and get the above plot as the result.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>That\u2019s all! In this tutorial, we learned about the&nbsp;<strong>Numpy exp<\/strong>&nbsp;method and practiced different types of examples using the same. If you want to learn more about NumPy, feel free to go through our&nbsp;<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.exp.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy exp Official Documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hello and welcome to this tutorial on Numpy exp. In this tutorial, we will be learning about the NumPy exp() method and also seeing a lot of examples regarding the same. So let us begin! What is NumPy exp? The exp method in NumPy is a function that returns the exponential of all the elements [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":37468,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-37466","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\/37466","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=37466"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37466\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/37468"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=37466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=37466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=37466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}