Skip to content
Related Articles

Related Articles

Compute the natural logarithm of one plus each element in floating-point accuracy Using NumPy
  • Last Updated : 02 Sep, 2020
GeeksforGeeks - Summer Carnival Banner

 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:



Python3




# 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:

Python3




# 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:

Python3




# 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]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :