Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can not reshape 2 dimensional array to 3 dimensional array for RNN in python

I can not reshape the array into 3-dimensional one. Here is the example dataset:

import pandas as pd
import numpy as np

df = {
"a": [0.06 , 0.07, 0.45, 0.98, 0.97 ],
"b": [12,45,65, 56, 34],
"c": [2,5,5, 5, 3],
"d": [23,55,25, 15, 34],
"e": [0.0005,0.55555,0.383825, 0.4747477415, 0.348344334],
"f": [0.0236 , 0.3407, 0.4545, 0.9658, 0.4597 ],
"g": [70 , 90, 123, 154, 99 ],    
}

#load into df:
df = pd.DataFrame(df)

print(df) 
df.shape

X = df[['a', 'b', 'c','d','e','f']].to_numpy()
y = df['g'].to_numpy()

X

This is what I found as a possible solution from one of the stackoverflow posts

# Reshaping the X data to be 3D
X = X.reshape(5, 7, -1)

But it did not work for me. I understand that matrix has its rules, however, is it possible to convert it into three dimensional array? Could you help me if you know? Thank you very much!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to get something like (5,7,3). The array that end with the 3.

>Solution :

I think you have to do:

X = df[['a', 'b', 'c','d','e','f']].to_numpy()
y = df['g'].to_numpy()

X.shape is (5, 6)

df[['a', 'b', 'c','d','e','f']].to_numpy().reshape(5,6,-1)

Since you have moved 'g' out to y

X = X.reshape(5, 6, -1)

Other variants are:

X=X.reshape(2, 5, 3)
X=X.reshape(5, 2, 3)
X=X.reshape(10, 1, 3)
X=X.reshape(1, 10, 3)

How we get them, actually the product of these numbers should be the product of shape. So, in this case product of (5,6) is 30.

Now, you want a 3D array with 3 at the end, so we need 10 from the first 2 dimensions.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading