The numpy.linspace()
function in NumPy is used to create evenly spaced numbers over a specified interval. It’s especially useful in numerical simulations, plotting, and mathematical computations where a range of values is needed.
The basic syntax is:
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False)
start
and stop
define the range.num
specifies how many samples to generate (default is 50).endpoint=True
includes the stop value; if set to False
, it excludes it.retstep=True
returns the step size along with the array.For example, np.linspace(0, 1, 5)
returns [0. , 0.25, 0.5 , 0.75, 1. ]
.
Unlike np.arange()
, which uses a fixed step size, linspace()
ensures that the values are evenly distributed between the start and stop points, regardless of the interval size.