我正在使用依赖readBin()
的函数将MNIST图像文件读入R。但是,逐行运行该函数,我发现readBin()
对于同一行代码返回不同的值(不更改任何参数)。怎么会来?
#Getting the data
> download.file("http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz",
+ "t10k-images-idx3-ubyte.gz")
#unzipped the .gz file manually out of R. The extracted file is 'train-images.idx3-ubyte'
#Using file() to read the 'train-images.idx3-ubyte' file
> f = file("train-images.idx3-ubyte", 'rb')
#this is what 'f' is:
> f
A connection with
description "train-images.idx3-ubyte"
class "file"
mode "rb"
text "binary"
opened "opened"
can read "yes"
can write "no"
#The following lines show the execution of readBin with the same parameters, though giving a different value each time
> readBin(f, 'integer', n = 1, size = 4, endian = 'big')
[1] 2051
> readBin(f, 'integer', n = 1, size = 4, endian = 'big')
[1] 60000
> readBin(f, 'integer', n = 1, size = 4, endian = 'big')
[1] 28
> readBin(f, 'integer', n = 1, size = 4, endian = 'big')
[1] 28
> readBin(f, 'integer', n = 1, size = 4, endian = 'big')
[1] 0
答案 0 :(得分:0)
您打开file
连接,然后再关闭它。因此,结果就是您所经历的。您只需阅读下一个号码即可。
重复序列
将为您提供一致的结果。
或者也使用readBin("the_file__not_the_connection", 'integer', n = 1, size = 4, endian = 'big')
。