"how to read mnist database in r?" Code Answer

1

endian="big", not "high":

> to.read = file("~/downloads/t10k-images-idx3-ubyte", "rb")

magic number:

> readbin(to.read, integer(), n=1, endian="big")
[1] 2051

number of images:

> readbin(to.read, integer(), n=1, endian="big")
[1] 10000

number of rows:

> readbin(to.read, integer(), n=1, endian="big")
[1] 28

number of columns:

> readbin(to.read, integer(), n=1, endian="big")
[1] 28

here comes the data:

> readbin(to.read, integer(), n=1, endian="big")
[1] 0
> readbin(to.read, integer(), n=1, endian="big")
[1] 0

as per the training set image data description on the web site.

now you just need to loop and read 28*28 byte chunks into matrices.

start again:

 > to.read = file("~/downloads/t10k-images-idx3-ubyte", "rb")

skip header:

> readbin(to.read, integer(), n=4, endian="big")
[1]  2051 10000    28    28

should really get the 28,28 from the header read but hard-coded here:

 > m = matrix(readbin(to.read,integer(), size=1, n=28*28, endian="big"),28,28)
 > image(m)

might need to transpose or flip the matrix, i think its an upside-down "7".

par(mfrow=c(5,5))
par(mar=c(0,0,0,0))
for(i in 1:25){m = matrix(readbin(to.read,integer(), size=1, n=28*28, endian="big"),28,28);image(m[,28:1])}

gets you:

oh, and google leads me to: http://www.inside-r.org/packages/cran/darch/docs/readmnist which might be useful.

By Soufiane Boutahlil on February 18 2022

Answers related to “how to read mnist database in r?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.