在 NumPy 中返回指定區間內的等間距數字,且不設定終點
要返回指定區間內的等間距數字,請在 Python NumPy 中使用 **numpy.linspace()** 方法。第一個引數是“start”,即序列的起點。第二個引數是“end”,即序列的終點。
除非 endpoint 設定為 False,否則 stop 是序列的結束值。在這種情況下,序列包含除最後一個 num + 1 個等間距樣本之外的所有樣本,因此 stop 被排除在外。請注意,當 endpoint 為 False 時,步長會發生變化。
dtype 是輸出陣列的型別。如果未給出 dtype,則從 start 和 stop 推斷資料型別。推斷出的 dtype 永遠不會是整數;即使引數會生成一個整數陣列,也會選擇浮點數。結果中儲存樣本的軸。僅當 start 或 stop 為陣列狀時才相關。預設情況下 (0),樣本將沿著插入到開頭的新軸。使用 -1 在末尾獲取軸。
步驟
首先,匯入所需的庫 -
import numpy as np
使用 numpy.linspace() 方法返回指定區間內的等間距數字。第一個引數是“start”,即序列的起點。第二個引數是“end”,即序列的終點。第三個引數是“num”,即要生成的樣本數。預設為 50。第四個引數是“endpoint”。如果為 True,則 stop 是最後一個樣本。否則,它不包括在內。預設為 True -
arr = np.linspace(100, 200, num = 10, endpoint = False)
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 over a specified interval, use the numpy.linspace() 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.
# The 4th parameter is the "endpoint". If True, stop is the last sample. Otherwise, it is not included. Default is True.
arr = np.linspace(100, 200, num = 10, endpoint = False)
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... [100. 110. 120. 130. 140. 150. 160. 170. 180. 190.] Type... float64 Dimensions... 1 Shape... (10,) Number of elements... 10
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP