Return the natural logarithm of one plus the input array element-wise in Numpy

NumpyServer Side ProgrammingProgramming

To return the natural logarithm of one plus the input array, element-wise., use the numpy.log1p() method in Python Numpy. It calculates log(1 + x).

For complex-valued input, log1p is a complex analytical function that has a branch cut [-inf, -1] and is continuous from above on it. log1p handles the floating-point negative zero as an infinitesimal negative number, conforming to the C99 standard.

Steps

At first, import the required library −

import numpy as np

Create an array using the array() method −

arr = np.array([1e-15, 10000, 1e-99])

Display the array −

print("Array...\n", arr)

Get the type of the array −

print("\nOur Array type...\n", arr.dtype)

Get the dimensions of the Array −

print("\nOur Array Dimension...\n",arr.ndim)

Get the shape of the Array −

print("\nOur Array Shape...\n",arr.shape)

To return the natural logarithm of one plus the input array, element-wise., use the numpy.log1p() method. Calculates log(1 + x) −

print("\nResult...\n",np.log1p(arr))

Example

import numpy as np

# Create an array using the array() method
arr = np.array([1e-15, 10000, 1e-99])

# Display the array
print("Array...\n", arr)

# Get the type of the array
print("\nOur Array type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nOur Array Dimension...\n",arr.ndim)

# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)

# To return the natural logarithm of one plus the input array, element-wise., use the numpy.log1p() method in Python Numpy
# Calculates log(1 + x).
print("\nResult...\n",np.log1p(arr))

Output

Array...
[1.e-15 1.e+04 1.e-99]

Our Array type...
float64

Our Array Dimension...
1

Our Array Shape...
(3,)

Result...
[1.00000000e-15 9.21044037e+00 1.00000000e-99]
raja
Updated on 08-Feb-2022 10:06:14

Advertisements