numpy.linspace() | Create same sized samples over an interval in Python

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,

Advertisements

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

 

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top