Home > Net >  Numpy array - Regroup values when index equals a specified value
Numpy array - Regroup values when index equals a specified value

Time:03-02

I'm pretty new to numpy arrays, and was not able to find a good explanation / example for my issue. I saw things like take() or take_along_axis() but I didn't understood what was going on...

I have this 2D numpy, which may contain N sub-arrays, of each 5 values (h, s, i, x, y):

    values = np.array([
                    [1,2,3,4,5],
                    [1,22,33,44,55],
                    [1,22,333,444,555],
                    [1,22,333,4444,5555],
                    [1,222,33,44,55],
                    [1,222,330,440,550],
                    [10,20,30,40,50],
                    [100,200,300,400,500],
                   ])

As you can see, values can be repeated for a same index. I want to regroup sub-arrays, by indexes values, such as:

1
    2
        3
            4
            5
    22
        33
            44
            55
        333
            444
            555
            4444
            5555
    222
        33
            44
            55
        330
            440
            550
10
    20
        30
            40
            50
100
    200
        300
            400
            500

The goal is to obtain a regular array like:

array = [1, 2, 3, 4 , 5, 22, 33, 44, 55, 333, 444, 555, 4444, 5555, 222, 33, 44, 55, 330, 440, 550, 10, 20, 30, 40, 50, 100, 200, 300, 400, 500]            

Thank you very much for your support.

CodePudding user response:

you can use flatten method list(values.flatten())

CodePudding user response:

Thank you for your answer However, the flatten method wont group indexes by same values. It just reshapes as 1D array.

  • Related