0

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!

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

Shu
  • 67
  • 6
  • Can you provide the explicit expected output 3D array given your input? – mozway Mar 07 '23 at 14:36
  • I was following the code from this kaggle notebook https://www.kaggle.com/code/mikoajfish99/simple-rnn-forecasting-price-of-a-house I wanted to reshape so that I can get something like this: X_train shape: (312, 1, 3) X_test shape: (35, 1, 3) y_train shape: (312,) y_test shape: (35,) Which I could use for the lstm modeling – Shu Mar 07 '23 at 14:41
  • something like reshaping what I have to (*, *, 3) – Shu Mar 07 '23 at 14:44

1 Answers1

0

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.

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
  • Thank you, but did not work for me. I wanted something that transform my array (5,6) to something (*, *, 3). Where * is a number. Do you know what we can do with it? – Shu Mar 07 '23 at 14:47
  • 1
    What about `(2,5,3)` , `(5,2,3)`,`(10,1,3)`, or `(1,10,3)`? – Talha Tayyab Mar 07 '23 at 14:48
  • Thank you very much! It worked. May you have a wonderful day! – Shu Mar 07 '23 at 14:57