Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

NumPy linspace: Formula, Endpoint Behavior, and How It Compares With arange

NumPy’s linspace is count-driven: it returns a specified number of evenly spaced samples, including the endpoint by default. See the formulas, endpoint=False behavior, and when arange is the better fit.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

np.linspace(start, stop, num) returns a requested number of evenly spaced samples. By default, it includes both start and stop; set endpoint=False to omit stop while keeping the same sample count. Use linspace when the number of values matters, and np.arange when a fixed step is the natural requirement.

What values does np.linspace return?

In NumPy, num is the number of samples to produce. It defaults to 50 and must be nonnegative. For example:

As an Amazon Associate I earn from qualifying purchases.

import numpy as np

np.linspace(2.0, 3.0, num=5)
# array([2.  , 2.25, 2.5 , 2.75, 3.  ])

There are five values, including both bounds, with a spacing of 0.25. The sample count—not a step size—is the input that controls how many values are returned. NumPy documents linspace as returning evenly spaced numbers over a specified interval: NumPy linspace reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What is the linspace formula?

For scalar bounds and num > 1, let i be a sample index from 0 through num - 1.

  • With the default endpoint=True, the value at index i is start + i * (stop - start) / (num - 1). Dividing the interval into num - 1 gaps makes the last sample equal to stop.
  • With endpoint=False, the value at index i is start + i * (stop - start) / num. The interval is divided into num gaps, and the returned samples omit stop.

For zero or one requested sample, do not apply those denominator formulas: the count and endpoint setting determine the result directly, and the formula above is specifically for more than one sample.

Does np.linspace include the endpoint?

Yes, by default: endpoint=True. With five samples from 2 to 3, the values are [2.0, 2.2, 2.4, 2.6, 2.8] when endpoint=False. The final value is below 3 because NumPy preserves the five-sample count while excluding the upper bound. This half-open behavior is useful, for example, when building a periodic grid where including both ends would duplicate a boundary point.

Set the option explicitly when endpoint placement matters:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
np.linspace(2.0, 3.0, num=5, endpoint=False)
# array([2. , 2.2, 2.4, 2.6, 2.8])

When should you use linspace instead of arange?

The key difference is what you specify: linspace takes a sample count, while arange takes a step size. NumPy describes arange as similar to linspace but using a step size instead of a number of samples: NumPy arange reference.

Decision np.linspace np.arange
Main input Number of samples (num) Increment (step)
Usual interval behavior Includes stop by default; excludes it with endpoint=False Normally half-open: includes start and excludes stop
Best fit A fixed number of points or deliberate endpoint placement A sequence defined by a fixed increment, especially an integer step
Floating-point concern Count is explicit, though individual values can still be floating-point approximations Output length may be numerically unstable, and the last value can exceed stop

For a grid that must contain exactly 101 values from 0 to 1, use np.linspace(0, 1, 101). For integer values at increments of 2 below 10, use np.arange(0, 10, 2). These examples express the difference in intent: choose a count for the first requirement and a step for the second.

Floating-point steps such as 0.1 are a case where the choice matters. The arange reference warns that output length can be unstable and the last value can exceed stop because of floating-point rounding and internal casting behavior. NumPy’s array-creation guide also recommends linspace for a fixed-size grid: NumPy array creation guide. Using linspace makes the count predictable, but it does not make floating-point representations exact.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What do retstep, axis, and dtype change?

Get the spacing with retstep=True

Pass retstep=True when downstream code also needs NumPy’s spacing. The return is a pair: the sample array and the step value.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
samples, step = np.linspace(2.0, 3.0, num=5, retstep=True)
# samples: array([2.  , 2.25, 2.5 , 2.75, 3.  ])
# step: 0.25

Place the sample dimension with axis

If start or stop is array-like, axis selects where the sample dimension is inserted. Its default is axis 0. This matters when the samples are part of a multidimensional result and its layout must match surrounding arrays.

Request integer output deliberately

By default, linspace does not infer an integer dtype, even when the bounds or some results are whole numbers. If you explicitly request an integer dtype, current NumPy documentation says values round toward negative infinity. That behavior changed in NumPy 1.20.0; it can differ from truncation toward zero for negative, non-integral values. If truncation-like conversion is intended, generate the default result and then call .astype(int) instead. See the NumPy linspace reference for the documented dtype behavior and version note.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.