Skip to content Skip to sidebar Skip to footer

How To Rearrange Numpy Arrays Stored In A List Based On The Lengths Of The Arrays

I have a list of numpy arrays and want to rearrange it based on the length of each array. This is my list: nums=[np.array([[2.], [3.]]), np.array([[8.], [7.], [1.]]),\ np.arr

Solution 1:

1. First, I'm using the pairwise() recipe from itertools to get two successive elements at a time, instead of using indexes:

import itertools

# https://docs.python.org/3/library/itertools.html#itertools-recipesdefpairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = itertools.tee(iterable)
    next(b, None)
    returnzip(a, b)

2. I created a helper function first_or_last() which returns a slice object which can be used on a numpy array, and is applied to the larger of the two neighbouring arrays being compared. Which slice needs to be applied depends on the average/mean value, as per the logic you described above. You can reduce it to a two-line function if you want, I've used the longer version for comments/explanation.

deffirst_or_last(smaller, larger):
    """return a slice object for either the first-n items or
    the last-n items of the larger array
    """
    size = len(smaller)
    if np.mean(smaller) < np.mean(larger):
        # take first-`size` elementsreturnslice(None, size)
    # take last-`size` elementsreturnslice(-size, None)  # don't skip the extra None, needed for axes# or just# return slice(None, size) if np.mean(smaller) < np.mean(larger) else slice(-size, None)

3 . The code which does the re-arranging:

nums = [np.array([[2.], [3.]]), np.array([[8.], [7.], [1.]]),
        np.array([[1.], [0.], [5.]]), np.array([[8.], [9.]]),
        np.array([[7.], [6.]])]

final = []
for ar1, ar2 in pairwise(nums):
    l1, l2 = len(ar1), len(ar2)
    if l1 == l2:  # same length
        final.append(np.append(ar1, ar2, axis=0))
        continue# different lengths
    arrs = (ar1, ar2[first_or_last(ar1, ar2)]) if l1 < l2 else (ar1[first_or_last(ar2, ar1)], ar2)
    final.append(np.append(*arrs, axis=0))

Value of final:

[array([[2.], [3.], [8.], [7.]]),
 array([[8.], [7.], [1.], [1.], [0.], [5.]]),
 array([[0.], [5.], [8.], [9.]]),
 array([[8.], [9.], [7.], [6.]])]

Notes:

  1. The output of this differs from what you have as final. See item #9 in my comment, as I don't see the rule/logic that would make the result have 3 items and not 4.

    9. Another inconsistency in the logic: for array 3 (len 3) and array 4 (len 2), the result should be 0 5 8 9 and the last item in the result should be 8 9 7 6. Why are these merged into one as 0 5 8 9 7 6 at the end? They should be separate. What's the logic to merge them all for the 3rd, 4th and 5th arrays but not for the 1st, 2nd & 3rd arrays?

  2. This solution won't give errors for your previous/original question but it also doesn't seem to be the right output - even though it does follow the same rules.

  3. NumPy array slicing returns a view on the orignal array, not a copy.

Post a Comment for "How To Rearrange Numpy Arrays Stored In A List Based On The Lengths Of The Arrays"