🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.log()

AI & DATA SCIENCE // np-log

np.log() computes the natural logarithm (base e) of each element in an array.

Syntax

np.log(arr)

Deep Dive Course

np.log(x) is the inverse of np.exp(): it returns the exponent you'd need to raise e to in order to get x. Like np.sqrt(), it's only defined for non-negative real inputs when working with a real-valued array — log(0) produces -inf, and log of a negative number produces nan plus a RuntimeWarning, since the natural logarithm of a negative real number isn't a real number. NumPy also provides np.log2() and np.log10() for logarithms in other common bases, and np.log1p(x), which computes log(1 + x) with better numerical precision for x values very close to 0.

1Understanding np.log()

np.log(x) is the inverse of np.exp(): it returns the exponent you'd need to raise e to in order to get x. Like np.sqrt(), it's only defined for non-negative real inputs when working with a real-valued array — log(0) produces -inf, and log of a negative number produces nan plus a RuntimeWarning, since the natural logarithm of a negative real number isn't a real number. NumPy also provides np.log2() and np.log10() for logarithms in other common bases, and np.log1p(x), which computes log(1 + x) with better numerical precision for x values very close to 0.

💡

Use np.log1p(x) instead of computing log(1 + x) manually when x is very close to 0 — computing 1 + x first can lose precision due to floating-point rounding, which np.log1p() avoids by handling that case specially.

editor.html
import numpy as np

arr = np.array([1, np.e, np.e ** 2])
print(np.log(arr))
localhost:3000

2Practical Example

Here is a real-world application of np.log() showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.array([1, 0, -1])
print(np.log(arr))
localhost:3000

3Best Practices

Follow these guidelines when working with np.log():

1. Guard against zero or negative inputs before calling np.log() if they're a realistic possibility, since they produce -inf or nan respectively

2. Use np.log1p() instead of computing log(1 + x) manually for values of x very close to zero, to avoid losing floating-point precision

3. Use np.log2()/np.log10() directly instead of dividing np.log(x) by np.log(2)/np.log(10), for both clarity and slightly better numerical precision

⚠️

Tip: Use np.log1p(x) instead of computing log(1 + x) manually when x is very close to 0 — computing 1 + x first can lose precision due to floating-point rounding, which np.log1p() avoids by handling that case specially.

editor.html
import numpy as np

arr = np.array([1, np.e, np.e ** 2])
print(np.log(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1, np.e, np.e ** 2])
print(np.log(arr))
Example 02Advanced Example
import numpy as np

arr = np.array([1, 0, -1])
print(np.log(arr))

Best Practices

  • Guard against zero or negative inputs before calling np.log() if they're a realistic possibility, since they produce -inf or nan respectively
  • Use np.log1p() instead of computing log(1 + x) manually for values of x very close to zero, to avoid losing floating-point precision
  • Use np.log2()/np.log10() directly instead of dividing np.log(x) by np.log(2)/np.log(10), for both clarity and slightly better numerical precision

Interview Question

Why does np.log(0) produce -inf, while np.log(-1) produces nan instead of also being -inf?

Hint: Think about the mathematical limit as x approaches 0, versus what a logarithm of a negative number would even mean.

As x approaches 0 from the positive side, the natural logarithm decreases without bound, mathematically approaching negative infinity, so NumPy represents log(0) as the special floating-point value -inf, which is a well-defined limit. A negative number, on the other hand, has no real logarithm at all — there's no real exponent you can raise e to that produces a negative result, so there's no meaningful numeric answer, real or infinite, and NumPy represents that undefined case as nan instead.

Exercises

MediumPractice using np.log() in a real scenario.
View Solution
import numpy as np

arr = np.array([1, np.e, np.e ** 2])
print(np.log(arr))

Frequently Asked Questions

Why does np.log(0) produce -inf, while np.log(-1) produces nan instead of also being -inf?

As x approaches 0 from the positive side, the natural logarithm decreases without bound, mathematically approaching negative infinity, so NumPy represents log(0) as the special floating-point value -inf, which is a well-defined limit. A negative number, on the other hand, has no real logarithm at all — there's no real exponent you can raise e to that produces a negative result, so there's no meaningful numeric answer, real or infinite, and NumPy represents that undefined case as nan instead.

Related Functions

np-expnp-sqrtnp-power