在 NumPy 中指定區間內返回等間距的數字並設定要生成的樣本數
要在指定區間內返回等間距的數字,請在 Python NumPy 中使用 **numpy.linspace()** 方法。第一個引數是“**start**”,即序列的起始位置。第二個引數是“**end**”,即序列的結束位置。第三個引數是 **num**,即要生成的樣本數。
除非將 endpoint 設定為 False,否則 stop 是序列的結束值。在這種情況下,序列包含除 num + 1 個等間距樣本中的最後一個樣本之外的所有樣本,因此 stop 將被排除。請注意,當 endpoint 為 False 時,步長會發生變化。
dtype 是輸出陣列的型別。如果未給出 dtype,則從 start 和 stop 推斷資料型別。推斷出的 dtype 永遠不會是整數;即使引數會生成一個整數陣列,也會選擇浮點數。結果中儲存樣本的軸。僅當 start 或 stop 為陣列狀時才相關。預設情況下 (0),樣本將沿著在開頭插入的新軸。使用 -1 在末尾獲取軸。
步驟
首先,匯入所需的庫 -
import numpy as np
要在指定區間內返回等間距的數字,請在 Python NumPy 中使用 numpy.linspace() 方法 -
arr = np.linspace(100, 200, 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 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.
arr = np.linspace(100, 200, 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... [100. 111.11111111 122.22222222 133.33333333 144.44444444 155.55555556 166.66666667 177.77777778 188.88888889 200. ] Type... float64 Dimensions... 1 Shape... (10,) Number of elements... 10
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP