Signup/Sign In

NumPy logspace() function

In this tutorial, we will cover numpy.logspace() function of the Numpy library.

The numpy.logspace() function in Numpy is used to create an array by using the numbers that are evenly separated on a log scale.

Syntax of numpy.logspace():

The syntax to use this function is as follows:

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

Parameters:

The parameters of this function are as follows:

  • start
    This parameter is used to represent the starting value of the interval in the base.

  • stop
    This parameter is used to represent the stopping value of the interval in the base.

  • num
    This parameter is used to indicate 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 is used to represent the base of the log space.

  • dtype
    This parameter is used to represent the data type of the array items.

Returned Values:

This function will return the array in the specified range.

Now it's time to look at a few examples in order to gain an understanding of this function.

Example 1:

Below we have the 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)


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 the example given below 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()

Output of the following code:

Numpy logspace() code example

Summary

In this tutorial, we covered 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 example.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.