Search code examples
rimagemnistkaggle

R - Image Plot MNIST dataset


My data set is the MNIST from Kaggle

I am trying to use the image function to visualise say the first digit in the training set. Unfortunately I am getting the following error:

>image(1:28, 1:28, im, col=gray((0:255)/255))
Error in image.default(1:28, 1:28, im, col = gray((0:255)/255)) : 
'z' must be numeric or logical

Adding a few codes:

rawfile<-read.csv("D://Kaggle//MNIST//train.csv",header=T) #Reading the csv file
im<-matrix((rawfile[1,2:ncol(rawfile)]), nrow=28, ncol=28) #For the 1st Image

image(1:28, 1:28, im, col=gray((0:255)/255))

Error in image.default(1:28, 1:28, im, col = gray((0:255)/255)) : 
'z' must be numeric or logical

Solution

  • At the moment your im is a matrix of characters. You need to convert it to a matrix of numbers, e.g. by issuing im_numbers <- apply(im, 2, as.numeric).

    You can then issue image(1:28, 1:28, im_numbers, col=gray((0:255)/255)).