# Flattening Images in the Logistic Regression Assignment in Course 1 Week 2

@chenf12: Why would you assume that’s the output order you would get? Python is an interactive language, so you can just try it and see what happens. Watch this:

``````A = testarray((3,2,2,3))
Ashape1 = A.reshape(A.shape[0],-1)
np.set_printoptions(suppress=True)
print("Ashape1.shape = " + str(Ashape1.shape))
print(Ashape1)
Ashape1.shape = (3, 12)
[[   0.    1.    2.   10.   11.   12.  100.  101.  102.  110.  111.  112.]
[1000. 1001. 1002. 1010. 1011. 1012. 1100. 1101. 1102. 1110. 1111. 1112.]
[2000. 2001. 2002. 2010. 2011. 2012. 2100. 2101. 2102. 2110. 2111. 2112.]]
``````

So you can see that you get exactly what you’d expect from the transpose that I showed in my original example.

The unrolling done by reshape happens from the highest dimension first. That is demonstrated by the two examples I gave. Notice that it’s easier to see what is happening without the transpose: it just starts at (0,0) and unrolls across the first row and it turns out it hits the end of the first row at just the right time because we set the dimensions correctly. Also have you tried reading the documentation for reshape? Just google “numpy reshape”.

Now it turns out what you will learn from reading the aforementioned documentation is that unrolling from the highest dimension first is just the default behavior of reshape. You can use the “order” parameter to specify a different arrangement. Watch this:

``````Ashape2 = A.reshape(A.shape[0],-1,order='F')
print("Ashape2.shape = " + str(Ashape2.shape))
print(Ashape2)
Ashape2.shape = (3, 12)
[[   0.  100.   10.  110.    1.  101.   11.  111.    2.  102.   12.  112.]
[1000. 1100. 1010. 1110. 1001. 1101. 1011. 1111. 1002. 1102. 1012. 1112.]
[2000. 2100. 2010. 2110. 2001. 2101. 2011. 2111. 2002. 2102. 2012. 2112.]]
``````

Notice that if you take the transpose of that matrix, it will still have the property that each column has a consistent first dimension value (0 for the first column, 1 for the second and so forth). That means that the data is not “scrambled”. It’s just a different way to order the pixels in each image. It turns out that you can run the experiment of using “F” order instead of the default “C” order and the training works exactly as well as it does with “C”. As long as you do the reshapes all in the same consistent way, the algorithm can learn to recognize the patterns.

2 Likes