AppDividend
Latest Code Tutorials

np.log1p: What is Numpy log1p() Method in Python

NumPy is a third-party library for the Python language, adding support for large, multi-dimensional arrays and matrices, accompanied by many high-level mathematical functions to execute on these arrays.

Numpy log() function is used to get the natural logarithm of value x+1, where x is an element of an array or x is an object. The log1p is the reverse of exp(x) – 1. 

np.log1p

The np.log1p() is a mathematical numpy library function that helps the user calculate the natural logarithmic value of x+1, where x belongs to all the input array elements. The log1p() is reverse of exp(x) – 1.

The np.log1p() function accepts two arguments: array and out parameters and returns an array of natural logarithms of value x+1 of the elements of the given array elements.

Syntax

numpy.log1p(array[, out] = ufunc ‘log1p’)

Parameters

The np.log1p() function can take up to two main arguments:

  1. array: This is the input array or the object whose log is to be calculated. But the function will add 1 with all elements of the array.
  1. out: This is an optional field. A position the result is stored in. If given, it must have the form to which the inputs convey. If not given or None, it returns a freshly-allocated list. A tuple must have the same length as the number of outputs (possible only as a keyword argument).

Return Value

The log1p() function returns an array of natural logarithms of value x+1 of the elements of the given array elements.

Program to show the working of numpy.log()

See the following code.

# Program to show the working of numpy.log

# Importing numpy
import numpy as np

# We will create an 1D array
arr = np.array([4, 14, 10, 63, 11, 4, 64])
# Printing the array
print("The array is: ", arr)
# Shape of the array
print("Shape of the array is : ", np.shape(arr))

# Calculating natural log of value arr[i]+1
out = np.log1p(arr)
print("Natural logarithm of the given array is ")
print(out)

Output

The array is:  [ 4 14 10 63 11  4 64]
Shape of the array is :  (7,)
Natural logarithm of the given array is
[1.60943791 2.7080502  2.39789527 4.15888308 2.48490665 1.60943791
 4.17438727]

Explanation

In this program, we declared an array of shape 7, and we printed the array. Then we have called numpy.log1p() to calculate the natural logarithm of value arr[i]+1 of the elements of the given array.

Graphical representation of log()

See the following code.

# Program to show Graphical represntation

# Importing numpy
import numpy as np
import matplotlib.pyplot as plt

# We will create an 1D array
arr = np.array([40, 2.4, 0.14, 63, 1.2, 1, 4])
# Printing the array
print("The array is: ", arr)
# Shape of the array
print("Shape of the array is : ", np.shape(arr))

# Calculating natural log of value arr[i]+1
out = np.log1p(arr)
print("Natural logarithm of the given array is ")
print(out)

# Ploting of original array in Graph
# Color will be in Green
plt.plot(arr, arr, color='green', marker='x')

# Ploting of natural log array in Graph
# Color will be in blue
plt.plot(out, arr, color='blue', marker='o')

# Showing the Graphical represntation
plt.title("numpy.log1p()")
plt.xlabel("Natural Log Array")
plt.ylabel("Original Array")
plt.show()

Output

The array is:  [40.    2.4   0.14 63.    1.2   1.    4.  ]
Shape of the array is :  (7,)
Natural logarithm of the given array is
[3.71357207 1.22377543 0.13102826 4.15888308 0.78845736 0.69314718
 1.60943791]

np log1p() in Python

Explanation

In this program, we have first declared an array of shape 7; we have printed the array where array elements are in float data type. Then we have called numpy.log1p() to calculate the natural logarithm of the elements of the given array.

After that, we have plotted the original array in a 2D graph which indicates using the Greenline. We haFinally, we plotted the out array, which we got after finding the natural logarithm, and this shows using the blue line. We can see the result in the above-given image.

That is it for this tutorial.

See also

Numpy log()

Numpy exmp1()

Numpy exp()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.