How to Get Evenly Spaced Numbers Over a Specified Range in Python with Numpy



Python


In this article, we show how to get evenly spaced numbers over a specified range in Python with numpy.

Numpy has a built-in function called linspace that allows us to get evenly spaced numbers over 2 specified points.

This is a great tool if you want a wide range of points over a specified range. It could be a statistical measure or for plotting a graph, etc.

So how can this be done in Python with numpy?

The code below gets 10 evenly spaced numbers from 0 to 5.



So, first, we must import numpy as np.

We then create a variable named numbers and set it equal to, np.linspace(0,5,10)

This produces an array of 10 numbers including 0 and including 10.

We then create another variable, numbers2, that contains 11 points from 0 to 10.

Now let's say we want a lot of points over a specified range. Let's say that we want 100 data points of evenly spaced numbers from 0 to 10.

The code to do this is shown below.

So just as before, we import numpy as np.

We then create a variable named numbers and set it equal to, np.linspace(0,10,100)

What this line of code does is it gets 100 data points of evenly spaced numbers from 0 to 10.

The linspace() function is a great tool for data science and analysis in Python. It can give us a broad range of analytical tools so that we can great numerous data points for any given set of numbers.

And this is how we can obtain evenly spaced numbers over a specified range in Python using Numpy.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...