NumPy log1p()
The numpy.log1p()
function computes the natural logarithm of one plus the input, element-wise.
This is useful for computing log(1 + x)
accurately, especially for small values of x
, where direct computation of log(1 + x)
may lose precision.
Syntax
</>
Copy
numpy.log1p(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Input values for computing log(1 + x) . |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when computing log1p() . |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array with the natural logarithm of 1 + x
computed for each element. If the input is a scalar, a scalar is returned.
Examples
1. Computing log(1 + x) for a Single Value
Here, we compute log(1 + x)
for a single scalar value.
</>
Copy
import numpy as np
# Define a single input value
x = 0.5
# Compute log(1 + x)
result = np.log1p(x)
# Print the result
print("log(1 + 0.5):", result)
Output:
log(1 + 0.5): 0.4054651081081644

2. Computing log(1 + x) for an Array
We compute log(1 + x)
for multiple elements in an array.
</>
Copy
import numpy as np
# Define an array of input values
x = np.array([0, 0.1, 0.5, 1, 10])
# Compute log(1 + x) for each element
log_values = np.log1p(x)
# Print the results
print("Input values:", x)
print("Computed log(1 + x):", log_values)
Output:
Input values: [ 0. 0.1 0.5 1. 10. ]
Computed log(1 + x): [0. 0.09531018 0.40546511 0.69314718 2.39789527]

3. Using the out
Parameter
Using an output array to store results instead of creating a new array.
</>
Copy
import numpy as np
# Define an array of input values
x = np.array([0.1, 0.2, 0.3, 0.4])
# Create an output array with the same shape
output_array = np.empty_like(x)
# Compute log(1 + x) and store the result in output_array
np.log1p(x, out=output_array)
# Print the results
print("Computed log(1 + x):", output_array)
Output:
Computed log(1 + x): [0.09531018 0.18232156 0.26236426 0.33647224]

4. Using the where
Parameter
Using a condition to compute log(1 + x)
only for selected elements.
</>
Copy
import numpy as np
# Define an array of input values
x = np.array([0, 0.1, 0.5, -0.9])
# Define a mask (compute log1p only where mask is True)
mask = np.array([True, False, True, True])
# Compute log(1 + x) where mask is True
result = np.log1p(x, where=mask)
# Print the results
print("Computed log(1 + x) with mask:", result)
Output:
Computed log(1 + x) with mask: [ 0. 0. 0.40546511 -2.30258509]

The log1p()
values are computed only for elements where mask=True
. The other values remain unchanged.