Difference between the code and why is my code wrong

train_set_x_flatten = train_set_x_orig.reshape((3*(num_px**2)), train_set_x_orig.shape[0])# Shape: (num_pxnum_px3, m_train)

test_set_x_flatten = test_set_x_orig.reshape((3*(num_px**2)), test_set_x_orig.shape[0]) # Shape: (num_pxnum_px3, m_test)

VS

train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T # Shape: (num_pxnum_px3, m_train)

test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T # Shape: (num_pxnum_px3, m_test)

The code written first is the one which i wrote but somehow the answer i got wasn’t correct viz the correct reshaped matrix. Can anyone explain why??

See : https://www.coursera.org/learn/neural-networks-deep-learning/programming/thQd4/logistic-regression-with-a-neural-network-mindset

go to the reshaping function (Exercise 2 ).

Regards

It is an interesting question that has come up before. It turns out that your method scrambles the data. It depends on the mechanics of how the “reshape” operation works. Just ending up with the correct shape is not good enough. Here’s a thread which explains this in detail.

1 Like

Thank you sir