Python multilayer list data processing question.

Asked 2 years ago, Updated 2 years ago, 79 views

I want to make a list of sizes (900, 156, 2) by repeating the process of averaging 2496 data with axis=1 in a three-dimensional list of sizes (900, 2496,2) in 16 window sizes and returning it as a single value a total of 156 times. What should I do?

Once the for slicing and operation and then (900, 1, 2) (900, 156, 2) and 156 times the quarters of this got, list list jjari to should make it, that's the way?

I don't know how to add the list only to a specific axis in a multidimensional list, so I'm opening the (900, 1, 2) to a one-dimensional list (1800, 1) and index the ordered data into two (900, 1) and make it into a stack (900, 2) and increase the dimension to (900, 2, 2).

I had a long question, but in conclusion, I would like to make a list of (900, 1566, 2) size list by calculating only the Axis=1 part of the (900, 2496, 2) size list as window size 16. I'd appreciate it if you could tell me the necessary function.

python list multidimensional-list numpy

2022-09-22 15:21

1 Answers

Since the dimensions are too big, let's divide the (2, 10, 3) array by two at axis=1 and average it to make it a (2, 5, 3) array.

It's usually numpy to deal with this multidimensional array, so let's do it on a numpy basis.

>>> import numpy as np
>>> from pprint import pprint
>>> a = np.arange(60).reshape(2, 10, 3)
>>> a
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8],
        [ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17],
        [18, 19, 20],
        [21, 22, 23],
        [24, 25, 26],
        [27, 28, 29]],

       [[30, 31, 32],
        [33, 34, 35],
        [36, 37, 38],
        [39, 40, 41],
        [42, 43, 44],
        [45, 46, 47],
        [48, 49, 50],
        [51, 52, 53],
        [54, 55, 56],
        [57, 58, 59]]])
>>> a.shape
(2, 10, 3)
>>> np.array_split(a, 5, axis=1)
[array([[[ 0,  1,  2],
        [ 3,  4,  5]],

       [[30, 31, 32],
        [33, 34, 35]]]),
 array([[[ 6,  7,  8],
        [ 9, 10, 11]],

       [[36, 37, 38],
        [39, 40, 41]]]),
 array([[[12, 13, 14],
        [15, 16, 17]],

       [[42, 43, 44],
        [45, 46, 47]]]),
 array([[[18, 19, 20],
        [21, 22, 23]],

       [[48, 49, 50],
        [51, 52, 53]]]),
 array([[[24, 25, 26],
        [27, 28, 29]],

       [[54, 55, 56],
        [57, 58, 59]]])]

>>> for sub in np.array_split(a, 5, axis=1):
    pprint(sub)


array([[[ 0,  1,  2],
        [ 3,  4,  5]],

       [[30, 31, 32],
        [33, 34, 35]]])
array([[[ 6,  7,  8],
        [ 9, 10, 11]],

       [[36, 37, 38],
        [39, 40, 41]]])
array([[[12, 13, 14],
        [15, 16, 17]],

       [[42, 43, 44],
        [45, 46, 47]]])
array([[[18, 19, 20],
        [21, 22, 23]],

       [[48, 49, 50],
        [51, 52, 53]]])
array([[[24, 25, 26],
        [27, 28, 29]],

       [[54, 55, 56],
        [57, 58, 59]]])

>>> for sub in np.array_split(a, 5, axis=1):
    pprint(sub.mean(axis=1))


array([[ 1.5,  2.5,  3.5],
       [31.5, 32.5, 33.5]])
array([[ 7.5,  8.5,  9.5],
       [37.5, 38.5, 39.5]])
array([[13.5, 14.5, 15.5],
       [43.5, 44.5, 45.5]])
array([[19.5, 20.5, 21.5],
       [49.5, 50.5, 51.5]])
array([[25.5, 26.5, 27.5],
       [55.5, 56.5, 57.5]])

>>> result = np.stack([ sub.mean(axis=1) for sub in np.array_split(a, 5, axis=1) ], axis=1)
>>> result
array([[[ 1.5,  2.5,  3.5],
        [ 7.5,  8.5,  9.5],
        [13.5, 14.5, 15.5],
        [19.5, 20.5, 21.5],
        [25.5, 26.5, 27.5]],

       [[31.5, 32.5, 33.5],
        [37.5, 38.5, 39.5],
        [43.5, 44.5, 45.5],
        [49.5, 50.5, 51.5],
        [55.5, 56.5, 57.5]]])
>>> result.shape
(2, 5, 3)


2022-09-22 15:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.