Get startedGet started for free

Array acrobatics

1. Array acrobatics

In this penultimate lesson, we'll discuss rearranging data by flipping the order of array elements and changing axis order.

2. Data augmentation

Why are these array acrobatics useful? In machine learning, data augmentation is the process of adding additional data by performing small manipulations on data that is already available. For example, let's say we've got a dataset of a thousand images we are using to train a model classifying whether the items are recyclable or not. We could augment this data by flipping each image and use both the flipped and original images to train the model. This helps the model learn that image orientation isn't relevant to its classification as recyclable or not.

3. Flipping an array

np-dot-flip reverses the order of array elements. Its default behavior is to flip array elements along every axis. Let's flip the NumPy logo RGB data and compare it to the original. The first axis, representing ordered rows of pixels, is flipped so that the rows of pixels formerly at the bottom of the image now appear at the top. The second axis, representing ordered columns of pixels, is flipped so that columns of pixels formerly on the left now appear on the right. Finally, the red, green, and blue values in the third axis are flipped so that each blue value is replaced with the red value for that pixel and vice versa. Since green is the middle value, green values remain the same.

4. Flipping along an axis

We can flip along a specified axis rather than all axes by setting the axis keyword argument. Here, we flip the NumPy logo along only the first axis, so that the order of the rows of pixels is reversed.

5. Flipping along an axis

We could flip only the RGB values by specifying the third axis, at index two.

6. Flipping multiple axes

Finally, we can flip along some axes but not others by enclosing the axes to flip in a tuple and setting this tuple as the axis keyword argument. Here, we keep the colors in the third axis the same while flipping the pixel row and column order in the first two axes.

7. Transposing an array

Here's an example of what np-dot-flip does to a 2D array of floats: it reverses element order along both axes since no axis keyword argument is passed. In contrast, np-dot-transpose flips axis order while keeping the element order within each axis the same. When passing the same array to np-dot-transpose, the first and second axes are swapped so that columns become rows and rows become columns. For example, what was the first column before transposing is now the first row. However, the order of the elements has not changed; they are just presented along a different axis.

8. Setting transposed axis order

The default behavior for np-dot-transpose is to reverse the axis order. We can also specify a custom axis order using the axes keyword argument. For example, in our NumPy logo, we make column values into row values and row values into column values by changing the axis order from zero, one, two to one, zero, two. This leaves the third axis, color, in the same position. Be careful! This keyword argument is axes, not axis, and it's named this way because we must pass all axes in the order we'd like them to appear in the output array - even if they remain in their original location in axis order, as the third axis does in this case.

9. Let's practice!

Let's have some fun practicing these skills with our Monet array.