Coding Ref

How to use numpy.exp() in Python

How to use numpy.exp() in Python

In Python, numpy.exp() is a function that is used to calculate the exponent of each element in a NumPy array.

This function is often used in mathematical and scientific calculations, as it allows you to easily compute the exponent of many values at once.

Syntax

Here is the syntax for numpy.exp():

numpy.exp(x, out=None)

This function takes the following arguments:

  • x: The input array. This is the array whose elements will have their exponent calculated.
  • out (optional): The output array. If provided, the exponent of the elements of x will be stored in this array. Otherwise, a new array will be created to hold the exponentiated values.

Examples

Here is an example of how you might use numpy.exp() to calculate the exponent of the elements in a NumPy array.

Suppose you have the following array:

main.py
import numpy as np

a = np.array([0, 1, 2, 3, 4, 5])

You can use numpy.exp() to calculate the exponent of each element in this array, like this:

main.py
a_exp = np.exp(a)

This will result in the following array:

output
array([ 1.00000000e+00, 2.71828183e+00, 7.38905610e+00,
2.00855369e+01, 5.45981500e+01, 1.48413159e+02])

You can also use numpy.exp() to calculate the exponent of the elements in place, without creating a new array.

To do this, you can provide the out argument to numpy.exp(), like this:

main.pyimport

a = np.array([0., 1., 2., 3., 4., 5.])

np.exp(a, out=a)

This will modify the original a array in place, so that it contains the exponentiated values of the original elements.

You'll also like

Related tutorials curated for you

    ModuleNotFoundError: No module named 'numpy' in Python

    How to use numpy.zeros() in Python

    How to use numpy.round() in Python

    How to use numpy.percentile() in Python

    How to use numpy.exp() in Python

    How to use numpy.linspace() in Python

    How to use numpy.ndarray.flatten() in Python

    How to use numpy.ravel() in Python

    What does numpy.arrange() do?

    How to use numpy.vstack() in Python

    NumPy: np.arrange()

    NumPy numpy.transpose()