np.linspace() in Python | numpy.linspace() in Python

Numpy linspace() function is used to create evenly spaced sequences between the specified interval. Here, you specify the starting and ending point of an interval and the total number of breakpoints that you want within this interval. One thing you must remember that starting and ending points are also included in that breakpoints. np.linspace() function returns Numpy array of evenly spaced values.

Syntax Numpy linspace

np.linspace(start, stop, num=50, dtype=None, endpoint=True)

start parameter is used to specify the starting point of the interval.

stop parameter is used to specify the ending point of the interval.

num parameter is optional and is used to specify the total number of items to be included within the range.

Note- If you do not want to include the ending point of an interval then set the value of endpoint parameter to False.

Let's understand linspace() function with the help of a simple example:

np.linspace(start=0, stop=50, num=6, dtype=int)

The above code will return a below Numpy array-

0 10 20 30 40 50

If we specify endpoint=False then 50 will not be included:

np.linspace(start=0, stop=50, num=5, endpoint=False, dtype=int)

Graphical Representation of numpy.linspace() using Matplotlib

import numpy as np
import matplotlib.pyplot as plt

data = np.linspace(start=0, stop=50, num=6)
plt.plot(data)
plt.show()

Output of the above code-

np.linspace() in Python

Recommended Posts