In this article we will discuss how to create a Numpy array of evenly spaced samples over a range using numpy.linspace().

Numpy.linspace()

Python’s Numpy module provides a function to create a evenly spaced samples over a specified interval i.e.

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

Arguments:
start : It’s the start of Sequence / range.
stop : End Value of range, array doesn’t include this value but it’s an end marker
num : Numbers of samples to be generated. It’s optional, if not provided default value will be 50.
dtype : Data type of elements, if not provided will be deduced from other arguments.

It returns num number of evenly spaced samples over the range [start, stop)

To use Numpy in our code we need to include following module i.e.

import numpy as np

Checkout some examples,

Example 1:

Create 5 evenly spaced samples in interval [20, 60)

# Create 5 evenly spaced samples in interval [20, 60}
arr = np.linspace(20,60, 5)

Contents of the Numpy array containing 5 Samples are,
[20. 30. 40. 50. 60.]

Data type of elements in this Numpy array is float64.

As default type of elements are deduced automatically therefore in this case it was float. We can also specify the datatype by dtype argument i.e.

#Create 5 evenly spaced int samples in interval [20, 60}
arr = np.linspace(20, 60, 5 , dtype=np.int32)

Contents of the Numpy array containing 5 Samples are,
[20 30 40 50 60]

Data type of elements in this Numpy array is int.

Example 2:

Get the Step size from numpy.linspace()

If we pass the argument retstep=True in numpy.linspace() then it will return step size between samples too along with the Numpy array of samples i.e.

#Create evenly spaced samples in interval [20, 60} and also get the step size
arr , step = np.linspace(20, 60, 5, retstep=True)

print('Contents of the Numpy Array : ')
print(arr)
print('Step size between two elements  : ', step)

It returns a tuple of step size & Numpy Array i.e.
Contents of the Numpy Array : 
[20. 30. 40. 50. 60.]
Step size between two elements  :  10.0

Complete example is as follows,
import numpy as np


def main():

   print('*** Create numpy array of evenly spaced samples using numpy.linspace() ***')

   # Create 5 evenly spaced samples in interval [20, 60}
   arr = np.linspace(20,60, 5)

   print('Contents of the Numpy Array : ')
   print(arr)
   print(arr.dtype)

   #Create 5 evenly spaced int samples in interval [20, 60}
   arr = np.linspace(20, 60, 5 , dtype=np.int32)

   print('Contents of the Numpy Array : ')
   print(arr)
   print(arr.dtype)

   print('*** Get evenly spaced samples over an interval & Step size ***')

   #Create evenly spaced samples in interval [20, 60} and also get the step size
   arr , step = np.linspace(20, 60, 5, retstep=True)

   print('Contents of the Numpy Array : ')
   print(arr)
   print('Step size between two elements  : ', step)



if __name__ == '__main__':
   main()


Output:
*** Create numpy array of evenly spaced samples using numpy.linspace() ***
Contents of the Numpy Array : 
[20. 30. 40. 50. 60.]
float64
Contents of the Numpy Array : 
[20 30 40 50 60]
int32
*** Get evenly spaced samples over an interval & Step size ***
Contents of the Numpy Array : 
[20. 30. 40. 50. 60.]
Step size between two elements  :  10.0

 

Join a list of 2000+ Programmers for latest Tips & Tutorials