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
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
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 soughtafter 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.