Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python/numpy combine subarrays 4 rows at a time

I have a numpy array that is split by each row:

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]]

I was hoping to merge said splitArray every 4 rows, and the last subarray not necessarily having to be 4, but just the remainder of what's left.

Below is the array I hope to have:

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]]
like image 247
lee.edward01 Avatar asked Jun 04 '18 17:06

lee.edward01


People also ask

How to join two arrays in NumPy?

Because two 2-dimensional arrays are included in operations, you can join them either row-wise or column-wise. Mainly NumPy () allows you to join the given two arrays either by rows or columns. Let us see some examples to understand the concatenation of NumPy.

How to split a 2-D array in NumPy?

Here we can use the split () method for splitting the 2-dimensional array either row-wise or column-wise. In this example, we have created a simple numpy array and now we want to break the 2-d array by using np.split ().

How to concatenate arrays along axis in NumPy?

numpy.concatenate () function concatenate a sequence of arrays along an existing axis. arr1, arr2, … : [sequence of array_like] The arrays must have the same shape, except in the dimension corresponding to axis. axis : [int, optional] The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0.

What are the operations performed on NumPy arrays?

Practically these are the operations performed on NumPy: Attributes of the array. Indexing of array. Joining and parting of an array. Finally subarrays as no-duplicate perspectives: The most significant thing in array slicing is that they return sees as opposed to duplicates of the exhibit information.


3 Answers

Using a 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]])]
like image 52
Joe Iddon Avatar answered Oct 07 '22 21:10

Joe Iddon


As a pure Numpythonic approach you can find all the desired indexes for splitting your array by creating a range from chunking number to number of rows with the chunking number as thestep arg of the range. Then use np.split() to split your array:

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

Demo:

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]])]

If you want the chunked arrays to be concatenated you can use @jpp's answer with np.concatenate() and map or slightly different in a list comprehension.

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))]
like image 25
Mazdak Avatar answered Oct 07 '22 20:10

Mazdak


That can be done using the infamous grouper recipe.

>>> 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.])]
like image 22
Paul Panzer Avatar answered Oct 07 '22 19:10

Paul Panzer