Definition of NumPy exponential
NumPy library contains various function exponential is one of them. This functions are used to perform calculations on the array or n-dimensional array. The exponential function is used to calculate the logarithm and exponential value of array elements. In python, NumPy exponential provides various function to calculate log and exp value. Functions are listed as :loglp, log1, log2, log3 for log. Expml, exp2, exp to calculate an exponential value.
Syntax:
To use this exponential function to need to import numPy library. After importing the package we can use the different functions to calculate the exponential values.
import numPy as myNum
myarr = myNum.array([100, 400, 500, 700, 800])
myNum.exp(myarr)
In the above syntax, we are using the exp() function to calculate the exponential value of the array elements.
e.g.
myNum.exp(your_array)
In short, we can pass our array inside the exponential function to calculate the values.
How does exponential Function Work in NumPy?
As now we know that we use NumPy exponential function to get the exponential value of every element of the array. This array can be of any type single, two, three or multidimensional array. The exponential function is takes two parameters. Some other parameters are also there where and out but we will discuss more about the basic parameter it takes.
numpy_name.exp(param)
Here this function is taking one parameter named as x. This parameter is like the input it takes to calculate the value. We just need to pass our array inside the function. This input parameter can accept anything like an array also it can accept the single values as well there is no restriction, so however we can also able to calculate the value of a single element if required. To pass any single value the syntax will be look like this see below;
1. With single value; we will see one practice example to understand it better;
Syntax:
numpy_name.exp(your_value here ..)
Example:
import numPy as myNum
myNum.exp(100)
So in this case we are just passing the single element as the parameter here so this exp() function will calculate its exponential value.
2. With array as the parameter inside the function; We will also going to see one practice example for better understating;
Syntax:
numpy_name.exp(your_array here ..)
Example:
import numPy as myNum
myarr = myNum.array([100, 400, 500, 700, 800])
myNum.exp(myarr)
Inthe above lines of code we are creating one array named as myarr which is going to hold some elements inside it. For creating an array we are using array() function provided by the numPy library in python. Followed by the exp() function here inside this we are passing our newlycreated array as the parameter and this function will give us the exponential value of this array.
3. With the multi-dimensional array inside the exp() function; we will see syntax for it to understand it better;
Syntax:
numpy_name.exp(your 2d array here ..)
Now here we have to create one 2d array to work with it. In order to create an 2d array we have one function called as ‘arrang’ provided by the numPy library in python.
Let’s see one basic structure to create 2d array using numPy function see below;
Example:
import numPy as myNum
myArr = myNum.arange(6)
myNum.exp(myArr)
In the above example we are using arrange function to work with 2d array in python but in order to use it we have to import numPy in our program. This function will create one 2d array for us followed by the exp() function. we just need to pass the 2d array inside the function to get the exponential values of the array elements.
Examples of NumPy exponential
Following are the examples are given below:
Example #1
In this example, we are creating a single dimension array and using the exp() function to get the exp values of elements.
Code:
import numpy as myNum
#creating array using numpy
myarr = myNum.array([4, 9, 2, 5])
#using exp() function to get the value
resultarr = myNum.exp(myarr)
#printing the result
print(resultarr)
Output:
Example #2
In this example we are creating a three dimensional array and calculating its value using exp() function from NumPy.
Code:
import numpy as myNum
#creating array using numpy
myarr1 = myNum.array([4, 9, 2, 5])
myarr2 = myNum.random.randint(0, 5, size = (2, 3, 8))
#using exp() function to get the value
resultarr1 = myNum.exp(myarr1)
resultarr2 = myNum.exp(myarr2)
#printing the result
print(resultarr1)
print(resultarr2)
Output:
Example #3
In this example we are creating 2d array but now we are using exp2() function. To get the exp value of the elements.
Code:
import numpy as myNum
#creating array using numpy
myarr1 = myNum.array([4, 9, 2, 5])
myarr2 = myNum.random.randint(0, 5, size = (2, 3, 8))
#printing the array value here ..
print(myarr1)
print(myarr2)
#using exp() function to get the value
resultarr1 = myNum.exp2(myarr1)
resultarr2 = myNum.exp2(myarr2)
#printing the result
print(resultarr1)
print(resultarr2)
Output:
Example #4
In this example we are creating multi dimension array but using expm1() function from exponential function library in python.
Code:
import numpy as myNum
#creating array using numpy
myarr1 = myNum.array([4, 9, 2, 5])
myarr2 = myNum.random.randint(0, 5, size = (2, 3, 8))
#printing the array value here ..
print(myarr1)
print(myarr2)
#using expm1() function to get the value
resultarr1 = myNum.expm1(myarr1)
resultarr2 = myNum.expm1(myarr2)
#printing the result
print(resultarr1)
print(resultarr2)
Output:
Example #5
In this example we are calculating the exp value of the decimal elements by using exp() function.
Code:
import numpy as myNum
#creating array using numpy
myarr1 = myNum.array([4, 92.4, 1.0, 3.5, 8.4])
myarr2 = myNum.random.randint(0, 5, size = (2, 3, 8))
#printing the array value here ..
print(myarr1)
print(myarr2)
#using expm1() function to get the value
resultarr1 = myNum.expm1(myarr1)
resultarr2 = myNum.expm1(myarr2)
#printing the result
print(resultarr1)
print(resultarr2)
Output:
Conclusion
NumPy library provides various functions that can be used for computation on the array. The exponential function is one of the utility we can say to get the exp value of the element. By the use of this, we can get exp value of single element as well not only array specific. So we can use these elements inside an array or a single element.
Recommend ed Articles
This is a guide to NumPy exponential. Here we also discuss the introduction and how does exponential function work in numpy along with different examples and its code implementation. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses