'Converting numpy 2d array to 3d

I have a 2d array of numbers coming from a csv

this is just a example of the data shape

[[ 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]
 [60 61 62 63 64 65 66 67 68 69]
 [70 71 72 73 74 75 76 77 78 79]
 [80 81 82 83 84 85 86 87 88 89]
 [90 91 92 93 94 95 96 97 98 99]]

Im learning to use numpy, my goal is to convert that 2d array into a 3d array of shape (10,2,4) for example

index 0

[[ 0  1  2  3  4  5  6  7  8  9]
 [10 11 12 13 14 15 16 17 18 19]]

index 1

 [[20 21 22 23 24 25 26 27 28 29]
 [30 31 32 33 34 35 36 37 38 39]]

index 2

 [[40 41 42 43 44 45 46 47 48 49]
 [50 51 52 53 54 55 56 57 58 59]]

index 3

 [[60 61 62 63 64 65 66 67 68 69]
 [70 71 72 73 74 75 76 77 78 79]]

index 4

 [[80 81 82 83 84 85 86 87 88 89]
 [90 91 92 93 94 95 96 97 98 99]]

I can do this by using a loop, but i wonder if there is a better way

also concatenating rows in one column would also work

my goal is to fit a keras model where a single sample is composed of multiple rows of a dataframe



Solution 1:[1]

You can use slicing and list comprehension

>>> src = [[ 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],
 [60, 61, 62, 63 ,64 ,65 ,66 ,67 ,68 ,69],
 [70, 71, 72, 73 ,74 ,75 ,76 ,77 ,78 ,79],
 [80, 81, 82, 83 ,84 ,85 ,86 ,87 ,88 ,89],
 [90, 91, 92, 93 ,94 ,95 ,96 ,97 ,98 ,99]]

>>> [src[i:i+2] for i in range(0,len(src),2)]
[[[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]],
 [[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
  [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]],
 [[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
  [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]]

Hope this answer your question

Edit : I re-read your question and look for the comment that @Anshumaan-mishra mention. You can also use numpy reshape.

>>> import numpy as np
>>> src = np.reshape(src,(5, 2, 10))
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]],

       [[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
        [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]],

       [[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
        [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]])

I answer with my code above because I'm focusing on the data structure that you want

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1