在對數刻度上返回等間距的數字,並設定在Numpy中生成的樣本數量


要在對數刻度上返回等間距的數字,請使用 Python Numpy 中的 **numpy.logspace()** 方法。第一個引數是“**start**”,即序列的起始值。第二個引數是“**end**”,即序列的結束值。第三個引數是 num,即要生成的樣本數量。預設為 50。

線上性空間中,序列從基數 **start**(基數的start次方) 開始,以基數 **stop**(參見下面的endpoint) 結束。start 是基數的start次方,是序列的起始值。stop 是基數的stop次方,是序列的最終值,除非 endpoint 為 False。在這種情況下,num + 1 個值在對數空間的區間內均勻分佈,其中除了最後一個值之外的所有值都會返回。對數空間的基數。ln(samples) / ln(base)(或 log_base(samples))中元素之間的步長是均勻的。預設為 10.0。

結果中用於儲存樣本的軸。僅當 start 或 stop 為陣列狀時才相關。預設情況下 (0),樣本將沿著在開頭插入的新軸排列。使用 -1 可在末尾獲得一個軸。

步驟

首先,匯入所需的庫:

import numpy as np

要在對數刻度上返回等間距的數字,請使用 numpy.logspace() 方法:

arr = np.logspace(100.0, 200.0, num = 10)
print("Array...
", arr)

獲取陣列型別:

print("
Type...
", arr.dtype)

獲取陣列的維度:

print("
Dimensions...
",arr.ndim)

獲取陣列的形狀:

print("
Shape...
",arr.shape)

獲取元素數量:

print("
Number of elements...
",arr.size)

示例

import numpy as np

# To return evenly spaced numbers on a log scale, use the numpy.logspace() method in Python Numpy
# The 1st parameter is the "start" i.e. the start of the sequence
# The 2nd parameter is the "end" i.e. the end of the sequence
# The 3rd parameter is the num i.e the number of samples to generate. Default is 50.
arr = np.logspace(100.0, 200.0, num = 10)
print("Array...
", arr) # Get the array type print("
Type...
", arr.dtype) # Get the dimensions of the Array print("
Dimensions...
",arr.ndim) # Get the shape of the Array print("
Shape...
",arr.shape) # Get the number of elements print("
Number of elements...
",arr.size)

輸出

Array...
[1.00000000e+100 1.29154967e+111 1.66810054e+122 2.15443469e+133
2.78255940e+144 3.59381366e+155 4.64158883e+166 5.99484250e+177
7.74263683e+188 1.00000000e+200]

Type...
float64

Dimensions...
1

Shape...
(10,)

Number of elements...
10

更新於:2022年2月16日

1K+ 瀏覽量

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告