How to Start Using Numpy logspace() Method in Python

What is numpy logspace?

The Numpy logspace() function returns or creates the array by using the evenly separated numbers on a log scale. In the linear space, the sequence basically starts at base ** start and ends at base ** stop.

Syntax

np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)

Parameters

  • Start: It is used to tell the starting value of the interval in the base.
  • Stop: It is used to tell the ending value of the interval in the base.
  • num: It is an integer value that is optional in the input. It is used to tell how many samples to generate. By default, the value is set to 50.
  • endpoint: It is a boolean value that is optional in the input. If we set the Boolean value to True, then the stop is the last sample. Otherwise, we don’t include it. By default, it is always True.
  • base: It is also optional in the input. It tells about the base of Logspace. By default, it is equal to 10.0.
  • dtype: It is the type of output array. The dtype is inferred from the start and stop if it is not given.
  • axis: It is the integer value that is optional in the input. It is the result of storing the samples.

Return value of numpy logspace()

An ndarray with the specified range equally spaced on a log scale.

Examples of numpy logspace()

Let us understand the concept of the numpy logspace() function in detail with the help of examples:

1. Using start, Stop and num parameter

In this example, we will import the numpy library to use the logspace() function and use the start value, stop value, and num parameter to return the spaced log scale evenly. Let’s see the output with these parameters:

#import numpy library
import numpy as np

Output = np.logspace(2.0, 3.0, num=5)
print("output array : ",Output)

Output:

output array :  [ 100.          177.827941    316.22776602  562.34132519 1000.        ]

Explanation:

Here, firstly, we have imported the numpy module as np. Secondly, we have applied the logspace() function with start, stop, and num parameters. At last, we printed the program output. Hence, we can see the result as an array that is generated.

2. Using base parameter

In this example, we will import the numpy library to use the logspace() function and use the start value, stop value, base, and num parameters to return the spaced log scale evenly. Let’s see the output with these parameters:

#import numpy library
import numpy as np

Output = np.logspace(2.0, 3.0, num=5, base = 30)
print("output array : ",Output)

Output:

output array :  [  900.          2106.31258739  4929.50301755 11536.7491727
 27000.        ]

Explanation:

Here, firstly, we have imported the numpy module as np. Secondly, we have applied the logspace() function with start, stop, base, and num parameters. At last, we printed the program output. Hence, we can see the result as an array that is generated.

3. Using dtype parameter

In this example, we will import the numpy library to use the logspace() function and use the start value, stop value, type, and num parameter to return the spaced log scale evenly. Let’s see the output with these parameters:

#import numpy library
import numpy as np

Output = np.logspace(2.0, 3.0, num=5, dtype=int)
print("output array : ",Output)

Output:

output array :  [ 100  177  316  562 1000]

Explanation:

Here, firstly, we have imported the numpy module as np. Secondly, we applied the logspace() function with start, stop, type, and number parameters. In this dtype parameter will print the array with the integer value. At last, we printed the program output. Hence, we can see the result as an array that is generated.

4. Using endpoint parameter

In this example, we will import the numpy library to use the logspace() function and use the start value, stop value, endpoint, type, and num parameter to return the spaced log scale evenly. Let’s see the output with these parameters:

#import numpy library
import numpy as np

Output = np.logspace(2.0, 3.0, num=5, endpoint = False,dtype=int)
print("output array : ",Output)
print("\n")
Output = np.logspace(2.0, 3.0, num=5, endpoint = True,dtype=int)
print("output array : ",Output)

Output:

output array :  [100 158 251 398 630]


output array :  [ 100  177  316  562 1000]

Explanation:

Here, firstly, we have imported the numpy module as np. Secondly, we applied the logspace() function with start, stop, type, and number parameters. Thirdly, we have taken the endpoint parameter, which is set to False and True, both the boolean values, so that you can see the change in the output and understand the concept easily. At last, we printed the program output. Hence, we can see the result as an array that is generated.

5. Using all the parameters

In this example, we will import the numpy library to use the logspace() function and use the start value, stop value, endpoint, dtype, base, axis, and the num parameter to return the spaced log scale evenly. Let’s see the output with these parameters:

#import numpy library
import numpy as np

Output = np.logspace(2.0,3.0,num=5,base=30,endpoint=False,dtype=int,axis=0)
print("output array : ",Output)

Output:

output array :  [  900  1776  3508  6926 13675]

Explanation:

Here, firstly, we have imported the numpy module as np. Secondly, we have applied the logspace() function with all the parameters such as start, stop, dtype, endpoint, axis, base, and num parameters. The parameters’ operation can be seen in the parameter section. At last, we printed the program output. Hence, we can see the result as an array that is generated.

NOTE: All the input is kept the same so that you understand the concept more precisely.

Graphical Representation of numpy logspace

In this example, we will use the matplotlib module, which will help you understand the numpy logspace() function.

#import numpy and matplotlib library
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.5, 'o')
plt.ylim([-0.6, 1])
plt.show()

Output:

Graphical Representation of numpy logspace

Explanation:

Here, firstly, we have imported the numpy library as np. Secondly, we have imported the matplotlib.pyplot library as plt. Thirdly, we have taken a variable n, which is set to 20. Fourthly, we have taken the x1 and x2 variables in which we have applied the logspace() function with some parameters. Fifthly, we have a variable y which np.zeros() function.

Then, we applied the plt library in which x1 and x2 are the x-axis and y is the y-axis for both the color graph. plt.ylim() function sets the y-limits of the current axis. At last, we have called plt.show () which will generate the graph, and you can see the output. Hence, we can see the result as a graph generated.

NOTE: This program will not be able to run on online compiler so try to run it in spyder or any other compiler.

Conclusion

In this tutorial, we learned how to use the numpy logspace() function. We have explained the concept in detail by explaining all its examples. We have also shown the working and diagrammatic representation of the numpy logspace() method in the matplotlib library.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments