Numpy Logspace Function

NumPyNumPyBeginner
Practice Now

Introduction

In this lab, you will learn about the numpy.logspace() function of the Numpy library. This function is used to create an array by using the numbers that are evenly separated on a logarithmic scale.

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.

Syntax

The syntax to use this function is as follows:

numpy.logspace(start, stop, num, endpoint, base, dtype)

Parameters:

  • start: This parameter represents the starting value of the interval in the base.
  • stop: This parameter represents the stopping value of the interval in the base.
  • num: This parameter indicates the number of values between the range.
  • endpoint: This parameter's value is in boolean and it is used to make the value represented by stop as the last value of the interval.
  • base: This parameter represents the base of the log space.
  • dtype: This parameter represents the data type of the array items.

Returned Values:

This function will return the array in the specified range.

Example 1

Here is an example of code snippet where we will use this function:

import numpy as np

arr = np.logspace(20, 30, num=7, base=4, endpoint=True)
print("The array over the given range is ")
print(arr)

Output:

The array over the given range is
[1.09951163e+12 1.10823828e+13 1.11703419e+14 1.12589991e+15
    1.13483599e+16 1.14384301e+17 1.15292150e+18]

Example 2

In this example, we will cover the graphical representation of numpy.logspace() function using matplotlib:

import numpy as np
import matplotlib.pyplot as plt

N = 20
x1 = np.logspace(0.1, 1, N, endpoint=True)
x2 = np.logspace(0.1, 1, N, endpoint=False)
y = np.zeros(N)

plt.plot(x1, y, 'o')
plt.plot(x2, y + 0.8, 'o')
plt.ylim([-0.5, 1])
plt.show()

Summary

In this lab, we covered the numpy.logspace() function of the Numpy library. We learned its syntax, parameters as well as the value returned by this function along with multiple code examples.

Other NumPy Tutorials you may like