NumPy logspace() Function

NumPy logspace() Function

This tutorial will discuss the linspace() and logspace() functions in NumPy.

Difference Between numpy.linspace() and numpy.logspace() Functions

The numpy.linspace() function is used to get evenly separated numerical values within a specified limit. The numpy.linspace(l, u, n) function takes the lower limit l, the upper limit u, and the number of values to return n as input parameters and returns n number of numerical values within the upper and lower limit evenly separated from each other.

On the other hand, the numpy.logspace() function is used to get the logarithm of evenly separated numerical values within a specified limit. The numpy.logspace(l, u, n) function also takes the lower limit l, the upper limit u, and the number of values to return n as input parameters and returns n number of logarithm values within the upper and lower limit evenly separated from each other.

The following code example illustrates the fundamental difference between the working of numpy.linspace() and numpy.logspace() functions in Python.

import numpy as np

linespace = np.linspace(0, 1, 10)
logspace = np.logspace(0, 1, 10)

print(linespace)
print(logspace)

Output:

[0.         0.11111111 0.22222222 0.33333333 0.44444444 0.55555556
 0.66666667 0.77777778 0.88888889 1.        ]
[ 1.          1.29154967  1.66810054  2.15443469  2.7825594   3.59381366
  4.64158883  5.9948425   7.74263683 10.        ]

We calculated ten evenly separated numerical values and ten evenly separated logarithm values between 0 and 1 with the linspace() and logspace() functions in NumPy. The result of the np.linspace() function is stored in the linespace, whereas the result of the np.logspace() function is stored inside the logspace. The linespace contains evenly separated numerical values between 0 and 1 like 0, 1.1, 2.2, and so on. The logspace contains the logarithm of these evenly separated numerical values like 1: logarithm of 0, 1.29, the logarithm of 0.11, and so on.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn