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.
import numpy as np
arr = np.logspace(0, 3, 4)
print(arr)2Practical Example
Here is a real-world application of np.logspace() showing how it is used in production NumPy code.
import numpy as np
sizes = np.logspace(1, 6, 6, base=2, dtype=int)
print(sizes)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.
import numpy as np
arr = np.logspace(0, 3, 4)
print(arr)