Home > database >  Numpy 2D to 3D array based on data in a column
Numpy 2D to 3D array based on data in a column

Time:02-21

Let's say I have data structured in a 2D array like this:

[[1, 3, 4, 6],
 [1, 4, 8, 2],
 [1, 3, 2, 9],
 [2, 2, 4, 8],
 [2, 4, 9, 1],
 [2, 2, 9, 3]]

The first column denotes a third dimension, so I want to convert this to the following 3D array:

[[[3, 4, 6],
  [4, 8, 2],
  [3, 2, 9]],
 [[2, 4, 8],
  [4, 9, 1],
  [2, 9, 3]]]

Is there a built-in numpy function to do this?

CodePudding user response:

You can try code below:

import numpy as np
array = np.array([[1, 3, 4, 6],
 [1, 4, 8, 2],
 [1, 3, 2, 9],
 [2, 2, 4, 8],
 [2, 4, 9, 1],
 [2, 2, 9, 3]])
array = np.delete(array, 0, 1)
array.reshape(2,3,-1)

Output

array([[[3, 4, 6],
        [4, 8, 2],
        [3, 2, 9]],

       [[2, 4, 8],
        [4, 9, 1],
        [2, 9, 3]]])

However, this code can be used when you are aware of the array's shape. But if you are sure that the number of columns in the array is a multiple of 3, you can simply use code below to show the array in the desired format.

array.reshape(array.shape[0]//3,3,-3)

CodePudding user response:

Use numpy array slicing with reshape function.

import numpy as np 

arr = [[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]

# convert the list to numpy array
arr = np.array(arr)

# remove first column from numpy array
arr = arr[:,1:]

# reshape the remaining array to desired shape
arr = arr.reshape(len(arr)//3,3,-1)

print(arr)

Output:

[[[3 4 6]
  [4 8 2]
  [3 2 9]]

 [[2 4 8]
  [4 9 1]
  [2 9 3]]]

CodePudding user response:

You list a non numpy array. I am unsure if you are just suggesting numpy as a means to get a non numpy result, or you are actually looking for a numpy array as result. If you don't actually need numpy, you could do something like this:

arr = [[1, 3, 4, 6],
       [1, 4, 8, 2],
       [1, 3, 2, 9],
       [2, 2, 4, 8],
       [2, 4, 9, 1],
       [2, 2, 9, 3]]

# Length of the 3rd and 2nd dimension.
nz = arr[-1][0]   (arr[0][0]==0)
ny = int(len(arr)/nz)

res = [[arr[ny*z_idx y_idx][1:] for y_idx in range(ny)] for z_idx in range(nz)]

OUTPUT:

[[[3, 4, 6], [4, 8, 2], [3, 2, 9]], [[2, 4, 8], [4, 9, 1], [2, 9, 3]]]

Note that the calculation of nz takes into account that the 3rd dimension index in your array is either 0-based (as python is per default) or 1-based (as you show in your example).

  • Related