簡體   English   中英

python / numpy組合子數組一次4行

[英]python/numpy combine subarrays 4 rows at a time

我有一個由每行分開的numpy數組:

splitArray:


[[0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0]]

我希望每4行合並一次splitArray,最后一個子陣列不一定必須是4,而只剩下剩下的剩余部分。

下面是我希望擁有的數組:

joinedArray:


[[0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0],
 [0,   0,   0,   0,   0,   0,  0,
  0,   0,   0,   0,   0,   0,  0]]

使用list-comp:

[a[i:i+4] for i in range(0, len(a), 4)]
#[array([[0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0]]),
# array([[0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0]]),
# array([[0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0]]),
# array([[0, 0, 0, 0, 0, 0, 0],
#        [0, 0, 0, 0, 0, 0, 0]])]

這可以使用臭名昭着的石斑魚配方來完成。

>>> from itertools import zip_longest
>>> import numpy as np
>>> 
>>> data = [7 * [0] for i in range(14)]
>>> i=iter(data); list(map(np.concatenate, zip_longest(*4*(i,), fillvalue=[])))
[array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0]), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])]

作為一種純粹的Numpythonic方法,您可以通過創建從分塊編號到行數的范圍來找到用於拆分陣列的所有所需索引,其中分塊編號作為rangestep arg。 然后使用np.split()拆分數組:

In [24]: def chunk_array(arr, ch):
    ...:     x = arr.shape[0]
    ...:     return np.split(a, np.arange(ch, x, ch))
    ...: 
    ...: 

演示:

In [25]: chunk_array(a, 4)
Out[25]: 
[array([[0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0]])]

In [26]: chunk_array(a, 3)
Out[26]: 
[array([[0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0]]), array([[0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0]])]

如果你想要連接分塊數組,你可以使用@jpp的答案與np.concatenate()map或略有不同的列表理解。

In [75]: def chunk_array(arr, ch):
    ...:     x = arr.shape[0]
    ...:     return [np.concatenate(subs) for subs in np.split(arr, np.arange(ch, x, ch))]

您可以將np.concatenatenp.split np.concatenate使用。 如果需要,您可以調整以下示例以輸出列表列表而不是數組列表。

如上所述,單個鋸齒狀的numpy陣列不是一個好主意。

A = np.zeros((14, 3))

res = list(map(np.concatenate, np.split(A, np.arange(4, A.shape[0], 4))))

print(res)

[array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),
 array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),
 array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),
 array([ 0.,  0.,  0.,  0.,  0.,  0.])]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM