Skip to content Skip to sidebar Skip to footer

Numpy Shape Not Including Subarrays

I'm using a library (keras) that's dependent on having a specific shape of a numpy array. I've never had the following issue: numpy isn't putting forward the correct shape? Here's

Solution 1:

Let's say you have data like these:

a = pd.Series(np.arange(1,3))
b = a.apply(lambda n: n * np.arange(3*4).reshape(4,-1))

b.values is now:

[ array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
 array([[ 0,  2,  4],
       [ 6,  8, 10],
       [12, 14, 16],
       [18, 20, 22]])]

You want to "stack" these two, 2D arrays to make a single 3D array. For example, 11 should be on top of 22, and adjacent to those, 10 should be on top of 20.

Use np.stack(b, 0):

[ array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
 array([[ 0,  2,  4],
       [ 6,  8, 10],
       [12, 14, 16],
       [18, 20, 22]])]

Post a Comment for "Numpy Shape Not Including Subarrays"