Chapter 5: NumPy Logs

NumPy Logs tutorial — written as if I’m sitting next to you, explaining slowly and patiently, showing many small realistic examples, comparing different log functions, warning about common traps, and giving you patterns you will actually use in real data analysis, machine learning, statistics, signal processing, and scientific computing.

Let’s pretend we’re looking at the same notebook.

Python

1. The four most important logarithm functions in NumPy

NumPy provides four different log functions — each one has its own purpose and safety characteristics.

Function Full name Computes Best used when Domain / Notes
np.log Natural logarithm ln(x) = logₑ(x) Most mathematical & statistical work x > 0
np.log1p log(1 + x) ln(1 + x) Very small positive x Accurate for x ≈ 0
np.log10 Base-10 logarithm log₁₀(x) When you need decadic (common) log x > 0
np.log2 Base-2 logarithm log₂(x) Bits, information theory, computer science x > 0

2. Why do we have log1p? (very important to understand)

For very small x > 0, np.log(1 + x) suffers from catastrophic cancellation in floating-point arithmetic.

Python

Rule #1 (write this down):

Whenever you need log(1 + x) and x is small (especially < 1e-6), always use np.log1p(x) instead of np.log(1 + x)

3. Basic usage examples – all four logs

Python

Output highlights:

text

4. What happens when x ≤ 0? (very common trap)

Python

Output:

text

Important behavior:

  • log(0) → -inf (negative infinity)
  • log(negative) → nan (not a number)
  • log(nan) → nan
  • log(inf) → inf

Safe pattern (you will use this often):

Python

5. Realistic patterns you will actually write many times

Pattern 1 – Log-transform skewed data (very common in ML & statistics)

Python

Pattern 2 – Log-scale plotting (very common)

Python

Pattern 3 – Convert log-returns to prices

Python

Pattern 4 – Log-loss / cross-entropy (machine learning)

Python

Summary – NumPy Log Functions Quick Reference

Function Computes Best used when Danger zone
np.log ln(x) General math / stats x ≤ 0 → nan / -inf
np.log1p ln(1 + x) Very small x > 0 (most important!) x < -1 → nan
np.log10 log₁₀(x) Decadic / engineering / pH / decibels x ≤ 0 → nan / -inf
np.log2 log₂(x) Computer science / bits / information x ≤ 0 → nan / -inf

Final teacher advice (very important)

Golden rule #1 Whenever you write np.log(1 + x) or log(1 + small_value) — replace it with np.log1p(x). This is one of the most common numerical accuracy mistakes people make.

Golden rule #2 Protect against invalid input:

Python

Golden rule #3 Use log-scale plots (plt.xscale(‘log’), plt.yscale(‘log’)) whenever you have heavy-tailed or exponential-looking data.

Would you like to go deeper into any of these areas?

  • How to safely handle log of zero / negative values in real datasets
  • Log-transforms in machine learning (when & why)
  • Logarithmic binning for histograms
  • Realistic mini-project: clean & visualize skewed real-world data (prices, counts…)
  • Difference between log1p vs expm1 (the pair)

Just tell me what you want to focus on next! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *