# Data augmentation technique not working correctly

Write a function that can shift an MNIST image in any direction (left, right, up, or down) by one pixel.⁠6 Then, for each image in the training set, create four shifted copies (one per direction) and add them to the training set. Finally, train your best model on this expanded training set and measure its accuracy on the test set. You should observe that your model performs even better now! This technique of artificially growing the training set is called data augmentation or training set expansion.

I am reading HOML book chapter two exercise 2 this is my attempt so far:

from scipy.ndimage.interpolation import shift
import numpy as np
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import cross_val_score

def shift_image(image, direction):
image = image.reshape((28, 28))
if direction == "right":
return shift(image, [0, 1], cval=0).reshape(1, -1)
elif direction == "left":
return shift(image, [0, -1], cval=0).reshape(1, -1)
elif direction == "up":
return shift(image, [-1, 0], cval=0).reshape(1, -1)
elif direction == "down":
return shift(image, [1, 0], cval=0).reshape(1, -1)

X_train_augmented = []
y_train_augmented = []

for img, label in zip(X_train, y_train):
X_train_augmented.append(img)
y_train_augmented.append(label)
for direction in ["right", "left", "up", "down"]:
X_train_augmented.append(shift_image(img, direction))
y_train_augmented.append(label)

X_train_augmented = np.vstack(X_train_augmented)
y_train_augmented = np.array(y_train_augmented)

sgd_clf = SGDClassifier()
sgd_clf.fit(X_train_augmented, y_train_augmented)
cross_val_score(sgd_clf, X_train_augmented, y_train_augmented, cv=3, scoring="accuracy")


however if we see the performance compared to a regular SGDclassifier in the data augmentatrion one ahs lower accuracy :

array([0.83068, 0.76707, 0.82064]) #sgd with augmented data performance
array([0.8788 , 0.8797 , 0.89235])# regular SDG performance

• The order in which you do things indeed is no 100%, you should not test on augmented images. Have a look at this answer: datascience.stackexchange.com/a/41485/54395 Commented Mar 11 at 10:03