Antonio L&#243;pez Ruiz - 2 years ago 91
Python Question

# Making an array of matrices into array of triples of matrices / Python Pandas

I have 891 matrices of size 7X32 together in an array. I would like to get 891 triples of matrices of size 7X32.

Example of two matrices (not that size obviously):

``````[[1,1,1,1,1],
[2,2,2,2,2],
[3,3,3,3,3]],
[[2,2,2,2,2],
[2,2,2,2,2],
[3,3,3,3,3]]
``````

I need it to become:

``````[[1,1,1,1,1],
[2,2,2,2,2],
[3,3,3,3,3]],
[[1,1,1,1,1],
[2,2,2,2,2],
[3,3,3,3,3]],
[[1,1,1,1,1],
[2,2,2,2,2],
[3,3,3,3,3]],
[[2,2,2,2,2],
[2,2,2,2,2],
[3,3,3,3,3]],
[[2,2,2,2,2],
[2,2,2,2,2],
[3,3,3,3,3]],
[[2,2,2,2,2],
[2,2,2,2,2],
[3,3,3,3,3]],
``````

where [] means it's a different matrix.

The code I used to get the matrices:

``````matrices = np.dstack([df.values.astype(int)] * 32)
``````

from a regular dataframe.

I thought maybe

``````matrices = np.dstack([[df.values.astype(int)] * 32]*3)
``````

would work, but it didn't.

I'm doing this to try to make a regular problem into a 'image' like the cifar 10 data.

From your original dataframe, this seems to work. However, I'd defer to @Divakar's advice if he produces what you need.

As for my solution, I'm sure there is a better way, but this worked. This might even give @Divakar what he needs to see in order to show us all a better way.

``````np.stack([np.dstack([df.values.astype(int)] * 32)] * 3).transpose(1, 0, 2, 3)
``````

Setup

``````df = pd.DataFrame({
'Embarked': [0, 1, 1, 3, 1],
'Family': [1, 1, 0, 1, 0],
'Fare': [1.0, 2.0, 1.0, 2.0, 2.0],
'Pclass': [3, 1, 3, 1, 3],
'Sex': [0, 1, 1, 1, 0],
'Title': [0, 3, 2, 3, 1]}
)['Pclass Sex Fare Embarked Title Family'.split()]
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download