0

With

>>> a.shape
(207, 155, 3)

What does this numpy code do to the numpy array a?

a = a.T.reshape(self.channels,-1).reshape(-1)
Gareth Rees
  • 60,601
  • 9
  • 119
  • 155
Alexandre Holden Daly
  • 6,714
  • 5
  • 20
  • 35

1 Answers1

4
>>> a.shape
(207, 155, 3)

I'm going to suppose that a represents an image of size 155×207 pixels, with 3 colour channels per pixel:

>>> height, width, channels = a.shape

(Note that I'm assuming here that the first axis is vertical and the second axis is horizontal: see "Multidimensional Array Indexing Order Issues" for an explanation.)

>>> b = a.T
>>> b.shape
(3, 155, 207)

a.T returns the transposed array. But actually under the hood it doesn't alter the image data in any way. A NumPy array has two parts: a data buffer containing the raw numerical data, and a view which describes how to index the data buffer. When you reshape or transpose an array, NumPy leaves the data buffer alone and creates a new view describing the new way to index the same data. (See here for a longer explanation.)

So a indexes the image using three axes (y, x, c), and b indexes the same image using the same three axes in the opposite order (c, x, y):

>>> x, y, c = 100, 200, 1
>>> a[y, x, c] == b[c, x, y]
True

The first call to numpy.reshape:

>>> c = b.reshape(3, -1)
>>> c.shape
(3, 32085)

flattens the last two indices into one (with the third index changing fastest), so that c indexes the image using two axes (c, x × height + y):

>>> a[y, x, c] == c[c, x * height + y]
True

The second reshape:

>>> d = c.reshape(-1)
>>> d.shape
(96255,)

flattens the remaining two indices into one, so that d indexes the image using the single axis ((c × width) + x) × height + y:

>>> a[y, x, c] == d[((c * width) + x) * height + y]
True

Note that the whole operation could be done in just one step using numpy.flatten:

>>> (a.flatten(order='F') == d).all()
True
Community
  • 1
  • 1
Gareth Rees
  • 60,601
  • 9
  • 119
  • 155