numpy.recarray#
- class numpy.recarray(shape, dtype=None, buf=None, offset=0, strides=None, formats=None, names=None, titles=None, byteorder=None, aligned=False, order='C')[原始碼]#
建構一個允許使用屬性存取欄位的 ndarray。
陣列可能具有包含欄位的資料型別,類似於試算表中的欄。一個例子是
[(x, int), (y, float)]
,其中陣列中的每個條目都是一對(int, float)
。通常,這些屬性是使用字典查找來存取的,例如arr['x']
和arr['y']
。記錄陣列允許將欄位作為陣列的成員存取,使用arr.x
和arr.y
。- 參數:
- shapetuple
輸出陣列的形狀。
- dtypedata-type, optional
所需的資料型別。預設情況下,資料型別由 formats、names、titles、aligned 和 byteorder 決定。
- formatslist of data-types, optional
包含不同欄的資料型別的列表,例如
['i4', 'f8', 'i4']
。formats *不* 支援直接使用型別的新慣例,即(int, float, int)
。請注意,formats 必須是列表,而不是元組。 鑑於 formats 有些限制,我們建議改為指定dtype
。- namestuple of str, optional
每個欄的名稱,例如
('x', 'y', 'z')
。- bufbuffer, optional
預設情況下,會建立給定形狀和資料型別的新陣列。如果指定了 buf 並且是一個公開緩衝區介面的物件,則陣列將使用現有緩衝區中的記憶體。在這種情況下,可以使用 offset 和
strides
關鍵字。
- 返回:
- recrecarray
給定形狀和型別的空陣列。
- 其他參數:
- titlestuple of str, optional
欄名稱的別名。例如,如果 names 是
('x', 'y', 'z')
並且 titles 是('x_coordinate', 'y_coordinate', 'z_coordinate')
,則arr['x']
等同於arr.x
和arr.x_coordinate
。- byteorder{‘<’, ‘>’, ‘=’}, optional
所有欄位的位元組順序。
- alignedbool, optional
按照 C 編譯器的方式對齊記憶體中的欄位。
- stridestuple of ints, optional
緩衝區 (buf) 根據這些步幅進行解釋(步幅定義每個陣列元素、列、欄等在記憶體中佔用多少位元組)。
- offsetint, optional
從此偏移量開始讀取緩衝區 (buf)。
- order{‘C’, ‘F’}, optional
row-major (C-style) 或 column-major (Fortran-style) 順序。
參見
numpy.rec.fromrecords
從資料建構記錄陣列。
numpy.record
recarray
的基本資料型別。numpy.rec.format_parser
從格式、名稱、標題確定資料型別。
註解
此建構函式可以與
empty
進行比較:它建立一個新的記錄陣列,但不使用資料填充它。若要從資料建立記錄陣列,請使用以下方法之一建立標準 ndarray 並使用
arr.view(np.recarray)
將其轉換為記錄陣列使用 buf 關鍵字。
使用 np.rec.fromrecords。
範例
建立一個具有兩個欄位
x
和y
的陣列>>> import numpy as np >>> x = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', '<f8'), ('y', '<i8')]) >>> x array([(1., 2), (3., 4)], dtype=[('x', '<f8'), ('y', '<i8')])
>>> x['x'] array([1., 3.])
將陣列檢視為記錄陣列
>>> x = x.view(np.recarray)
>>> x.x array([1., 3.])
>>> x.y array([2, 4])
建立一個新的空記錄陣列
>>> np.recarray((2,), ... dtype=[('x', int), ('y', float), ('z', int)]) rec.array([(-1073741821, 1.2249118382103472e-301, 24547520), (3471280, 1.2134086255804012e-316, 0)], dtype=[('x', '<i4'), ('y', '<f8'), ('z', '<i4')])
- 屬性:
T
轉置陣列的檢視。
base
如果記憶體來自其他物件,則為基礎物件。
ctypes
一個簡化陣列與 ctypes 模組互動的物件。
data
指向陣列資料開始位置的 Python 緩衝區物件。
- device
dtype
陣列元素資料型別。
flags
有關陣列記憶體佈局的資訊。
flat
陣列上的 1-D 迭代器。
imag
陣列的虛部。
- itemset
itemsize
一個陣列元素的長度,以位元組為單位。
mT
矩陣轉置陣列的檢視。
nbytes
陣列元素消耗的總位元組數。
ndim
陣列維度數。
- newbyteorder
- ptp
real
陣列的實部。
shape
陣列維度的元組。
size
陣列中的元素數。
strides
遍歷陣列時,在每個維度中步進的位元組元組。
方法
all
([axis, out, keepdims, where])如果所有元素評估為 True,則返回 True。
any
([axis, out, keepdims, where])如果 a 的任何元素評估為 True,則返回 True。
argmax
([axis, out, keepdims])返回沿給定軸的最大值的索引。
argmin
([axis, out, keepdims])返回沿給定軸的最小值的索引。
argpartition
(kth[, axis, kind, order])返回將對此陣列進行分割的索引。
argsort
([axis, kind, order])返回將對此陣列進行排序的索引。
astype
(dtype[, order, casting, subok, copy])陣列的副本,強制轉換為指定的型別。
byteswap
([inplace])交換陣列元素的位元組
choose
(choices[, out, mode])使用索引陣列從一組選擇中建構一個新陣列。
clip
([min, max, out])返回一個值限制在
[min, max]
中的陣列。compress
(condition[, axis, out])返回沿給定軸的這個陣列的選定切片。
conj
()複數共軛所有元素。
返回逐元素的複數共軛。
copy
([order])返回陣列的副本。
cumprod
([axis, dtype, out])返回沿給定軸的元素的累積乘積。
cumsum
([axis, dtype, out])返回沿給定軸的元素的累積總和。
diagonal
([offset, axis1, axis2])返回指定的對角線。
dump
(file)將陣列的 pickle 傾印到指定檔案。
dumps
()將陣列的 pickle 作為字串返回。
fill
(value)用純量值填充陣列。
flatten
([order])返回摺疊成一維的陣列副本。
getfield
(dtype[, offset])將給定陣列的欄位作為特定型別返回。
item
(*args)將陣列的元素複製到標準 Python 純量並返回它。
max
([axis, out, keepdims, initial, where])返回沿給定軸的最大值。
mean
([axis, dtype, out, keepdims, where])返回沿給定軸的陣列元素的平均值。
min
([axis, out, keepdims, initial, where])返回沿給定軸的最小值。
nonzero
()返回非零元素的索引。
partition
(kth[, axis, kind, order])以這樣的方式部分排序陣列中的元素:第 k 個位置的元素的值位於排序陣列中它應在的位置。
prod
([axis, dtype, out, keepdims, initial, ...])返回陣列元素在給定軸上的乘積
put
(indices, values[, mode])為索引中的所有 n 設定
a.flat[n] = values[n]
。ravel
([order])返回扁平化陣列。
repeat
(repeats[, axis])重複陣列的元素。
reshape
(shape, /, *[, order, copy])返回一個包含相同資料但具有新形狀的陣列。
resize
(new_shape[, refcheck])就地更改陣列的形狀和大小。
round
([decimals, out])返回 a,其中每個元素都四捨五入到給定的小數位數。
searchsorted
(v[, side, sorter])尋找應將 v 的元素插入 a 中的索引以維持順序。
setfield
(val, dtype[, offset])將值放入資料型別定義的欄位中的指定位置。
setflags
([write, align, uic])分別設定陣列標誌 WRITEABLE、ALIGNED、WRITEBACKIFCOPY。
sort
([axis, kind, order])就地排序陣列。
squeeze
([axis])從 a 中移除長度為 1 的軸。
std
([axis, dtype, out, ddof, keepdims, where])返回沿給定軸的陣列元素的標準差。
sum
([axis, dtype, out, keepdims, initial, where])返回陣列元素在給定軸上的總和。
swapaxes
(axis1, axis2)返回軸 axis1 和 axis2 互換的陣列檢視。
take
(indices[, axis, out, mode])返回從給定索引處的 a 元素形成的陣列。
tobytes
([order])建構包含陣列中原始資料位元組的 Python 位元組。
tofile
(fid[, sep, format])將陣列作為文字或二進位寫入檔案(預設)。
tolist
()將陣列作為
a.ndim
層級深的 Python 純量巢狀列表返回。tostring
([order])與
tobytes
的相容性別名,行為完全相同。trace
([offset, axis1, axis2, dtype, out])返回沿陣列對角線的總和。
transpose
(*axes)返回軸轉置的陣列檢視。
var
([axis, dtype, out, ddof, keepdims, where])返回沿給定軸的陣列元素的變異數。
view
([dtype][, type])具有相同資料的陣列新檢視。
dot
field
to_device