在numpy中,數組可能具有包含字段的數據類型,類似於電子表格中的列。一個例子是[(a, int), (b, float)]
,其中數組中的每個條目都是一對(int,float)。通常,這些屬性是使用字典查找(例如,arr['a'] and arr['b']
。記錄數組允許使用以下方式將字段作為數組的成員進行訪問arr.a and arr.b
。
numpy.recarray.ravel()函數返回返回連續的扁平記錄數組,即具有所有input-array元素且具有相同類型的一維數組。
用法: numpy.recarray.ravel([order])
參數:
order :['C','F','A','K',可選]
- “ C”表示以行優先的C-style順序索引元素,最後一個軸索引更改最快,回到第一個軸索引更改最快。
- “ F”表示以Fortran-style列為主的索引元素,第一個索引變化最快,最後一個索引變化最慢。
- 如果a在內存中是連續的Fortran,則“ A”表示以Fortran-like索引順序讀取元素,否則以C-like順序讀取元素。
- “ K”表示按順序在內存中讀取元素,但當步幅為負時反轉數據除外。
默認情況下,使用“ C”索引順序。
返回:[數組]展平的數組,其類型與Input數組相同,並且順序與選擇相同。
代碼1:
# Python program explaining
# numpy.recarray.ravel() 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.ravel methods
# to float record array
out_arr = rec_arr.a.ravel()
print ("Output flattenrd float array : ", out_arr)
# applying recarray.ravel methods
# to int record array
out_arr = rec_arr.b.ravel()
print ("Output flattenrd int array : ", out_arr)
輸出:
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 flattenrd float array : [ 5. 3. 6. 9. 5. -12.] Output flattenrd int array : [ 2 -4 9 1 4 -7]
代碼2:
我們正在申請numpy.recarray.ravel()
整個記錄數組。
# Python program explaining
# numpy.recarray.ravel() 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, -7)],
[(9.0, 1), (6.0, 4), (-2.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)
# applying recarray.ravel methods to record array
out_arr = rec_arr.ravel()
print ("Output array : ", out_arr)
輸出:
Input array : [[( 5., 2) ( 3., 4) ( 6., -7)] [( 9., 1) ( 6., 4) (-2., -7)]] Output array : [( 5., 2) ( 3., 4) ( 6., -7) ( 9., 1) ( 6., 4) (-2., -7)]
相關用法
- Python numpy.cov()用法及代碼示例
- Python Numpy MaskedArray.mean()用法及代碼示例
- Python numpy.copyto()用法及代碼示例
- Python numpy.nanmean()用法及代碼示例
- Python Numpy recarray.ptp()用法及代碼示例
- Python Numpy recarray.put()用法及代碼示例
- Python Numpy recarray.var()用法及代碼示例
- Python Numpy MaskedArray.any()用法及代碼示例
- Python numpy.issubdtype()用法及代碼示例
- Python Numpy recarray.all()用法及代碼示例
- Python Numpy recarray.any()用法及代碼示例
- Python Numpy recarray.dot()用法及代碼示例
- Python numpy.issctype()用法及代碼示例
- Python Numpy recarray.min()用法及代碼示例
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 Numpy recarray.ravel() function | Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。