Open In App

Compute the natural logarithm of one plus each element in floating-point accuracy Using NumPy

 Let’s see the program for computing the natural logarithm of one plus each element of a given array in floating-point accuracy using NumPy library.

For doing this task we are using numpy.log1p() function of NumPy. This function returns the array of natural logarithm of one plus each element of the input array.



Syntax: numpy.log1p(arr, out = None, *, where = True, casting = ‘same_kind’, order = ‘K’, dtype = None, ufunc ‘log1p’)

Now, let’s see an example:



Example 1:




# Import numpy library
import numpy as np
  
# Create a numpy array
arr = np.array([1e-90, 1e-100])
  
# Applying the function
rslt = np.log1p(arr)
  
print(rslt)

Output:

[1.e-090 1.e-100]

Example 2:




# Import numpy library
import numpy as np
  
# Create a numpy array
arr = np.array([1, 2, 3, 4])
  
# Applying the function
rslt = np.log1p(arr)
  
print(rslt)

Output:

[0.69314718 1.09861229 1.38629436 1.60943791]

Example 3:




# Import numpy library
import numpy as np
  
# Create a numpy array
arr = np.array([1, 1e-1, 3, 1e-0])
  
# Applying the function
rslt = np.log1p(arr)
  
print(rslt)

Output:

[0.69314718 0.09531018 1.38629436 0.69314718]

Article Tags :