Written and maintained by CASRAI Editorial Board
Last updated
numpy.log returns the natural logarithm (base e) of every element of an array, and it returns a scalar when you pass a scalar. Call it as np.log(x) after import numpy as np. For base 10, base 2, or values very close to zero, NumPy has separate functions: np.log10, np.log2 and np.log1p. This guide follows the NumPy 2.5 reference manual and was checked against NumPy 2.5.1 on 2026-10-29.
What np.log is used for in research
The natural logarithm is the inverse of the exponential function, so np.log(np.exp(x)) gives back x. Researchers reach for it when a variable spans several orders of magnitude (concentrations, counts, incomes, reaction rates), when a right-skewed variable needs to be brought closer to symmetry before modelling, and when a multiplicative relationship should become an additive one. The reasoning behind a log transform, and when it is appropriate, is covered in the log transformation formula guide; the distribution whose logarithm is normal is described in the lognormal distribution guide. A geometric mean is the exponential of the mean of logs, see the geometric mean guide.
Install or access
NumPy’s installation page (numpy.org/install) gives these routes. With pip, run pip install numpy. To keep it isolated, create a virtual environment first with python -m venv my-env, activate it (source my-env/bin/activate on macOS and Linux, my-env\Scripts\activate on Windows), then run pip install numpy. With conda, run conda create -n my-env, conda activate my-env, then conda install numpy. Confirm the install with:
import numpy as np
print(np.__version__)
If the import fails, NumPy maintains an ImportError troubleshooting page at numpy.org/doc/stable/user/troubleshooting-importerror.html.
Step-by-step: natural log, other bases and log1p
1. Take the natural log of a number or an array
- Import NumPy:
import numpy as np. - Call
np.logon a scalar or an array:np.log(np.array([1, 10, 100])). - Read the result, a float array:
array([0. , 2.30258509, 4.60517019]).
The signature in the manual is numpy.log(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]). Only x is required. The manual’s own example is:
np.log([1, np.e, np.e**2, 0])
# array([ 0., 1., 2., -inf])
How do I take a log with a different base?
For base 10 and base 2, use the dedicated functions: np.log10(x) and np.log2(x). The manual shows np.log10([1e-15, -3.]) returning array([-15., nan]) and np.log2(np.array([0, 1, 2, 2**4])) returning array([-inf, 0., 1., 4.]).
For any other base b, use the change-of-base identity, logb(x) = ln(x) / ln(b), which is the same definition Python’s standard library uses for math.log(x, base) (“calculated as log(x)/log(base)” in the Python documentation). In NumPy that is:
np.log(np.array([8, 27])) / np.log(3)
# array([1.89278926, 3. ])
np.log vs np.log10
np.log is base e (about 2.718), np.log10 is base 10. They differ only by a constant factor, so a regression on log10(y) has the same fit as one on ln(y); only the coefficient scale changes. Use base 10 when readers expect orders of magnitude, and base e when coefficients should read as approximate proportional changes.
2. Use log1p when values can be zero or very small
np.log1p(x) computes log(1 + x). The manual states that for real input it is accurate even for x so small that 1 + x == 1 in floating-point arithmetic. Its own example shows the difference:
np.log1p(1e-99) # 1e-99
np.log(1 + 1e-99) # 0.0
For count data that contains zeros, np.log1p(counts) maps 0 to 0 instead of -inf, which is the usual reason it is chosen over adding a constant by hand. Whether a log(1 + x) transform is statistically appropriate for your data is a separate question from whether the function is numerically accurate.
3. Log an array that contains zeros without producing -inf
The where argument limits the computation to positions where a condition is true. Per the manual, at those positions out is set to the result, and elsewhere out keeps its original value. Because of that, always supply an initialised out; with the default out=None the skipped positions remain uninitialised.
x = np.array([0., 1., 10., 100.])
out = np.zeros_like(x)
np.log(x, out=out, where=x > 0)
# array([0. , 0. , 2.30258509, 4.60517019])
Note that the 0 in the first position is a placeholder you chose, not a logarithm. Record that choice in your methods. To select or replace values by condition more generally, see the np.where guide.
Output and how to read it
| Input | np.log result | Source |
|---|---|---|
| Positive real | The natural log, a float | NumPy manual, numpy.log |
| Exactly 1 | 0.0 | Manual example |
| 0 | -inf | Manual example, np.log([1, np.e, np.e**2, 0]) |
| Negative real | nan; the invalid floating-point flag is set | Manual: values that cannot be expressed as a real number or infinity yield nan |
| Complex | Complex result; branch cut [-inf, 0], imaginary part in (-pi, pi] | Manual |
The result has the same shape as the input, and it is a scalar if the input is a scalar. Integer input returns floats. For real input, np.log always returns real output, so a negative number gives nan rather than a complex number. If you want the complex result for negatives, the manual points to numpy.emath.log; np.log(-1 + 0j) also returns 3.141592653589793j because the input is complex. To handle nan values that appear downstream, see the NumPy nan guide.
Troubleshooting
What does np.log(0) return?
It returns -inf. In recent NumPy versions this also prints a RuntimeWarning (“divide by zero encountered in log”) when run interactively. The warning is observed behaviour rather than something the manual documents.
Why do I get nan from np.log?
At least one input is negative (or already nan). Real input never produces a complex result, so the invalid value is returned as nan, with a RuntimeWarning (“invalid value encountered in log”) in the version tested. Check the sign of your data, for example whether a baseline-subtracted measurement went below zero, before choosing a fix.
Why do infinities spread through my statistics?
A single -inf makes a mean or standard deviation of the logged array -inf or nan. Use np.log1p, the masked where pattern above, or remove the zero rows deliberately and say so.
Why is np.log(1 + x) returning 0.0 for tiny x?
Adding 1 loses the small value in floating-point arithmetic. Use np.log1p(x).
Why does import numpy fail?
See the ImportError troubleshooting page linked under Install or access.
Free alternatives and related functions
| Function | What it does | Source |
|---|---|---|
numpy.log10 |
Base 10 logarithm, element-wise; nan for negative input | NumPy manual |
numpy.log2 |
Base 2 logarithm of x | NumPy manual |
numpy.log1p |
log(1 + x), accurate for tiny x | NumPy manual |
numpy.emath.log |
Natural log that returns a complex result for negative real input | NumPy manual, “See also” of numpy.log |
math.log(x[, base]) |
Python standard library; one argument gives the natural log, two arguments give the log to the given base, calculated as log(x)/log(base). Works on single numbers, not arrays | Python documentation, math module |
FAQ
Is np.log the natural log or base 10?
Natural log (base e). Use np.log10 for base 10.
How do I take the log of every column of a table?
Convert the numeric columns to a NumPy array and call np.log on it; the function works element-wise on arrays of any shape. To build evenly spaced input values for a plot, see the np.linspace guide.
What is the difference between np.log and np.log1p?
np.log1p(x) equals np.log(1 + x) but stays accurate for very small x, where 1 + x rounds to 1.
How do I undo np.log?
Apply np.exp. The manual states that log(exp(x)) = x.
Sources
All sources below were checked on 2026-10-29.
- NumPy v2.5 Manual, numpy.log: https://numpy.org/doc/stable/reference/generated/numpy.log.html
- NumPy v2.5 Manual, numpy.log1p: https://numpy.org/doc/stable/reference/generated/numpy.log1p.html
- NumPy v2.5 Manual, numpy.log10: https://numpy.org/doc/stable/reference/generated/numpy.log10.html
- NumPy v2.5 Manual, numpy.log2: https://numpy.org/doc/stable/reference/generated/numpy.log2.html
- NumPy, Installing NumPy: https://numpy.org/install/
- NumPy, ImportError troubleshooting: https://numpy.org/doc/stable/user/troubleshooting-importerror.html
- Python documentation, math module: https://docs.python.org/3/library/math.html








