Two Ways of Visualization of MNIST with R

Background: https://charleshsliao.wordpress.com/2017/02/24/svm-tuning-based-on-mnist/

To load data of MNIST and visualize it would be significant for future exploration, and here are two ways to do it.

1.Load data first, visualize single digit as necessity.

1.1 Load Data

load_image_file <- function(filename) {
 ret = list()
 f = file(filename,'rb')
 readBin(f,'integer',n=1,size=4,endian='big')
 ret$n = readBin(f,'integer',n=1,size=4,endian='big')
 nrow = readBin(f,'integer',n=1,size=4,endian='big')
 ncol = readBin(f,'integer',n=1,size=4,endian='big')
 x = readBin(f,'integer',n=ret$n*nrow*ncol,size=1,signed=F)
 ret$x = matrix(x, ncol=nrow*ncol, byrow=T)
 close(f)
 ret
}

load_label_file <- function(filename) {
 f = file(filename,'rb')
 readBin(f,'integer',n=1,size=4,endian='big')
 n = readBin(f,'integer',n=1,size=4,endian='big')
 y = readBin(f,'integer',n=n,size=1,signed=F)
 close(f)
 y
}

build histograms to see the distribution of digits:


resTable <- table(as.numeric(Training$n))
par(mfrow = c(1, 1), yaxt="s",xaxt="s")
par(mar = c(5, 4, 4, 2) + 0.1) # increase y-axis margin.
plot <- plot(Training$n,main = "Total Number of Digits (Training Set)",
 ylim = c(0, 9500), ylab = "Examples Number")
text(x = plot, y = resTable + 50, labels = resTable, cex = 0.75)

# Build hist to show numbers of Test set
testTable <- table(as.numeric(Test$n))
plot <- plot(Test$n,main = "Total Number of Digits (Test Set)",
 ylim = c(0, 1500), ylab = "Examples Number")
text(x = plot, y = testTable + 50, labels = testTable, cex = 0.75)

1.2Visualize

#show handwritten digit with show_digit(matriximage$x[n,]),n is any number below 60000.
matriximage<-load_image_file("train-images-idx3-ubyte")
show_digit <- function(arr784, col=gray(12:1/12), ...) {
 image(matrix(arr784, nrow=28)[,28:1], col=col, ...)
}
#Prepare fore prediction visualization
matrixtest<-as.list(load_image_file("t10k-images-idx3-ubyte"))
show_number <- function(arr784, col=gray(12:1/12), ...) {
 image(matrix(arr784, nrow=28)[,28:1], col=col, ...)
}
show_digit(matriximage$x[2017,])
show_number(matrixtest$x[9999,])

2. Visualize certain number of digits to have a big picture of the whole situation.


to.read <- file("train-images-idx3-ubyte", "rb")
readBin(to.read,integer(),n=1,endian="big")
readBin(to.read,integer(),n=1,endian="big")
readBin(to.read,integer(),n=1,endian="big")
readBin(to.read,integer(),n=1,endian="big")
readBin(to.read,integer(),n=1,endian="big")
readBin(to.read,integer(),n=1,endian="big")
to.read <- file("train-images-idx3-ubyte", "rb")
readBin(to.read,integer(),n=4,endian="big")
image(m)
par(mfrow=c(6,6))
par(mar=c(0,0,0,0))
#to see digits with specific number for(i in 1:n)
for(i in 1:16){m = matrix(readBin(to.read,integer(), size=1, n=28*28, endian="big"),28,28)
image(m[,28:1])}

0225201706

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s