Generating Evenly Spaced Numbers with NumPy

PythonPythonBeginner
Practice Now

Introduction

In this tutorial, we will cover the usage of the numpy.linspace() function in the NumPy library. This function is used to generate evenly spaced numbers over a specified interval. It is similar to the numpy.arange() function with the only difference being that instead of a step size, the number of evenly spaced values between the interval is specified using the num argument.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Understanding the Syntax

The syntax of the numpy.linspace() function is as follows:

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

The function takes in various parameters:

  • start: The starting value of the interval.
  • stop: The ending value of the interval.
  • num: The number of evenly spaced samples over the interval to be generated. The default value is 50.
  • endpoint: A boolean value that indicates whether or not to include the endpoint of the interval. If True, then the endpoint is included. False, then it is excluded. The default value is True.
  • retstep: A boolean value that indicates whether or not to return the step value i.e. the spacing between the consecutive numbers. The default value is False.
  • dtype: The data type of the array items.

Generating an Array using linspace()

To generate an array using numpy.linspace(), you just need to pass in the start, stop and number of samples you want. Here's an example:

import numpy as np

a = np.linspace(0, 10, num=11)
print(a)

The output of this code will be:

[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]

Excluding the Endpoint

To exclude the endpoint, we simply set the endpoint parameter to False. Here's an example:

b = np.linspace(0, 10, num=10, endpoint=False)
print(b)

The output of this code will be:

[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]

Returning the Step Value

To return the step or spacing value between the consecutive numbers generated by linspace(), set the retstep parameter to True. Here's an example:

c, step = np.linspace(0, 10, num=11, retstep=True)
print(c)
print(step)

The output of this code will be:

[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]
1.0

Plotting using Linspace()

We can use linspace() together with matplotlib to plot graphs. Here's an example:

import matplotlib.pyplot as plt

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

plt.plot(x, y)
plt.show()

The output of this code is a simple sine wave plot.

Using Non-scalar Values

Starting in the newer versions of NumPy, linspace() supports non-scalar values of start and end parameters. Here's an example:

start = np.array([0, 1, 2])
end = np.array([6, 7, 8])
d = np.linspace(start, end, num=5, axis=1)
print(d)

The output of this code is:

[[0.  1.5 3.  4.5 6. ]
 [1.  2.5 4.  5.5 7. ]
 [2.  3.5 5.  6.5 8. ]]

Summary

In this tutorial, we have demonstrated the use of numpy.linspace() function in the NumPy library. We covered the syntax, the various parameters, and various examples. With this knowledge, you can now generate evenly spaced arrays quickly and efficiently.

Other Python Tutorials you may like