【问题标题】:Numpy memmap first rows randomNumpy memmap 第一行随机
【发布时间】:2019-11-11 10:39:42
【问题描述】:

我正在测试np.memmap,因为我需要将它用于大型数据文件。我在 Windows 机器上运行 python 3.7。 我的测试例子很简单。

import numpy as np
arr = np.ones((10**4, 10), dtype=np.float32)
np.save("./arr_test.npy", arr)
data = np.memmap("./arr_test.npy", dtype=np.float32, shape=arr.shape) 
print((data!=1).sum(), data[:30])

输出显示前 32 个不等于 1。

(32, memmap([[2.2366853e+08, 1.2387478e-40, 3.4833497e-15, 4.4898648e+21,
          1.5767864e-19, 2.1442303e-07, 2.2228396e-15, 7.6830766e+31,
          1.7177136e+19, 6.7425655e+22],
         [1.5767864e-19, 1.8727951e+31, 2.2228527e-15, 2.7904159e+29,
          1.5767847e-19, 6.4098282e-10, 1.4584911e-19, 2.4043096e-12,
          1.3593928e-19, 1.3563156e-19],
         [1.3563156e-19, 1.3563156e-19, 1.3563156e-19, 1.3563156e-19,
          1.3563156e-19, 1.3563156e-19, 1.3563156e-19, 1.3563156e-19,
          1.3563156e-19, 1.3563156e-19],
         [1.3563156e-19, 7.7097618e-33, 1.0000000e+00, 1.0000000e+00,
          1.0000000e+00, 1.0000000e+00, 1.0000000e+00, 1.0000000e+00,
          1.0000000e+00, 1.0000000e+00],
         [1.0000000e+00, 1.0000000e+00, 1.0000000e+00, 1.0000000e+00,
          1.0000000e+00, 1.0000000e+00, 1.0000000e+00, 1.0000000e+00,
          1.0000000e+00, 1.0000000e+00],

我错过了什么?

【问题讨论】:

  • "memmap" 适用于原始数据,但 .npy 有一些额外的标头数据。请改用“numpy.lib.format.open_memmap”。
  • 确实解决了!
  • @MichaelButscher 如果您发表评论回答我会接受它

标签: python numpy numpy-memmap


【解决方案1】:

memmap 在没有额外信息的情况下处理原始数据,但 NPY 格式的文件有一个包含有关数据类型、维度等信息的标题。

非零数字是解释为浮点数的标题数据。

函数numpy.lib.format.open_memmap 旨在memmap 现有的NPY 文件或先创建一个新文件。

【讨论】:

    猜你喜欢
    • 2020-10-10
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多