Updated March 28, 2023
Introduction to NumPy Log
In this article, we will discuss the NumPy log() function in Python. In Python, NumPy is a module that is used for working with array and NumPy log() function is provided by the NumPy module that is used for working with logarithmic operations, which means log() function is a mathematical function that will help us to compute natural logarithms in Python programming language. In general, the natural logarithm is inverse of the exponential function exp() of the input elements of the given array, which can be defined in the formula format as log(exp(x)) = x, where x is an item of the input array.
Working of logarithm function log() of NumPy in Python
In this article, we will discuss in detail the NumPy.log() function, which is another mathematical operation that can be used on the given array to calculate the logarithms of the given items in the input array in base e. In Python, this log() function can also be used to find the logarithm not only in base e but also in base 2, 10 also and we should note that if the input array contains the values of real number data type, then this log() function returns the output which is also in real number data type and if each value is not expressed in real value then the function also yields nan as it will also not understand which data type and hence leading to floating-point error flag. Similarly, if the input array items are complex numbers, then the function also returns the complex analytical values. Therefore, we can use this log() function of NumPy with a single number, a list of numbers or items, with 1 and a multidimensional array.
Now let us see in the below section the log() function syntax and examples on this function to demonstrate it to understand it better.
Syntax:
numpy.log( input_array, out )
Parameters:
- Input_array: This parameter is used to specify the array that needs to be passed as an argument to the log() function.
- Out: This is an optional parameter used to store the result array, which is an output array returned after computing the log() function on the given array.
Examples of NumPy Log
Now let us see various examples on demonstrating the log() function in the below section. But before starting with examples, we should note that we should first import the NumPy module before executing this log() function.
Example #1
Now let us see an example of using the log() function to calculate logarithm value in base e.
Code:
import numpy as np
print("Program to demonstrate log() function in base e.")
print("\n")
input_arr = [4, 6, 2, 9]
print ("The given input array is as follows: ")
print(input_arr)
print("\n")
output_arr = np.log(input_arr)
print ("The result obtained in the array form is as follows:")
print(output_arr)
Output:
In the above program, we can see that we have declared an input array that contains 4 elements, and we have printed the array as shown in the screenshot of the output. Then we are passing this given array to the log function by specifying the array name as an argument to the function, and we have not mentioned any base so that it will take by default as base e, and the result will return an array again with log values of the items in the given array as we can see in the above screenshot.
Now we will see another example where we will print both log values of the items in the given array to the base 2 and 10. In this example, we will also see how to find the log value of the item having an exponential function which is of the form e^x or e ** x, and when we are finding the log value of this exponential function which will be of the form ln(e^x) = x.
Example #2
Code:
import numpy as nl
print("Program to demonstrate log() function with base 2 and 10:")
print("\n")
input_arr = [5, 8, 3, 7]
print ("The given input array is as follows: ")
print(input_arr)
print("\n")
output_arr1 = nl.log2(input_arr)
print ("The result obtained in the array form for base 2 is as follows:")
print(output_arr1)
print("\n")
output_arr2 = nl.log10(input_arr)
print ("The result obtained in the array form for base 10 is as follows:")
print(output_arr2)
print("\n")
exp_arr = [ 2**4, 5**2]
print("The given array having power raised valued items is as follows:")
print(exp_arr)
print("\n")
out_exparr = nl.log(exp_arr)
print("The result of the log values of exponential items is as follows:")
print(out_exparr)
Output:
In the above program, we can see we are first declaring an array with 4 elements, and we are finding the log values of these specified items with base 2 and base 10. The result is as shown in the above screenshot. In the above program, we can also see we have declared an array having item values such as power raised values like (2**4 = 2 ^4 = 16) as shown in the above screenshot of the output we can see when we print the array of this exponential value it will print the values obtained after multiplying the values and hence it will find the value of these values and then it will find the log value of the product value obtained as in the array in the above screenshot.
This log() function can also be used to find the value of a single value also; as we saw in the above examples, we worked on the list or array given directly. We can also create an array using the NumPy module and then find the created array’s log values. Now let us demonstrate these both concepts in below example:
Example #3
Code:
import numpy as nl
print("Program to demonstrate log() function for single value:")
print("\n")
print("The eulers constant value in Python numpy module")
print(nl.e)
print("\n")
out1 = nl.log(nl.e)
print ("The log value result obtained for the constant value is as follows:")
print(out1)
print("\n")
input_arr = nl.arange(start = 5, stop = 8)
print("The created array is as follows:")
print(input_arr)
print("\n")
out_arr = nl.log(input_arr)
print ("The log value result obtained for the created array is as follows:")
print(out_arr)
Output:
In the above program, we are finding the Euler constant and its log value. We also created an array using the NumPy module’s arrange() function, and then we are finding the log values of this created array.
Conclusion
This article concludes that we have defined the numpy.log() function, which is used for finding the logarithm values of the given item, array, or list. In this article, we saw examples of finding log values with base e, 2, and 10. In this article, we also saw examples of how to find the log value of a single value, array, and any constant values with examples.
Recommended Articles
This is a guide to NumPy Log. Here we discuss the Working of the logarithm function log() of NumPy in Python along with the Examples. You may also have a look at the following articles to learn more –