Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

NumPy logspace: Syntax, Parameters and Examples

How numpy.logspace builds log-spaced grids: exponents vs endpoints, endpoint, base, dtype and axis, with outputs from the NumPy manual and geomspace compared.

Written and maintained by CASRAI Editorial Board

Last updated

numpy.logspace returns numbers spaced evenly on a log scale. The detail that trips most people up is that start and stop are exponents, not endpoints: np.logspace(0, 3, 4) gives 1, 10, 100, 1000 because the function computes 10**0 to 10**3. Its signature in the current NumPy manual (v2.5) is numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0).

What logspace is used for in research

A log-spaced grid is the right choice when a quantity matters across orders of magnitude rather than in absolute steps. Typical research uses are a sweep over a regularisation strength or learning rate (0.001, 0.01, 0.1), a frequency axis, a range of concentrations or sample sizes, and tick positions for a log-scaled plot. A linear grid from 0.001 to 1000 would put almost every point in the top decade; a log grid puts the same number of points in each decade. For evenly spaced values on a linear scale use numpy linspace instead.

Record start, stop, num, endpoint and base in your analysis script or methods note. Without them a reader cannot rebuild the same grid, which matters for computational reproducibility.

Install or access

NumPy is installed from the official installation page. The two commands it gives are:

  • pip install numpy
  • conda install numpy (inside a conda environment, created with conda create -n my-env and activated with conda activate my-env)

To keep a project isolated, the same page shows a virtual environment: python -m venv my-env, then source my-env/bin/activate on macOS and Linux or my-env\Scripts\activate on Windows, followed by pip install numpy. For sharing the environment with collaborators see sharing a reproducible conda environment. Import the package as np; print(np.__version__) confirms which version you have.

Step-by-step: build a log-spaced grid

  1. Import the package: import numpy as np.
  2. Convert the range you want into exponents. For values from 0.001 to 1 with the default base 10, the exponents are -3 and 0.
  3. Choose num, the number of samples (default 50).
  4. Decide whether the last value belongs in the grid. endpoint=True (the default) includes base ** stop; endpoint=False leaves it out.
  5. Call the function, for example np.logspace(-3, 0, num=4), which returns array([0.001, 0.01 , 0.1 , 1. ]).
  6. If you want a different base, pass base. The documentation example np.logspace(2.0, 3.0, num=4, base=2.0) runs from 2**2 to 2**3.

Output and how to read it

The documentation examples show the behaviours that matter most.

Call Result What it shows
np.logspace(2.0, 3.0, num=4) array([ 100., 215.443469, 464.15888336, 1000.]) The sequence runs from 10**2 to 10**3, both included.
np.logspace(2.0, 3.0, num=4, endpoint=False) array([100., 177.827941, 316.22776602, 562.34132519]) The last value is dropped; the samples are the first four of five log-spaced points.
np.logspace(2.0, 3.0, num=4, base=2.0) array([4., 5.0396842, 6.34960421, 8.]) The sequence runs from 2**2 = 4 to 2**3 = 8.

How the spacing is defined

The documentation says the step size between the elements in ln(samples) / ln(base) (or log_base(samples)) is uniform. Equal ratios, not equal differences, separate neighbouring values. For the default base the ratio between neighbours in np.logspace(0, 3, 4) is 10 every time.

For a scalar base the documentation gives the equivalent construction y = np.linspace(start, stop, num=num, endpoint=endpoint) followed by power(base, y).astype(dtype). Seeing logspace as linspace in exponent space explains every parameter.

endpoint=False

With endpoint=False, num + 1 values are spaced over the interval in log space and all but the last are returned. This is why np.logspace(2.0, 3.0, num=4, endpoint=False) ends at 562.34 rather than 1000: the spacing is set by dividing the exponent range into four equal steps.

Array-valued base and axis

Since NumPy 1.25.0 a non-scalar base is supported. The documentation example np.logspace(2.0, 3.0, num=4, base=[2.0, 3.0], axis=-1) returns two rows, one per base: [[4, 5.0396842, 6.34960421, 8], [9, 12.98024613, 18.72075441, 27]]. The axis argument matters only when start, stop or base is array-like; the default 0 puts the samples on a new first axis and -1 puts them at the end.

logspace, geomspace and linspace compared

All three are NumPy functions that return a fixed number of samples. They differ in what you hand them. The outputs below were checked by running each call in NumPy 2.5.

Function What start and stop mean Example Result
np.logspace Exponents of the base np.logspace(0, 3, 4) [1., 10., 100., 1000.]
np.geomspace The actual endpoint values np.geomspace(1, 1000, 4) [1., 10., 100., 1000.]
np.linspace The actual endpoint values, evenly spaced np.linspace(1, 1000, 4) [1., 334., 667., 1000.]

The NumPy manual lists geomspace under “See also” for logspace as “similar to logspace, but with endpoints specified directly”. Its signature is numpy.geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0): there is no base, and it also accepts decreasing and negative inputs (for example 1000 to 1, or -1000 to -1). If you already know the endpoint values you want, geomspace saves the exponent conversion.

Troubleshooting

My values are huge, or the result is inf

This almost always means endpoint values were passed where exponents belong. np.logspace(1, 1000, 3) asks for 10**1 to 10**1000, which exceeds the largest float64; running np.logspace(1, 400, 3) returns [1.e+01, 3.16227766e+200, inf] with an overflow warning. To go from 10 to 1000, use np.logspace(1, 3, num) or np.geomspace(10, 1000, num).

I asked for integers and got floats

The documentation says the inferred type “will never be an integer” and float is chosen even if the arguments would produce an array of integers. Pass dtype explicitly if you need another type. Casting truncates, so check the values afterwards. The geomspace documentation notes that an integer dtype example “may not produce exact integers” (it gives 7 and 63 where 8 and 64 are expected) and shows np.around(...).astype(np.int_) as the exact route.

The last value is not the one I asked for

Check endpoint. With endpoint=False the stop value is excluded, as in the table above.

The shape is not what I expected

If start, stop or base is an array, the samples go on a new axis set by axis. Use axis=-1 to put them last.

Free alternatives

NumPy itself is free and open source, and the alternatives below are part of the same package.

Function Use it when Source
np.geomspace You know the endpoint values and want geometric spacing NumPy manual, geomspace
np.linspace You want evenly spaced values on a linear scale NumPy manual, linspace; see numpy linspace
np.power(base, np.linspace(start, stop, num)) You want the construction spelled out; the manual gives it as the scalar-base equivalent of logspace NumPy manual, logspace notes

FAQ

What does numpy logspace do?

It returns num numbers spaced evenly on a log scale, from base ** start to base ** stop. The default base is 10.0 and the default num is 50.

What is the difference between logspace and geomspace?

In logspace, start and stop are exponents of the base. In geomspace they are the actual endpoint values and there is no base argument.

Does logspace include the endpoint?

Yes by default (endpoint=True). Set endpoint=False to exclude base ** stop.

Can logspace return integers?

Not by inference: the inferred dtype is never an integer. You can pass dtype, but check the result for rounding.

Can I use a base other than 10?

Yes. Pass base, for example base=2.0. Since NumPy 1.25.0 the base can also be an array.

Sources

Related reading on CASRAI: histograms and the normal distribution and multimodal distributions.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about NumPy logspace: Syntax, Parameters and Examples

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →