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

2. Computing Logarithm for an Array of Values

We compute the natural logarithm for multiple positive numbers stored in an array.

</>
Copy
import numpy as np

# Define an array of positive numbers
values = np.array([1, np.e, 10, 100, 1000])

# Compute the natural logarithm
log_values = np.log(values)

# Print the results
print("Input values:", values)
print("Natural logarithm:", log_values)

Output:

Input values: [   1.            2.71828183   10.          100.         1000.        ]
Natural logarithm: [0.         1.         2.30258509 4.60517019 6.90775528]

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 values
values = np.array([1, np.e, 10, 100])

# Create an empty output array
output_array = np.empty_like(values)

# Compute logarithm and store in output_array
np.log(values, out=output_array)

# Print results
print("Computed logarithm values:", output_array)

Output:

Computed logarithm values: [0.         1.         2.30258509 4.60517019]

4. Using the where Parameter

Using a condition to compute the logarithm only for selected elements.

</>
Copy
import numpy as np

# Define an array of values
values = np.array([1, 10, 100, 1000])

# Define a mask (compute log only where mask is True)
mask = np.array([True, False, True, False])

# Compute log values where mask is True
result = np.log(values, where=mask)

# Print the results
print("Computed logarithm values with mask:", result)

Output:

Computed logarithm values with mask: [0.         0.         4.60517019 0.        ]

The logarithm values are computed only for elements where mask=True. The other values remain unchanged.