Home > Mobile >  Numpy; reshape 1d number sequence to 2d number sequences
Numpy; reshape 1d number sequence to 2d number sequences

Time:02-04

Soppose I have a numpy array like this:

arr = np.array([0,1,2,5,6,7,10,11,12,15,16,17])

And I want the following result:

array([[ 0,  1,  2],
       [ 5,  6,  7],
       [10, 11, 12]
       [15, 16, 17]])

Another example would be:

arr = np.array([3900, 3901, 3902, 3903, 3964, 3965, 3966, 3967, 4028, 4029, 4030, 4031, 4092, 4093, 4094, 4095])

Result:

array([[3900, 3901, 3902, 3903],
       [3964, 3965, 3966, 3967],
       [4028, 4029, 4030, 4031],
       [4092, 4093, 4094, 4095]])

These 1d arrays are generated dynamically with different length.

result = arr.reshape((?,?))

The resulting array must consist of rows that are in sequential order. When the sequence breaks, that's a new row.

CodePudding user response:

Assuming consecutive values with breaks at regular intervals, like in your two examples. You can use np.diff(arr) to compute the difference between successive values and np.argmax(np.diff(arr)!=1) to identify the index of the first "break" in the increasing sequence. The width of the reshape is this number 1.

We can now use reshape:

arr = np.array([3900, 3901, 3902, 3903, 3964, 3965, 3966, 3967, 4028, 4029, 4030, 4031, 4092, 4093, 4094, 4095])

arr.reshape((-1, (1 np.argmax(np.diff(arr)!=1))))

output:

array([[3900, 3901, 3902, 3903],
       [3964, 3965, 3966, 3967],
       [4028, 4029, 4030, 4031],
       [4092, 4093, 4094, 4095]])

CodePudding user response:

The following works with both arrays, but would require that the rows have all the same length.

# get indices where sequence breaks
indices = np.where(np.diff(arr)!=1)[0]

# get lengths of the sequences (except one is missing)
row_length = np.diff(indices)

# check if row length matches
if np.all(row_length == row_length[0]):
    reshaped = arr.reshape((-1, row_length[0]))
    print(arr)
    print(reshaped)
else:
    print('unable to reshape, sequences have different lengths')

Output:

[ 0  1  2  5  6  7 10 11 12 15 16 17]
[[ 0  1  2]
 [ 5  6  7]
 [10 11 12]
 [15 16 17]]

[3900 3901 3902 3903 3964 3965 3966 3967 4028 4029 4030 4031 4092 4093
 4094 4095]
[[3900 3901 3902 3903]
 [3964 3965 3966 3967]
 [4028 4029 4030 4031]
 [4092 4093 4094 4095]]

CodePudding user response:

import numpy as np

arr = np.array([0,1,2,5,6,7,10,11,12,15,16,17])

result = arr.reshape(4, -1)

print(result)

Output:

[[ 0,  1,  2],
[ 5,  6,  7],
[10, 11, 12],
[15, 16, 17]]
  •  Tags:  
  • Related