Skip to main content
Please wait...
Understanding The Numpy linspace Function
18 Apr, 2025

Understanding The Numpy linspace Function

The numpy.linspace function is a versatile tool that generates an array of equally spaced values over a specified range. This is particularly useful for creating grids or sampling points in numerical computations.

Input Arguments of numpy.linspace 

  • start: The starting value of the sequence. 

  • stop: The end value of the sequence. 

  • num: The number of samples to generate (default is 50). 

  • endpoint: If True (default), stop is the last sample. Otherwise, it is not included. 

  • retstep: If True, return (samples, step), where step is the spacing between samples. 

  • dtype: The type of the output array. 

  • axis: The axis in the result array along which the samples are stored (default is 0). 

Code Example 

Here's a simple example of using numpy.linspace to generate N linearly spaced points between the start and stop parameters: 

import numpy as np 
 
# Generate 20 linearly spaced points between 0 and 5 
array = np.linspace(0, 5, 20) 
 
print(array) 

This will output: 

[0.         0.26315789 0.52631579 0.78947368 1.05263158 1.31578947 
 1.57894737 1.84210526 2.10526316 2.36842105 2.63157895 2.89473684 
 3.15789474 3.42105263 3.68421053 3.94736842 4.21052632 4.47368421 
 4.73684211 5.        ]     

 

Effect of endpoint=False 

In the previous example, we notice the generated array includes the endpoint. That is because the default value of the endpoint parameter is true. 

If the endpoint parameter is set to False, the stop value is not included in the generated array. Instead, the array will contain values that are evenly spaced up to, but not including, the stop value. 

Here's an example: 

import numpy as np 
 
# Generate 20 linearly spaced points between 0 and 5, excluding the endpoint 
array = np.linspace(0, 5, 20, endpoint=False) 
print(array) 

This will output: 

[0.   0.25 0.5  0.75 1.   1.25 1.5  1.75 2.   2.25 2.5  2.75 3.   3.25 
 3.5  3.75 4.   4.25 4.5  4.75]

 

Example in an Inference Algorithm

Now, let's see how numpy.linspace can be used within an inference algorithm to create a uniform sampling step size: 

import numpy as np 
 
# Define the function to be sampled 
def func(x): 
    return np.sin(x) 
 
# Create an array of 100 evenly spaced values from 0 to 2π 
x_values = np.linspace(0, 2 * np.pi, 100) 
 
# Sample the function at these points 
y_values = func(x_values) 
 
# Print the sampled values 
print(y_values) 

In this example: 

  • np.linspace(0, 2 * \pi, 100) generates 100 evenly spaced values between 0 and (2\pi). 

  • The func function is defined to be the sine function. 

  • y_values contains the sine values at each of the 100 points. 

 

This approach ensures a uniform sampling step size, which is useful for various inference algorithms, such as numerical integration or interpolation. 

References 

[1] Lopez Corzo, Edward Felipe. "Development and evaluation of a calculation tool for prediction of condensation behaviour in vertical tubes in the presence of non-condensable components." (2020).  [2] Gong, Dayoung, Suha Kwak, and Minsu Cho. "ActFusion: a Unified Diffusion Model for Action Segmentation and Anticipation." Advances in Neural Information Processing Systems 37 (2024): 89913-89942.  [3] Asadi, Farzin. Essential Circuit Analysis Using NI MultisimTM and MATLAB®. Springer Nature, 2022.  [4] Balestriero, Randall. "Symjax: symbolic cpu/gpu/tpu programming." arXiv preprint arXiv:2005.10635 (2020).