Question about reshaping matrix (DLS Course 1, Week 2, Exercise 2)

Hi, I just want to know why we should specifically use the function

X_flatten = X.reshape(X.shape[0], -1).T

as the solution for the problem. For example when I use

(1)
train_set_x_flatten = train_set_x_orig.reshape(num_pxnum_px3,m_train)

where previously:
m_train = train_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]

instead of

(2)
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T

to reshape the matrix. Both functions will reshape the matrix into the size of (12288, 209) however the first few elements are different when looked using train_set_x_flatten[0:10,1] command.

The function (1) when I type will result in
array([31, 34, 79, 46, 33, 69, 36, 20, 67, 31], dtype=uint8)
But the function (2) will result in
array([196, 192, 190, 193, 186, 182, 188, 179, 174, 213], dtype=uint8)

Does anyone know the difference between both functions? Please let me know. Thank you

Hello Vinwilbe,

Welcome to the community!

Here’s a brilliant thread posted by Super mentor Paul (sir), would give you a detailed idea on why we specifically use X_flatten = X.reshape(X.shape[0], -1).T to reshape the matrix.

1 Like