numpy linspace demonstration

Using the numpy linspace() method

In this tutorial, we will look at the numpy linspace method with the help of some examples. We will also look at a range of different use-cases where you might need this method.

Numpy linspace function demonstration

The numpy linspace() function is used to create an array of equally spaced values between two numbers. The following is its syntax:

import numpy as np

# np.linspace with all the default paramters
arr = np.linsapce(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

# mostly you'll be only using these paramters
arr = np.linspace(start, stop, num)

It returns a numpy array of evenly spaced numbers over the specified interval with both the endpoints “start” and “stop” included.

  • You can exclude the “stop” endpoint by passing False to the endpoint parameter which is True by default.
  • It returns an array with 50 values. You can specifiy the number of values to generate by passing the desired number to the num paramter which is 50 by default.

Let’s look at some examples of using the linspace() function for a variety of use-cases:

Let’s create an array of equally spaced values between the numbers 1 and 10.

import numpy as np

# using numpy linspace
arr = np.linspace(1, 10)
# display the returned array
print(arr)

Output:

[ 1.          1.18367347  1.36734694  1.55102041  1.73469388  1.91836735
  2.10204082  2.28571429  2.46938776  2.65306122  2.83673469  3.02040816
  3.20408163  3.3877551   3.57142857  3.75510204  3.93877551  4.12244898
  4.30612245  4.48979592  4.67346939  4.85714286  5.04081633  5.2244898
  5.40816327  5.59183673  5.7755102   5.95918367  6.14285714  6.32653061
  6.51020408  6.69387755  6.87755102  7.06122449  7.24489796  7.42857143
  7.6122449   7.79591837  7.97959184  8.16326531  8.34693878  8.53061224
  8.71428571  8.89795918  9.08163265  9.26530612  9.44897959  9.63265306
  9.81632653 10.        ]

We get 50 equally spaced values between 1 and 10 (both inclusive). Let’s confirm the size and type of arr –

# size of arr
print(len(arr))
# type of arr
print(type(arr))

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

50
<class 'numpy.ndarray'>

The returned object is a numpy array of size 50. The size is 50 because the default value of the num parameter is 50.

Let’s now provide a custom value for the parameter num. For example, let’s create 10 equally spaced values between 2 and 20

# using numpy linspace
arr = np.linspace(2, 20, 10)
# display the returned array
print(arr)

Output:

[ 2.  4.  6.  8. 10. 12. 14. 16. 18. 20.]

We get 10 numbers that are equally spaced between 2 and 20.
The syntax np.linspace(start, stop, num) will be the one that you might end up using the most.

By default, the np.linspace() function includes both the start and the stop endpoints in the generated array. If you don’t want the stop endpoint to be included, pass False to the endpoint parameter. For example, let’s create 10 equally spaced values between 2 and 20 with 20 not included.

# using numpy linspace 
arr = np.linspace(2, 20, 10, endpoint=False)
# display the returned array
print(arr)

Output:

[ 2.   3.8  5.6  7.4  9.2 11.  12.8 14.6 16.4 18.2]

In the output, you can see that we get 10 values but 20 is not included. The step size is different for the same start and stop values as in the previous example and is calculated using (stop-start)/(num+1) which comes out to be around 1.8 in this example. Note that this way of step size calculation is used only when endpoint=False. That is when the stop endpoint is not included.

If you want to get values between two numbers starting from the largest, pass the larger number as the start value and the smaller number as the end value. For example, let’s get 10 equally spaced values between 2 and 20 but in the reverse order.

# using numpy linspace in reverse order
arr = np.linspace(20, 2, 10)
# display the returned array
print(arr)

Output:

[20. 18. 16. 14. 12. 10.  8.  6.  4.  2.]

Here we passed 20 as the start value and 2 as the end value. You can see that we get 10 equally spaced values between 2 and 20 in the reverse order.

You can also apply the linspace() function on negative values. For example, let’s get 5 equally spaced values between -10 and -2.

# using numpy linspace on negative values
arr = np.linspace(-10, -2, 5)
# display the returned array
print(arr)

Output:

[-10.  -8.  -6.  -4.  -2.]

The function works similarly as it did for positive numbers. We can see that we get 5 equally spaced values between -10 and -2.

We can also get values between a range of negative and positive numbers. For example, let’s get 5 equally spaced values between -10 and 10.

# using numpy linspace
arr = np.linspace(-10, 10, 5)
# display the returned array
print(arr)

Output:

[-10.  -5.   0.   5.  10.]

For more on the numpy linspace() function, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top