What is the logspace() function from NumPy in Python?
Overview
The logspace() function in Python is used to return numbers which are evenly spaced on the log scale.
Syntax
numpy.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0)
Required parameters
The logspace() function takes the following parameter values:
start: This represents the starting value of the sequence.stop: This represents the final value of the sequence, unless theendpointparameter isFalse.dtype: This represents the data type of the output array
Optional parameters
num: This represents the number of samples to generate.endpoint: This takes a boolean value. IfTrue, the value of thestopparameter is the last sample. Otherwise, it is not included. It isTrueby default.base: This represents the base of the log space.axis: This represents the axis in the result to store the samples. It is0by default.
Return value
The logspace() function returns number samples equally spaced on a log scale.
Example
import numpy as np# creating the arraymyarray = np.logspace(1, 10, num=10, endpoint = True, base = 2, dtype = float, axis = 0)print(myarray)
Explanation
- Line 1: We import the
numpylibrary. - Line 4: We create an array with samples that starts from
1, stops at10, with aTruevalue for its endpoint (thestopvalue is included), log base of2,floatdata type and a default value for theaxis. We assign the result to a variablemyarray. - Line 6: We print the array
myarray.