Ashley O
Ashley O

Reputation: 1190

Reshaping Array from 1D to 3D for Keras

I want to reshape my data so that it displays 3 dimensions.

Here is code to create dummy data:

Sample = [{'account': 'Jones LLC', 'Jan': 150, 'Label': 0, 'Mar': [[.332, .326], [.058, .138]]},
 {'account': 'Alpha Co',  'Jan': 200, 'Label': 0, 'Mar': [[.234, .246], [.013, .592]]},
 {'account': 'Blue Inc',  'Jan': 50,  'Label': 1,  'Mar': [[.084, .23], [.745, .923]]}]
df = pd.DataFrame(Sample)

Here is the data visualized:

 df:
  account        Jan                    Mar
Jones LLC  |     150   |  [[.332, .326], [.058, .138]]
Alpha Co   |     200   |  [[.234, .246], [.234, .395]] 
Blue Inc   |     50    |  [[.084, .23], [.745, .923]]

Now if I type:

df['Mar'].shape

I get (3,)

How can I change this column so that its shape is (3, 2, 2), to represent the data within the arrays?

Thanks!!!

Upvotes: 0

Views: 256

Answers (1)

woodpav
woodpav

Reputation: 2027

np.asarray(df['Mar'].values.tolist())

This has shape (3, 2, 2).

The problem is because the rows of df['Mar'] are lists and simply using as_matrix() returns an array of lists. Casting everything to a list and then to an array gets everything to np.array.

Upvotes: 1

Related Questions