Reference link: numpy.linspace in Python
numpy.logspace:
Returns numbers evenly spaced on a logarithmic scale; that is, an array of geometric progressions can be created through the np.logspace method.
Specific usage:
np.logspace(start, stop, num=num, endpoint=endpoint, base=base, dtype=dtype)
Simple code example:
>>>np.logspace(2.0, 3.0, num=4)
array([ 100. , 215.443469 , 464.15888336, 1000. ])
#Code Interpretation: By default, using 10 as the base, generate 4 (num) arrays of powers from 2.0 to 3.0
>>>np.logspace(2.0, 3.0, num=4, endpoint=False)
array([ 100. , 177.827941 , 316.22776602, 562.34132519])
#Code Interpretation: endpoint=False means not calculating the value to the power of 3.0
>>>np.logspace(2.0, 3.0, num=4, base=2.0)
array([ 4. , 5.0396842 , 6.34960421, 8. ])
#Code Interpretation: base=2.0 means to calculate with a base of 2.0
Draw a simple graphic example:
Code:
>>> import matplotlib.pyplot as plt
>>> N = 10
>>> 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')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.plot(x2, y + 0.5, 'o')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.ylim ([- 0.5, 1])
(-0.5, 1)
>>> plt.show()
operation result:
numpy.linspace:
Returns the equally spaced samples with an interval of [start, stop]; that is, an arithmetic sequence array can be created through the numpy.linspace method.
specific method:
np.linspace(start, stop, num, endpoint, retstep, dtype)
Code example:
>>>np.linspace(2.0, 3.0, num=5)
array([ 2. , 2.25, 2.5 , 2.75, 3. ])
#Code Interpretation: Create an array of 5 numbers equally divided from 2.0 to 3.0.
>>> np.linspace(2.0, 3.0, num=5, endpoint=False)
array([ 2. , 2.2, 2.4, 2.6, 2.8])
#Code Interpretation: endpoint=False means that the ending 3.0 number is not included.
>>> np.linspace(2.0, 3.0, num=5, retstep=True)
(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
#Code Interpretation: retstep=True means show the final step result.
Drawing example:
Code part:
>>> import matplotlib.pyplot as plt
>>> N = 8
>>> y = np.zeros(N)
>>> x1 = np.linspace(0, 10, N, endpoint=True)
>>> x2 = np.linspace(0, 10, N, endpoint=False)
>>> plt.plot(x1, y, 'o')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.plot(x2, y + 0.5, 'o')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.ylim ([- 0.5, 1])
(-0.5, 1)
>>> plt.show()
The above is the detailed introduction of np.logspace and np.linspace!