database - 如何在 R 中读取 MNIST 数据库?

标签 database r file-io

我目前正在做一个案例研究,我需要为 MNIST database 工作.
this site 中的文件据说是 IDX 文件格式。我尝试使用记事本和写字板等基本文本编辑器查看这些文件,但没有成功。
预计它们将采用高端格式,我尝试了以下操作:

to.read = file("t10k-images.idx3-ubyte", "rb")
readBin(to.read, integer(), n=100, endian = "high")

我得到了一些数字作为输出,但它们对我来说没有任何意义。

谁能解释一下如何在 R 中读取 MNIST 数据库文件以及如何解释这些数字?谢谢。

最佳答案

endian="big",不是"high":

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

魔数(Magic Number):

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

图片数量:

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

行数:

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

列数:

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

数据来了:

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

根据网站上的训练集图像数据描述。

现在你只需要循环并将 28*28 字节的 block 读入矩阵。

重新开始:

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

跳过标题:

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

应该真正从 header 中读取 28,28,但在此处进行了硬编码:

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

可能需要转置或翻转矩阵,我认为它是一个倒置的“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])}

让你:

enter image description here

哦,谷歌引导我到:http://www.inside-r.org/packages/cran/darch/docs/readMNIST这可能有用。

关于database - 如何在 R 中读取 MNIST 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21521571/

相关文章:

database - 考虑夏令时将(日期)类型的oracle历史数据转换为unix时间戳

php - 将 unix_timestamp 插入 mysql 数据库

database - 调试时如何在 TOAD 中查看 CLOB 变量的完整值

r - ggplot2 scale_color_manual 显示图例中的所有值

winapi - I/O 完成端口 vs. RegisterWaitForSingleObject?

java - 利用庞大的数据库提高 SQLite 性能

r - 在 geom_histogram 中指定精确的 bin 范围

r - 使用 lpSolveAPI 获取 0/1-Knapsack MILP 的多个解决方案

c - 如何读取文件直到C中的一个字符

java - 不使用扫描仪将文本数据存储到java数组中