# NumPy exp – A Complete Guide

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 of the input array. This means that it calculates e^x for each x in the input array. Here, e is the Euler’s constant and has a value of approximately 2.718281.

It can be said that np.exp(i) is approximately equal to e**i, where ‘**’ is the power operator. We will see the examples for this function in the upcoming section of this tutorial.

## Syntax of NumPy exp method

numpy.exp(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Returns: An array containing the element-wise exponential of x. If x is a scalar, then the result is also a scalar.

## Using the Numpy exp() method

Let’s check out how to use the numpy exp method through different examples.

### 1. Exponential of a scalar value using numpy exp()

import numpy as np

# positive scalar
a = 6
ans = np.exp(a)
print("a =", a)
print("Exponential =", ans)

Output:

a = 6
Exponential = 403.4287934927351

The answer is calculated as e^6 i.e. (2.718281)^6 = 403.4287934927351.

import numpy as np

# negative scalar
a = -6
ans = np.exp(a)
print("a =", a)
print("Exponential of the array =", ans)

Output:

a = -6
Exponential of the array = 0.0024787521766663585

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.

### 2. Exponential of a 1-dimensional array using numpy exp()

import numpy as np

a = [0, 3, -2, 1]
ans = np.exp(a)
print("a =", a)
print("Exponential of the array =", ans)

Output:

a = [0, 3, -2, 1]
Exponential of the array = [ 1.         20.08553692  0.13533528  2.71828183]

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.

### 3. Exponential of a 2-dimensional array using numpy exp()

import numpy as np

a = [[2, -4, 1],
[0, 1, 5]]
ans = np.exp(a)
print("a =\n", a)
print("Exponential of the array =\n", ans)

Output:

a =
[[2, -4, 1], [0, 1, 5]]
Exponential of the array =
[[7.38905610e+00 1.83156389e-02 2.71828183e+00]
[1.00000000e+00 2.71828183e+00 1.48413159e+02]]

Similar to the above example, the resulting array contains an exponential of e for each value in the input array in order.

### 4. Plotting the graph of np.exp() using numpy exp()

Let us now plot the graph of the np.exp() function against some input values using the Matplotlib library in Python.

import numpy as np
import matplotlib.pyplot as plt

# input
x = np.linspace(0, 5, 100)
# output
y = np.exp(x)

# changing the size of figure to 8x8
plt.figure(figsize=(8, 8))
display(plt.plot(x, y))
plt.grid()
# title of the graph
plt.title("Graph of e^x")
plt.xlabel("x")
plt.ylabel("e^x")

Output:

In this example, we have created an evenly spaced array of numbers (x) from 0 to 5 having 100 values in total. Then this array is passed to the np.exp() function and stored in the result in y. At last, we plot the graph of y v/s x and get the above plot as the result.

## Summary

That’s all! In this tutorial, we learned about the Numpy exp method and practiced different types of examples using the same. If you want to learn more about NumPy, feel free to go through our NumPy tutorials.