# Natural log python numpy – Python NumPy log() Function

NumPy log() Function:

Natural log python numpy: The log() function of the NumPy module finds the natural logarithm(base e) of a specified value.

The inverse of exp() is the natural logarithm log, therefore log(exp(x)) = x.

Hence, the natural logarithm means log with base e.

Syntax:

numpy.log(a, out=None)

Parameters

a: This is required. It is an array or an object given as input.

out: This is optional. It is the location where the result will be stored. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.

Return Value:

np.log: The natural logarithm of each element of a is returned by the log() function of the NumPy module.

## NumPy log() Function in Python

Example1

Approach:

• Import numpy module using the import keyword
• Pass some random list as an argument to the array() function of the numpy module to create an array.
• Store it in a variable.
• Print the above-given array.
• Pass the above-given array as an argument to the log() function of the numpy module to get the natural logarithmic(base e) values of each element of the given array.
• Store it in another variable.
• Print the natural logarithmic values of each element of the given array.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the
# array() function to create an array.
# Store it in a variable.
gvn_arry = np.array([5, 1, 2.4, 10.5, 4])
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
print()
# Pass the above given array as an argument to the log() function of the
# numpy module to get the natural logarithmic(base e) values of each element of
# the given array
# Store it in another variable.
rslt = np.log(gvn_arry)
# Print the natural logarithmic values of each element of the given array
print("The natural logarithmic values of each element of the given array:")
print(rslt)

Output:

The above given array is:
[ 5. 1. 2.4 10.5 4. ]

The natural logarithmic values of each element of the given array:
[1.60943791 0.  0.87546874  2.35137526  1.38629436]

Example2(Plotting a Graph)

Approach:

• Import numpy module using the import keyword
• Import pyplot from the matplotlib module using the import keyword
• Pass some random list as an argument to the array() function of the numpy module to create an array.
• Store it in a variable.
• Pass the above given array as an argument to the log() function of the numpy module to get the natural logarithmic(base e) values of each element of the given array
• Store it in another variable.
• Plot the input array with some random color and marker values using the plot() function of the matplotlib module
• Plot the output array(numpy log values) with some other random color and marker values
using the plot function of the matplotlib module
• Give the title of the plot using the title() function of the matplotlib module
• Display the plot using the show() function of the matplotlib module.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Import pyplot from the matplotlib module using the import keyword
import matplotlib.pyplot as plt
# Pass some random list as an argument to the array() function of the
# numpy module to create an array.
# Store it in a variable.
gvn_arry = np.array([5, 1, 2.4, 10.5, 4])
# Pass the above given array as an argument to the log() function of the
# numpy module to get the natural logarithmic(base e) values of each element of
# the given array
# Store it in another variable.
rslt = np.log(gvn_arry)
# Plot the input array with some random color and marker values using the
# plot function of the matplotlib module
plt.plot(gvn_arry, gvn_arry, color = 'green', marker = "*")

# Plot the output array(numpy log values) with some other random color and marker values
# using the plot() function of the matplotlib module
plt.plot(rslt, gvn_arry, color = 'orange', marker = "o")
# Give the title of the plot using the title() function of the matplotlib module
plt.title("NumPy log values")
plt.xlabel("Output array(natural log values)")
plt.ylabel("Given input array")
# Display the plot using the show() function of the matplotlib module
plt.show()

Output: