Home > Enterprise >  Reshaping array to fit in Keras model
Reshaping array to fit in Keras model

Time:10-18

Let's say I have some data with this shape:

X=np.array(X[0:10368]).reshape(432,24,1)
Y=np.array(Y[0:10368]).reshape(432,24,1)

So I want to feed my model this way:

X vector:           Y vector:         Example:

[24 x 1] vector --> [2x1] vector  / [0,1,...,24] ---> [0,1]
[24 x 1] vector --> [2x1] vector  / [0,1,...,24] ---> [0,0]
[24 x 1] vector --> [2x1] vector  / [0,1,...,24] ---> [1,0]
    .                 .                      .
    .                 .                      .
    .                 .                      .
432 batches         432 batches             432 batches

How can I reshape my Y to be this way?

Y = np.random.randint(2, size=(432, 2))

I want my Y to be: (432, 2)

CodePudding user response:

With the information that I have, I would suggest trying something like this:

X = np.random.random(size=(5192, 24))
Y = np.random.randint(2, size=(5192, 2))

N = 432

indices = np.random.choice(X.shape[0], N, replace=False)
X_reduced = X[indices]
Y_reduced = Y[indices]

print(X_reduced.shape)
print(Y_reduced.shape)

#(432, 24)
#(432, 2)

Since your dataset is made up of 5192 entries, what I am doing is randomly choosing 432 indices from X and Y in order to get the shapes (432, 24) and (432, 2). If you want to preserve the order of your data, you can just apply sliding windows of size 432 to your data without any randomness:

X = np.random.random(size=(5192, 24))
Y = np.random.randint(2, size=(5192, 2))

N = 432

X = [X[i:i N,:] for i in range(0, X.shape[0], N)]
Y = [Y[i:i N,:] for i in range(0, Y.shape[0], N)]

print(X[0].shape)
print(Y[0].shape)

#(432, 24)
#(432, 2)

Note that the last batch has the shape (8, 24) for X and (8,2) for Y as the 5192 entries cannot be equally divided by 432. You could, for example, copy the first 424 entries from your first batch into your last batch. That way all batches are equal.

  • Related