0

I have a block of data from a csv file i have converted into a numpy array of x,y and z values. the data is a series of lines, i have organised the data so that the lines are sorted by x and then y values so get the correct order i want.

The problem i have is that this data contains a series of lines, i want to split the array into blocks a section for each line that i can work on. i have some code that can find the points i require to split the array:

for i in range(0,array_len):
    if data_sort[i][0] == data_sort[i+1][0]:
        pass
    else:
        print("split")

where data_sort contains my array and the code checks if the values in the column change. Where the consecutive column values change i need to split the array. the print is just there to test i had the correct number of splits which i did.

does anyone have a good way of doing this without needing to know the final array size required?

Edit if the input data was:

array([[-0.18798 ,  0.028104, -0.14745 ],
   [-0.18798 ,  0.028214, -0.14732 ],
   [-0.11279 ,  0.028188, -0.21054 ],
   [-0.11279 ,  0.028214, -0.21052 ],
   [-0.18798 ,  0.028214,  0.14732 ],
   [-0.18798 ,  0.028104,  0.14745 ],
   [-0.037596,  0.028   , -0.23602 ],
   [-0.037596,  0.028214, -0.23585 ],
   [ 0.      ,  0.028   , -0.23899 ],
   [ 0.      ,  0.028214, -0.23883 ]])

I would like to split the array into the following way:

    [array([[-0.18798 ,  0.028104, -0.14745 ],
        [-0.18798 ,  0.028214, -0.14732 ],
        [-0.11279 ,  0.028188, -0.21054 ],
        [-0.11279 ,  0.028214, -0.21052 ],
        [-0.18798 ,  0.028214,  0.14732 ]]),
    array([[-0.18798 ,  0.028104,  0.14745 ],
        [-0.037596,  0.028   , -0.23602 ],
        [-0.037596,  0.028214, -0.23585 ],
        [ 0.      ,  0.028   , -0.23899 ],
        [ 0.      ,  0.028214, -0.23883 ]])]

This was done using np.split(test,2), but this simply splits the array into even blocks

1 Answer 1

1

You can use np.split as shown below.

Say you want to split the array into 4 groups. group1 - index 0 to 1 group2 - index 2 to 3 group3 - index 4 to 8 group4 - index 9 to 10

then you can do

np.split(arr, [1,3,8])

which will give

[array([[-0.18798 ,  0.028104, -0.14745 ]]), array([[-0.18798 ,  0.028214, -0.14732 ],
       [-0.11279 ,  0.028188, -0.21054 ]]), array([[-0.11279 ,  0.028214, -0.21052 ],
       [-0.18798 ,  0.028214,  0.14732 ],
       [-0.18798 ,  0.028104,  0.14745 ],
       [-0.037596,  0.028   , -0.23602 ],
       [-0.037596,  0.028214, -0.23585 ]]), array([[ 0.      ,  0.028   , -0.23899 ],
       [ 0.      ,  0.028214, -0.23883 ]])]
Sign up to request clarification or add additional context in comments.

4 Comments

Thats not what i was really looking for, i will edit the post to show a desired output, but i already know the positions to split the array
so if you already know the indices where you have to split, why not just pass those indices to np.split or am I still missing something
i believe that n.split does not allow you to specify the position of split but rather the number of blocks, but i may well be wrong
You can specify the indices as an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.