Python 将行追加到NumPy记录数组

Python 将行追加到NumPy记录数组,python,numpy,Python,Numpy,是否有方法将行附加到NumPy rec.array()中?比如说, x1=np.array([1,2,3,4]) x2=np.array(['a','dd','xyz','12']) x3=np.array([1.1,2,3,4]) r = np.core.records.fromarrays([x1,x2,x3],names='a,b,c') append(r,(5,'cc',43.0),axis=0) 最简单的方法是将所有列提取为nd.array()类型,向每个列添加单独的元素,然后重新

是否有方法将行附加到NumPy rec.array()中?比如说,

x1=np.array([1,2,3,4])
x2=np.array(['a','dd','xyz','12'])
x3=np.array([1.1,2,3,4])
r = np.core.records.fromarrays([x1,x2,x3],names='a,b,c')

append(r,(5,'cc',43.0),axis=0)
最简单的方法是将所有列提取为nd.array()类型,向每个列添加单独的元素,然后重新生成rec.array()。不幸的是,这种方法的内存效率很低。是否有其他方法可以在不分离rec.array()的情况下执行此操作

干杯

伊莱


但它仍然会分割,这次是按行分割。也许更好?

您可以就地调整numpy阵列的大小。这比先转换为列表然后再转换回numpy数组要快,而且占用的内存也更少

print (r.shape)
# (4,)
r.resize(5)   
print (r.shape)
# (5,)
r[-1] = (5,'cc',43.0)
print(r)

# [(1, 'a', 1.1000000000000001) 
#  (2, 'dd', 2.0) 
#  (3, 'xyz', 3.0) 
#  (4, '12', 4.0)
#  (5, 'cc', 43.0)]

如果内存不足,无法就地扩展阵列,则调整大小(或追加)操作可能会强制NumPy为全新阵列分配空间,并将旧数据复制到新位置。当然,这相当慢,因此如果可能的话,您应该尽量避免使用
调整大小
追加
。相反,从一开始就预先分配足够大小的数组(即使比最终需要的要大一些)。

扩展@unutbu的答案,我发布了一个更通用的函数,附加任意数量的行:

def append_rows(arrayIN, NewRows):
    """Append rows to numpy recarray.

    Arguments:
      arrayIN: a numpy recarray that should be expanded
      NewRows: list of tuples with the same shape as `arrayIN`

    Idea: Resize recarray in-place if possible.
    (only for small arrays reasonable)

    >>> arrayIN = np.array([(1, 'a', 1.1), (2, 'dd', 2.0), (3, 'x', 3.0)],
                           dtype=[('a', '<i4'), ('b', '|S3'), ('c', '<f8')])
    >>> NewRows = [(4, '12', 4.0), (5, 'cc', 43.0)]
    >>> append_rows(arrayIN, NewRows)
    >>> print(arrayIN)
    [(1, 'a', 1.1) (2, 'dd', 2.0) (3, 'x', 3.0) (4, '12', 4.0) (5, 'cc', 43.0)]

    Source: http://stackoverflow.com/a/1731228/2062965
    """
    # Calculate the number of old and new rows
    len_arrayIN = arrayIN.shape[0]
    len_NewRows = len(NewRows)
    # Resize the old recarray
    arrayIN.resize(len_arrayIN + len_NewRows, refcheck=False)
    # Write to the end of recarray
    arrayIN[-len_NewRows:] = NewRows
def append_行(arrayIN,NewRows):
“”“将行附加到numpy重新排列。
论据:
arrayIN:应该扩大的numpy重新安排
新行:与“arrayIN”形状相同的元组列表`
想法:如果可能,调整重新排列的大小。
(仅适用于小型阵列)
>>>arrayIN=np.数组([(1,'a',1.1),(2,'dd',2.0),(3,'x',3.0)],

dtype=[('a','@Paul,问题是:
“有没有更有效的方法来做到这一点”
def append_rows(arrayIN, NewRows):
    """Append rows to numpy recarray.

    Arguments:
      arrayIN: a numpy recarray that should be expanded
      NewRows: list of tuples with the same shape as `arrayIN`

    Idea: Resize recarray in-place if possible.
    (only for small arrays reasonable)

    >>> arrayIN = np.array([(1, 'a', 1.1), (2, 'dd', 2.0), (3, 'x', 3.0)],
                           dtype=[('a', '<i4'), ('b', '|S3'), ('c', '<f8')])
    >>> NewRows = [(4, '12', 4.0), (5, 'cc', 43.0)]
    >>> append_rows(arrayIN, NewRows)
    >>> print(arrayIN)
    [(1, 'a', 1.1) (2, 'dd', 2.0) (3, 'x', 3.0) (4, '12', 4.0) (5, 'cc', 43.0)]

    Source: http://stackoverflow.com/a/1731228/2062965
    """
    # Calculate the number of old and new rows
    len_arrayIN = arrayIN.shape[0]
    len_NewRows = len(NewRows)
    # Resize the old recarray
    arrayIN.resize(len_arrayIN + len_NewRows, refcheck=False)
    # Write to the end of recarray
    arrayIN[-len_NewRows:] = NewRows