Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*******please program it in python******* Write a function that can shift an MNI

ID: 3748979 • Letter: #

Question

*******please program it in python*******
Write a function that can shift an MNIST image in any direction (left, right, up, or down) by one pixel. 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 can use the shift() function from the scipy.ndimage.interpolation module. For example, shift(image, [2, 1], cval=0) shifts the image 2 pixels down and 1 pixel to the right.

*******please program it in python*******
Write a function that can shift an MNIST image in any direction (left, right, up, or down) by one pixel. 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 can use the shift() function from the scipy.ndimage.interpolation module. For example, shift(image, [2, 1], cval=0) shifts the image 2 pixels down and 1 pixel to the right.

*******please program it in python*******
Write a function that can shift an MNIST image in any direction (left, right, up, or down) by one pixel. 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 can use the shift() function from the scipy.ndimage.interpolation module. For example, shift(image, [2, 1], cval=0) shifts the image 2 pixels down and 1 pixel to the right.

Explanation / Answer

Download MNIST train data from https://pjreddie.com/media/files/mnist_train.csv
Download MNIST test data from https://pjreddie.com/media/files/mnist_test.csv

===================================================================================

import csv
from numpy import genfromtxt
import scipy.ndimage.interpolation as inp


train = genfromtxt('mnist_train.csv', delimiter=',')

train_label, train_data = np.split(train,[1],1)
train_data = np.reshape(train_data ,(-1,28,28))
right_shifted = inp.shift(train_data,[0,0,1],cval = 0)
left_shifted = inp.shift(train_data,[0,0,-1],cval = 0)
down_shifted = inp.shift(train_data,[0,1,0],cval = 0)
up_shifted = inp.shift(train_data,[0,-1,0],cval = 0)

Final_train = np.concatenate((train_data,right_shifted,left_shifted,down_shifted,up_shifted ),axis = 0)
Final_train_label = np.concatenate((train_label,train_label,train_label,train_label,train_label ),axis = 0)

test = genfromtxt('mnist_test.csv', delimiter=',')
test_label, test_data = np.split(test,[1],1)

Note: Final_train_label,Final_train_data,test_label,test_data contains the training label, training example, test label and test example respectively.

Training best model will take so much time to train. Use this data to train your model.