在numpy中,數組可能具有包含字段的數據類型,類似於電子表格中的列。一個例子是[(a, int), (b, float)]
,其中數組中的每個條目都是一對(int,float)。通常,這些屬性是使用字典查找(例如,arr['a'] and arr['b']
。記錄數組允許使用以下方式將字段作為數組的成員進行訪問arr.a and arr.b
。
numpy.recarray.flatten()函數返回一維記錄數組。
用法: numpy.recarray.flatten(order='C')
參數:
order : [['C','F','A','K'],可選]
“ C”表示按行主(C-style)順序展平。
“ F”表示按列大(Fortran樣式)的順序展平。
“ A”表示如果a在內存中是連續的,則按列優先順序進行展平,否則按行優先進行展平。
“ K”表示按元素在內存中出現的順序展平。默認值為“ C”。
返回:輸入數組的副本,展平為一個維度。
代碼1:
# Python program explaining
# numpy.recarray.flatten() method
# importing numpy as geek
import numpy as geek
# creating input array with 2 different field
in_arr = geek.array([[(5.0, 2), (3.0, -4), (6.0, 9)],
[(9.0, 1), (5.0, 4), (-12.0, -7)]],
dtype =[('a', float), ('b', int)])
print ("Input array : ", in_arr)
# convert it to a record array,
# using arr.view(np.recarray)
rec_arr = in_arr.view(geek.recarray)
print("Record array of float: ", rec_arr.a)
print("Record array of int: ", rec_arr.b)
# applying recarray.flatten methods
# to float record in Fortan order
out_arr1 = rec_arr.a.flatten(order ='F')
print ("Output float flattened array in Fortan order: ", out_arr1)
# applying recarray.flatten methods
# to float record array in default order
out_arr2 = rec_arr.a.flatten()
print ("Output float flattenedarray in default order : ", out_arr2)
# applying recarray.flatten methods
# to int record in 'A' order
out_arr3 = rec_arr.b.flatten(order ='A')
print ("Output int flattened array in A order: ", out_arr3)
# applying recarray.flatten methods
# to float record array in 'K' order
out_arr4 = rec_arr.b.flatten(order ='K')
print ("Output intt flattened array in K order : ", out_arr4)
輸出:
Input array : [[( 5., 2) ( 3., -4) ( 6., 9)] [( 9., 1) ( 5., 4) (-12., -7)]] Record array of float: [[ 5. 3. 6.] [ 9. 5. -12.]] Record array of int: [[ 2 -4 9] [ 1 4 -7]] Output float flattened array in Fortan order: [ 5. 9. 3. 5. 6. -12.] Output float flattenedarray in default order : [ 5. 3. 6. 9. 5. -12.] Output int flattened array in A order: [ 2 -4 9 1 4 -7] Output intt flattened array in K order : [ 2 -4 9 1 4 -7]
相關用法
- Python numpy.cov()用法及代碼示例
- Python Numpy recarray.mean()用法及代碼示例
- Python Numpy MaskedArray.any()用法及代碼示例
- Python Numpy recarray.ptp()用法及代碼示例
- Python Numpy recarray.max()用法及代碼示例
- Python Numpy recarray.min()用法及代碼示例
- Python Numpy recarray.dot()用法及代碼示例
- Python Numpy MaskedArray.var()用法及代碼示例
- Python numpy.nanmean()用法及代碼示例
- Python Numpy MaskedArray.mean()用法及代碼示例
- Python Numpy recarray.any()用法及代碼示例
- Python numpy.issubsctype()用法及代碼示例
- Python Numpy recarray.all()用法及代碼示例
- Python numpy.issubdtype()用法及代碼示例
- Python Numpy MaskedArray.sum()用法及代碼示例
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 Numpy recarray.flatten() function | Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。