W2_Ex-2_Reshaping Training Samples

Hi,
does anyone understand, why I cant flatten the train-/test-matrix using:

test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[1] * test_set_x_orig.shape[2] * test_set_x_orig.shape[3], test_set_x_orig.shape[0])

When I try this, the output does not pass the assertions that check the order of the pixel values. It seems to work only with

test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T

In principle however, the two operations should be equivalent - right?

Thanks a lot

Please pay attention to order of how elements are laid out when reshaping an array.

In [1]: import numpy as np

In [2]: a = np.arange(10)

In [3]: a.reshape(2,5)
Out[3]: 
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

In [4]: a.reshape(5,2).T
Out[4]: 
array([[0, 2, 4, 6, 8],
       [1, 3, 5, 7, 9]])

In [5]: a.reshape(5,2, order='F').T
Out[5]: 
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

In addition to Balaji’s reply, reading this will also help you understand the concepts.

Best,
Saif.