NumPy log()

The numpy.log() function computes the natural logarithm (log base e) of each element in an input array. It is the inverse operation of the exponential function, satisfying the identity log(exp(x)) = x.

Syntax

</>
Copy
numpy.log(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput values. Must be positive since log is undefined for negative numbers.
outndarray, None, or tuple of ndarray and None, optionalOptional output array to store results. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying where to compute the logarithm. Other values retain their original state.
castingstr, optionalDefines how data is cast during computation.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalData type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array containing the natural logarithm of the input values, computed element-wise. If the input is a scalar, a scalar is returned.


Examples

1. Computing the Natural Logarithm of a Single Value

We compute the natural logarithm of a single positive value.

</>
Copy
import numpy as np

# Define a positive number
num = np.e  # Euler's number (approximately 2.718)

# Compute the natural logarithm
result = np.log(num)

# Print the result
print("Natural logarithm of e:", result)

Output:

Natural logarithm of e: 1.0