Log transformation
Transforming positive measurements or model features whose multiplicative relationships are easier to analyze additively.
NumPy · Function
Computes the natural logarithm, base e, element-wise. It accepts scalars and array-like inputs and exposes standard NumPy ufunc options for masking, output storage, casting, and dtype control.
Call np.log with a scalar or array-like input to compute the natural logarithm of each value. Optional ufunc controls such as out, where, dtype, and casting govern destination storage, selected elements, output type, and conversion rules.
numpy.log(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature])| Name | Type | Description |
|---|---|---|
x | array_like | Input values. |
out | ndarray, None, or tuple of ndarray and None | Optional output array or tuple of arrays and None. |
where | array_like of bool, optional | Condition broadcast over the input; only true locations are evaluated. |
casting | {'no','equiv','safe','same_kind','unsafe'}, optional | Controls casting used for the computation. |
order | {'K','A','C','F'}, optional | Memory layout order for the result. |
dtype | dtype, optional | Output data type. |
subok | bool, optional | Whether subclasses are passed through. |
signature | string or None, optional | Optional generalized-ufunc signature. |
An ndarray containing the natural logarithm of x element-wise, or a scalar when x is scalar.
import numpy as np
x = np.array([1.0, np.e, np.e**2, 0.0])
y = np.log(x)
print(y) # [ 0. 1. 2. -inf]Computes natural logarithms element by element; zero produces negative infinity.
Transforming positive measurements or model features whose multiplicative relationships are easier to analyze additively.
Calculating natural-log terms in probability, optimization, and scientific formulas.
Applying one mathematical operation efficiently across an ndarray without an explicit Python loop.
np.log computes the natural logarithm, base e, element-wise. It is the inverse of the exponential function, so log(exp(x)) equals x mathematically.
Follow-up: How would you compute another base? Divide np.log(x) by np.log(base), or use np.log10 or np.log2 when those bases are appropriate.
For a scalar input, the result is a scalar; for array input, NumPy returns the element-wise logarithms in an ndarray. Zero maps to negative infinity.
Follow-up: What should be done for negative real data? Use np.emath.log if complex-valued logarithms are intended.
The ufunc accepts an out destination and a where mask, along with dtype, casting, order, and subok controls. where selects which elements are evaluated or written.
Follow-up: Why can masked output contain unexpected values? If out is newly allocated, locations skipped by where remain uninitialized unless initialized first.
numpy.log10numpy.log2numpy.log1pnumpy.emath.lognumpy.exp