numpy.linspace() in Python: Introduction, Syntax & Examples

The numpy.linspace() function in Python’s NumPy library creates an array of evenly spaced values over a specified range. It’s particularly useful when we need to generate a set of data points for mathematical functions or plots, and we want precise control over the number of points within a specified interval.

In this article, we will understand Python numpy.linspace() function, its syntax, and learn how to use it with the help of five examples. Let’s get started.

Syntax of numpy.linspace() Function

It is almost similar to numpy.arange() function but the question arises when to use numpy.arange() and when to use numpy.linspace(). So the simplest answer will be when we need to specify the step size between two values we can use numpy.arange() and when we need to specify the number of points between two values we can use numpy.linspace().

Below is the syntax for using the numpy.linspace() function in Python.

Syntax:

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

Parameters:

  • start: The starting value of the range.
  • stop: The end value of the range.
  • num (optional): The number of evenly spaced values to generate. The default is 50.
  • endpoint (optional): If True (default), stop is the last value in the range. If False, it’s not included.
  • retstep (optional): If True, return the step size between values as well.
  • dtype (optional): The data type of the output array. It’s typically inferred from the input arguments, but we can specify a different data type if needed.

Return:

This returns an array of evenly spaced values.

Examples of numpy.linspace() Function

Let us now look at some examples to demonstrate the use of numpy.linspace() function in Python. In each example, we have passed different parameter combinations to understand how this function works in different scenarios.

Example 1: By specifying start and stop

Here we will provide the parameters start which is the starting value of the range and stop which is the ending value of the range in numpy.linspace() function.

import numpy as np
np.linspace(1,10)

In the code above to use the linspace() function we first imported the Numpy library as np then we provided parameters start as 1 and stop as 10 in the numpy.linspace() function.

Output:

Example 1: By specifying the start and stop

In the output above we got an array of 50 values between 1 to 10 as by default num is 50.

Example 2: By specifying start, stop, and num

Here in the numpy.linspace() function with start and stop we will also provide num which is the number of evenly spaced values between the start and stop.

np.linspace(1,100,num=5)

In the code above we provided parameters start as 1, stop as 100, and num as 5 in the numpy.linspace() function which means it should be 5 evenly spaced values between 1 and 100.

Output:

Example 2: By specifying start, stop, and num

In the output above we got an array of 5 values between 1 to 100 as we have taken num as 5.

Example 3: By specifying start, stop, num, and endpoint

Here in the numpy.linspace() function with start, stop, and num we will also provide endpoint as False means stop will not be included in the range.

np.linspace(1,100,num=5,endpoint=False)

In the code above inside the numpy.linspace() function we provided parameters start as 1, stop as 100, num as 5, and the endpoint as False means stop has been exclusive in range.

Output:

Example 3: By specifying start, stop, num, and endpoint

In the output above we got an array of 5 values between 1 to 99 as we have mentioned endpoint as False which made stop to be exclusive in the range.

Example 4: By specifying start, stop, num, and retstep

Here in the numpy.linspace() function with start, stop, and num we will also provide retstep as True which will be responsible for returning the step size between the values in the range.

np.linspace(1,100,num=4,retstep=True)

In the code above inside the numpy.linspace() function we provided parameters start as 1, stop as 100, num as 4, and the retstep as True.

Output:

Example 4: By specifying start, stop, num, and retstep

In the output above we got an array of 4 values between 1 to 100 with the step size as 33.0 because we have mentioned retstep as True.

Example 5: By specifying start, stop, num, and dtype

Here in the numpy.linspace() function with start, stop, and num we will also provide dtype as int which will be responsible for defining the data type of our output array.

 np.linspace(1,100,num=4,dtype=int)

In the code above inside the numpy.linspace() function we provided parameters start as 1, stop as 100, num as 4, and the dtype as int means we have set the data type of values in the output array as an integer.

Output:

Example 5: By specifying start, stop, num, and dtype

In the output above we got an array of 4 values between 1 to 100 of datatype integer as we have mentioned dtype as int.

Difference Between numpy.linspace() and numpy.arange() Function

Firstly, in numpy.linspace() both the endpoints (start and stop) are inclusive but when we talk about numpy.arange() function, it excludes the upper end (stop) by default.

Secondly, when we talk about the floating point precision then numpy.linspace() will be preferable as compared to numpy.arange() as numpy.linspace() has more control on floating point values.

Lastly, if we talk about which one is easy to use then it will be slightly easier to use numpy.linspace() rather than numpy.arange() as in numpy.linspace() we don’t need to worry about the step sizes if we need a specific number of points within the two values but in numpy.arange() we have to pass the step size.

Both functions are good and they have their own importance but if we want inclusive endpoints and a specified number of points between two values then we can pick numpy.linspace() and if we don’t require a specified number of points between two values and want to give more control over the stepsize then we can go for numpy.arange() function.

Summary

In this tutorial, we have discussed numpy.linspace() function provided by Python’s NumPy library and also explored five examples to create an array of evenly spaced values using numpy.linspace() function. After reading this tutorial, we hope you can easily create an array of evenly-spaced values in Python.

Reference

https://stackoverflow.com/questions/66643615/python-numpy-linspace-function-for-bidimensional-array

Learn more

Priyanshu Singh
Priyanshu Singh
Articles: 44