Understand numpy.linspace() with Examples – NumPy Tutorial

By | September 15, 2022

In order to split a sequence from start and end, we can use numpy.linspace() function. In this tutorial, we will use some examples to show you how to use it.

numpy.linspace()

It is defined as:

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

It will split a sequence [start, stop] to num samples.

Parameters

endpoint: if it is True, the endpoint will be returned.

For example:

import numpy

startframe = numpy.linspace(start = 0, stop = 8, num=6)

print(startframe)

Run this code, we will see:

[0.  1.6 3.2 4.8 6.4 8. ]

Because endpoint = True, the startframe starts with 0 and ends with 8.

Moreover:

import numpy

startframe = numpy.linspace(start = 0, stop = 8, num=6, endpoint=False)

print(startframe)

Here endpoint = False, we will see:

[0.         1.33333333 2.66666667 4.         5.33333333 6.66666667]

How about start = stop in numpy.linspace()?

For example:

startframe = numpy.linspace(start = 0, stop = 0, num=6, endpoint=False)

print(startframe)

We will get:

[0. 0. 0. 0. 0. 0.]

Leave a Reply