NumPy Tutorial NumPy Statistics NumPy References

NumPy - logspace() function



The NumPy logspace() function returns numbers spaced evenly on a log scale. The values are generated in the range [base ** start, base ** stop] with specified number of samples. The endpoint of the interval can optionally be excluded.

Syntax

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

Parameters

start Required. Specify the starting value of the sequence. The starting value is base ** start.
stop Required. Specify the end value of the sequence (end value is base ** end), unless endpoint is False. In that case, num + 1 values are spaced over the interval in log-space, of which all but the last (a sequence of length num) are returned.
num Optional. Specify the number of samples to generate. Default is 50. Must be non-negative.
endpoint Optional. Specify boolean value. If True, stop is the last sample. Otherwise, it is not included. Default is True.
base Optional. Specify base of the log space. The step size between the elements in ln(samples) / ln(base) (or log_base(samples)) is uniform. Default is 10.0.
dtype Optional. Specify type of the output array. If dtype is not given, infer the data type from the other input arguments.

Return Value

Returns an array with elements within the specified range.

Example: creating array using logspace() function

In the example below, logspace() function is used with different parameters to create arrays.

import numpy as np

#creating 5 samples points in the given range
Arr1 = np.logspace(1,2,num=5)
print("Arr1 is:", Arr1)

#when endpoint=False, (num+1) samples are  
#generated and returns samples without last 
Arr2 = np.logspace(1,2,num=5,endpoint=False)
print("Arr2 is:", Arr2)

#using base=2.0
Arr3 = np.logspace(1,2,num=5,base=2.0)
print("Arr3 is:", Arr3)

The output of the above code will be:

Arr1 is: [10.     17.7827941   31.6227766   56.23413252  100.       ]
Arr2 is: [10.     15.84893192  25.11886432  39.81071706  63.09573445]
Arr3 is: [2.      2.37841423   2.82842712   3.36358566   4.         ]

Example: Visualize the sample

In the example below, arrays are created by taking endpoint parameter as True and False, and plotted using matplolib library to visualize the result.

import numpy as np
import matplotlib.pyplot as plt

#creating 10 samples points using endpoint=True
Arr1 = np.logspace(1,2,num=10,endpoint=True)

#creating 10 samples using endpoint=True
Arr2 = np.logspace(1,2,num=10,endpoint=False)

y = np.zeros(10)

#plotting Arr1 and Arr2
plt.plot(Arr1, y+0.3, 'o')
plt.plot(Arr2, y+0.6, 'o')
plt.ylim([0,1])
plt.legend(labels = ('Arr1', 'Arr2'))
plt.show()

The output of the above code will be:

Numpy logspace

❮ NumPy - Functions

5