linspace & logspace
Create a fixed number of evenly spaced values with np.linspace (num, endpoint, retstep) and log-spaced values with np.logspace.
Watch the lesson
1 video on CodeWithMunnaX
Suppose you want an economy-rate scale from 6.5 to 9.5 with exactly 5 marks for a chart axis. With arange you would have to calculate the step yourself (0.75). np.linspace lets you say how many values you want, and it works out the step.
Definition
np.linspace(start, stop, num=50)returnsnumevenly spaced values over the closed interval[start, stop]— both ends are included by default.np.logspace(start, stop, num=50, base=10.0)returnsnumvalues spaced evenly on a logarithmic scale, frombase**starttobase**stop.
- linspace = "linear space": the differences between neighbours are equal.
- Closed interval — unlike
arange, the stop value is included (unlessendpoint=False). - num — how many values you want; the step is computed as
(stop - start) / (num - 1). - logspace — the ratios between neighbours are equal (1, 10, 100, 1000). Note:
startandstopare exponents, not the values themselves.
Syntax
python
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)| Parameter | Meaning | Default |
|---|---|---|
start | First value (linspace) / first exponent (logspace) | required |
stop | Last value / last exponent | required |
num | Number of values to generate | 50 |
endpoint | Include stop? If False, the interval becomes [start, stop) | True |
retstep | (linspace) Also return the step size, as (array, step) | False |
base | (logspace) Base of the log scale | 10.0 |
dtype | Output data type | float64 |
Example — linspace basics
python
import numpy as np
print(np.linspace(6.0, 12.0, num=7))
print(np.linspace(0, 1, 5))
print(np.linspace(0, 10, 4))
print(np.linspace(10, 0, 3))
print(len(np.linspace(1, 100)))text
[ 6. 7. 8. 9. 10. 11. 12.]
[0. 0.25 0.5 0.75 1. ]
[ 0. 3.33333333 6.66666667 10. ]
[10. 5. 0.]
50How it works:
- Run-rate scale 6 to 12 with 7 values → step = (12 − 6) / (7 − 1) = 1.
- 0 to 1 with 5 values → step 0.25, and 1 is included.
- 0 to 10 with 4 values → step 3.333…; values do not need to be "nice".
start > stopsimply counts down.- If you omit
num, you get 50 values — a common surprise.
Example — endpoint and retstep
python
import numpy as np
print(np.linspace(0, 1, 5, endpoint=False))
vals, step = np.linspace(6.5, 9.5, num=5, retstep=True)
print(vals, step)
print(np.linspace(1, 5, 5, dtype=int))text
[0. 0.2 0.4 0.6 0.8]
[6.5 7.25 8. 8.75 9.5 ] 0.75
[1 2 3 4 5]How it works:
endpoint=Falseexcludesstop, so the step becomes(stop − start) / num= 0.2.retstep=Truereturns a tuple(array, step)— unpack it into two variables. Here the economy scale step is 0.75.dtype=intconverts the values to integers.
Example — logspace
python
import numpy as np
print(np.logspace(0, 3, 4))
print(np.logspace(1, 3, 3, base=2))
print(np.logspace(0, 2, 5))
print(np.geomspace(1, 1000, 4))text
[ 1. 10. 100. 1000.]
[2. 4. 8.]
[ 1. 3.16227766 10. 31.6227766 100. ]
[ 1. 10. 100. 1000.]How it works:
np.logspace(0, 3, 4)= 10⁰, 10¹, 10², 10³ — the arguments are exponents.- With
base=2: 2¹, 2², 2³. np.logspace(0, 2, 5)puts 5 points between 1 and 100, each about 3.16× the previous.np.geomspace(start, stop, num)does the same but takes the actual start/stop values instead of exponents — often easier to read.
Log-spaced values are used for things that grow by multiplication: learning rates in ML (0.001, 0.01, 0.1), audience growth, sound frequencies.
Comparison — arange vs linspace vs logspace
np.arange | np.linspace | np.logspace | |
|---|---|---|---|
| You specify | the step | the count (num) | the count, in exponent terms |
| Stop included? | No | Yes (default) | Yes (default) |
| Spacing | equal differences | equal differences | equal ratios |
| Best for | integer sequences | float ranges, plot axes | scales spanning orders of magnitude |
| Example | np.arange(0, 1, 0.25) → 4 values | np.linspace(0, 1, 5) → 5 values | np.logspace(0, 3, 4) → 1 … 1000 |
Common mistakes
- ❌ Thinking the 3rd argument is the step (like arange):
np.linspace(0, 10, 2)gives[0. 10.], not steps of 2. ✅ The 3rd argument is num. - ❌ Forgetting the default
num=50. ✅ Always passnumexplicitly. - ❌ Writing
np.logspace(1, 1000, 4)expecting 1 … 1000. ✅ Exponents:np.logspace(0, 3, 4), or usenp.geomspace(1, 1000, 4). - ❌ Using
retstep=Trueand treating the result as an array. ✅ It returns a tuple:vals, step = ....
Interview tip
Q: What is the difference between np.arange(0, 1, 0.25) and np.linspace(0, 1, 5)?
A: Both have a step of 0.25, but arange excludes the stop (4 values: 0, 0.25, 0.5, 0.75) while linspace includes it (5 values ending in 1.0). arange is driven by the step, linspace by the number of points.
Key points to remember
- np.linspace(start, stop, num) returns num evenly spaced values, including both start and stop by default.
- The step is (stop - start) / (num - 1); you choose the count, NumPy computes the step.
- num defaults to 50 — always pass it explicitly.
- endpoint=False excludes stop; retstep=True returns (array, step).
- np.logspace(a, b, num) gives base**a … base**b with equal ratios; a and b are exponents.
- np.geomspace takes actual start/stop values for log spacing.
- Prefer linspace over arange for float ranges — no off-by-one surprises.
Try it yourself
Run-rate and economy scales
arange vs linspace side by side
logspace and geomspace
Practice: strike-rate axis
Revision Flashcards
Tap a card to flip it and see the answer.
