NumPy linspace()

Summary: in this tutorial, you’ll learn how to use the numpy linspace() to create a new numpy array with evenly spaced numbers of a specified interval.

Introduction to the numpy linspace() function

The numpy linspace() function creates a new numpy array with evenly spaced numbers over a given interval:

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)Code language: Python (python)

The linspace() works like the arange() function. But instead of specifying the step size, it defines the number of elements in the interval between the start and stop values.

For example, the following uses the linspace() function to create a new array with five numbers between 1 and 2:

import numpy as np

a = np.linspace(1, 2, 5)

print(a)Code language: Python (python)

Output:

[1.   1.25 1.5  1.75 2.  ]Code language: Python (python)

If you don’t want to include the stop value, you can exclude it using the endpoint parameter. For example:

import numpy as np

a = np.linspace(1, 2, 5, endpoint=False)

print(a)Code language: Python (python)

Output:

[1.  1.2 1.4 1.6 1.8]Code language: Python (python)

Note that the endpoint is True by default. Therefore, the linspace() function returns the stop as the last sample by default.

Summary

  • Use the numpy linspace() function to create a numpy array with evenly spaced numbers over a given interval.
Did you find this tutorial helpful ?