🚀 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.logspace()

AI & DATA SCIENCE // np-logspace

np.logspace() creates a 1D array of values evenly spaced on a logarithmic scale, generated by raising a base to a set of evenly spaced exponents.

Syntax

np.logspace(start, stop, num=50, base=10.0)

Deep Dive Course

np.logspace(start, stop, num) first generates num evenly spaced exponents between start and stop, exactly like np.linspace() would, then raises base, 10 by default, to each of those exponents — so np.logspace(0, 3, 4) produces 10 to the powers 0, 1, 2, and 3, giving [1, 10, 100, 1000]. This is the natural choice whenever you need sample points that span several orders of magnitude, such as testing an algorithm across dataset sizes from 10 to 10 million, where evenly spaced values on a linear scale would cluster all your samples at the low end.

1Understanding np.logspace()

np.logspace(start, stop, num) first generates num evenly spaced exponents between start and stop, exactly like np.linspace() would, then raises base, 10 by default, to each of those exponents — so np.logspace(0, 3, 4) produces 10 to the powers 0, 1, 2, and 3, giving [1, 10, 100, 1000]. This is the natural choice whenever you need sample points that span several orders of magnitude, such as testing an algorithm across dataset sizes from 10 to 10 million, where evenly spaced values on a linear scale would cluster all your samples at the low end.

💡

Reach for np.logspace() whenever a linear scan of values would cluster almost all your samples uselessly at the low end of a wide range, like benchmark sizes spanning several orders of magnitude, since exponential spacing distributes samples evenly across each order of magnitude instead.

editor.html
import numpy as np

arr = np.logspace(0, 3, 4)
print(arr)
localhost:3000

2Practical Example

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

editor.html
import numpy as np

sizes = np.logspace(1, 6, 6, base=2, dtype=int)
print(sizes)
localhost:3000

3Best Practices

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

1. Use np.logspace() instead of np.linspace() when your value range spans multiple orders of magnitude and you want even coverage of each magnitude

2. Pass the start and stop arguments as exponents, not as the actual desired values, since they represent powers of base

3. Set base explicitly (e.g. base=2) when working in a domain that naturally uses a different logarithmic base than 10

⚠️

Tip: Reach for np.logspace() whenever a linear scan of values would cluster almost all your samples uselessly at the low end of a wide range, like benchmark sizes spanning several orders of magnitude, since exponential spacing distributes samples evenly across each order of magnitude instead.

editor.html
import numpy as np

arr = np.logspace(0, 3, 4)
print(arr)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.logspace(0, 3, 4)
print(arr)
Example 02Advanced Example
import numpy as np

sizes = np.logspace(1, 6, 6, base=2, dtype=int)
print(sizes)

Best Practices

  • Use np.logspace() instead of np.linspace() when your value range spans multiple orders of magnitude and you want even coverage of each magnitude
  • Pass the start and stop arguments as exponents, not as the actual desired values, since they represent powers of base
  • Set base explicitly (e.g. base=2) when working in a domain that naturally uses a different logarithmic base than 10

Interview Question

Why does np.logspace(1, 3, 3) produce [10, 100, 1000] rather than [1, 2, 3]?

Hint: Think about what the start and stop arguments actually represent.

The start and stop arguments to np.logspace() are exponents applied to the base, not the literal output values themselves. np.logspace(1, 3, 3) generates the evenly spaced exponents 1, 2, and 3, then raises the default base of 10 to each of them, producing 10 to the first, second, and third powers, which is [10, 100, 1000]. This is easy to misread if you expect the arguments to be the actual values in the resulting array, the way they are in np.linspace().

Exercises

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

arr = np.logspace(0, 3, 4)
print(arr)

Frequently Asked Questions

Why does np.logspace(1, 3, 3) produce [10, 100, 1000] rather than [1, 2, 3]?

The start and stop arguments to np.logspace() are exponents applied to the base, not the literal output values themselves. np.logspace(1, 3, 3) generates the evenly spaced exponents 1, 2, and 3, then raises the default base of 10 to each of them, producing 10 to the first, second, and third powers, which is [10, 100, 1000]. This is easy to misread if you expect the arguments to be the actual values in the resulting array, the way they are in np.linspace().

Related Functions

np-linspacenp-arangenp-power