Ben Gorman

Ben Gorman

Life's a garden. Dig it.

Challenge

Here's a grayscale image of a hand-written four from the MNIST dataset.

Four

import numpy as np
 
arr = np.array(
  [[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  50, 224,   0,   0,   0,   0,   0,   0,   0,  70,  29,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 121, 231,   0,   0,   0,   0,   0,   0,   0, 148, 168,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   4, 195, 231,   0,   0,   0,   0,   0,   0,   0,  96, 210,  11,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,  69, 252, 134,   0,   0,   0,   0,   0,   0,   0, 114, 252,  21,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,  45, 236, 217,  12,   0,   0,   0,   0,   0,   0,   0, 192, 252,  21,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0, 168, 247,  53,   0,   0,   0,   0,   0,   0,   0,  18, 255, 253,  21,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,  84, 242, 211,   0,   0,   0,   0,   0,   0,   0,   0, 141, 253, 189,   5,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0, 169, 252, 106,   0,   0,   0,   0,   0,   0,   0,  32, 232, 250,  66,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,  15, 225, 252,   0,   0,   0,   0,   0,   0,   0,   0, 134, 252, 211,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,  22, 252, 164,   0,   0,   0,   0,   0,   0,   0,   0, 169, 252, 167,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   9, 204, 209,  18,   0,   0,   0,   0,   0,   0,  22, 253, 253, 107,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0, 169, 252, 199,  85,  85,  85,  85, 129, 164, 195, 252, 252, 106,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,  41, 170, 245, 252, 252, 252, 252, 232, 231, 251, 252, 252,   9,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,  49,  84,  84,  84,  84,   0,   0, 161, 252, 252,   0,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 127, 252, 252,  45,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 128, 253, 253,   0,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 127, 252, 252,   0,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 135, 252, 244,   0,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 232, 236, 111,   0,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 179,  66,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
   [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0]]
)

Modify the array so that the image flips across the horizontal centerline.

Four flipped

Solution

Flipping an object is one example of an affine transformation. There are multiple ways to do it, but a simple technique (implemented below) is to index arr with an array that selects the last row first, the second-to-last row second, and so on.

import numpy as np
import matplotlib.pyplot as plt
 
flipped = arr[np.arange(27,-1,-1)] 
 
plt.imshow(
  X=flipped,
  vmin=0, 
  vmax=255,
  interpolation='none',
  cmap='gray',
)