如何使用Python使用对数刻度创建均匀间隔的数字列表?
在本文中,我们将使用对数刻度创建一个均匀间隔数字的列表。这意味着在对数尺度上,两个相邻样本之间的差异是相同的。可以使用Python Numpy 库中的两个不同函数来实现该目标。
使用的功能:
- numpy.logspace:此函数返回在对数刻度上均匀缩放的数字。
Parameters:
- start: Starting value of sequence is base**start
- stop: If endpoint is True then ending value of sequence is base**stop
- num (Optional): Specifies the number of samples to generate
- endpoint (Optional): It can either be true or false with default value true
- base (Optional): Specifies the base of log sequence. Default value is 10.
- dtype (Optional): Specifies the type of output array
- axis (Optional): The axis in the result to store the samples.
Return: It returns array of samples equally spaced on log scale.
- numpy.geomspace:这个函数类似于logspace函数,只是直接指定了端点。在输出样本中,每个输出都是通过将先前的输出乘以相同的常数来获得的。
Parameters:
start: It is the starting value of sequence
stop: If endpoint is True then it is the ending value of sequence
num (Optional): Specifies the number of samples to generate
endpoint (Optional): It can either be true or false with default value true
dtype (Optional): Specifies the type of output array
axis (Optional): The axis in the result to store the samples.
Return: It returns array of samples equally spaced on log scale.
示例 1:此示例使用 logspace函数。在这个例子中,start 传递为 1,stop 传递为 3,基数为 10。所以序列的起点将是 10**1 = 10,序列的终点将是 10**3 = 1000。
Python3
# importing the library
import numpy as np
import matplotlib.pyplot as plt
# Initializing variable
y = np.ones(10)
# Calculating result
res = np.logspace(1, 3, 10, endpoint = True)
# Printing the result
print(res)
# Plotting the graph
plt.scatter(res, y, color = 'green')
plt.title('logarithmically spaced numbers')
plt.show()
Python3
# importing the library
import numpy as np
import matplotlib.pyplot as plt
# Initializing variable
y = np.ones(10)
# Calculating result
res = np.geomspace(10, 1000, 10, endpoint = True)
# Printing the result
print(res)
# Plotting the graph
plt.scatter(res, y, color = 'green')
plt.title('logarithmically spaced numbers')
plt.show()
Python3
# importing the library
import numpy as np
import matplotlib.pyplot as plt
# Initializing variable
y = np.ones(10)
# Calculating result
res = np.logspace(1, 3, 10, endpoint = False)
# Printing the result
print(res)
输出:
示例 2:此示例使用 geomspace函数生成与前一个示例相同的列表。这里我们直接通过10和1000作为起点和终点
蟒蛇3
# importing the library
import numpy as np
import matplotlib.pyplot as plt
# Initializing variable
y = np.ones(10)
# Calculating result
res = np.geomspace(10, 1000, 10, endpoint = True)
# Printing the result
print(res)
# Plotting the graph
plt.scatter(res, y, color = 'green')
plt.title('logarithmically spaced numbers')
plt.show()
输出:
示例 3:在此示例中,端点设置为 false,因此它将生成 n+1 个样本并仅返回前 n 个样本,即停止将不包含在序列中。
蟒蛇3
# importing the library
import numpy as np
import matplotlib.pyplot as plt
# Initializing variable
y = np.ones(10)
# Calculating result
res = np.logspace(1, 3, 10, endpoint = False)
# Printing the result
print(res)
输出:
[ 10. 15.84893192 25.11886432 39.81071706 63.09573445
100. 158.48931925 251.18864315 398.10717055 630.95734448]