python - 当我尝试 reshape numpy 数组时,为什么 memmap 需要文件名?

标签 python arrays numpy pickle

我得到了一个我觉得很难理解的堆栈跟踪:

screwed_up_code.py in atleast_4d(arr)
     28 def atleast_4d(arr):
     29     stshape = arr.shape
     30     while len(stshape)<4: stshape+=(1,)
     31     print arr.shape, stshape
---> 32     return arr.reshape(stshape)

/usr/lib/python2.7/dist-packages/numpy/core/memmap.pyc in __array_finalize__(self, obj)
    255         if hasattr(obj, '_mmap'):
    256             self._mmap = obj._mmap
--> 257             self.filename = obj.filename
    258             self.offset = obj.offset
    259             self.mode = obj.mode

AttributeError: 'memmap' object has no attribute 'filename'

如果您想知道 arr.shape = (192, 384, 6)stshape = (192, 384, 6, 1)


更新

根据 NPE 的建议,我调查了 the bug report of a similar sounding AttributeError 。那里的一张海报将其归咎于由于 ndarray 的 pickle 而导致属性丢失。我确实正在 pickle 数组,当重新激活加载的数组时,如下所示:

newarr = numpy.ndarray(pickled_array)
pickled_array = newarr                  # use the recreated instead of the pickled arr

我收到警告而不是异常,并且我的代码运行:

Exception AttributeError: AttributeError("'NoneType' object has no attribute 'tell'",) in  ignored
Exception AttributeError: AttributeError("'NoneType' object has no attribute 'tell'",) in  ignored
Exception AttributeError: AttributeError("'NoneType' object has no attribute 'tell'",) in <bound method memmap.__del__ of memmap([  85389.2734375,  125935.75     ,  173624.09375  ,  272958.78125  ,
        305687.65625  ,  433026.3125   ], dtype=float32)> ignored

我很高兴我的代码能够运行,并且暂时保留它。

最佳答案

这听起来与 this bug 非常相似.

stack trace有一点不同,但代码最终在完全相同的点失败,并出现完全相同的异常:

 File "C:\Python26\Lib\site-packages\numpy\core\memmap.py", line 257, in __array_finalize__
   self.filename = obj.filename
AttributeError: 'memmap' object has no attribute 'filename'

numpy-discussion thread首次报告此问题的位置表明该问题可能与 this other ticket 有关。与 memmap 对象的 pickle 有关。无论如何,该主题都值得一读。

关于python - 当我尝试 reshape numpy 数组时,为什么 memmap 需要文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15986133/

相关文章:

Python sed 正则表达式匹配

python - Django 创建自定义用户模型

javascript - Python for 括号内循环语法

javascript - 如何使用 Jquery onclick 获取数组的下一个元素

matlab:不循环不同大小子数组的总和

java - 模拟世界游戏的数据结构

python - 堆叠具有重叠索引的数组。寻找循环上的矢量化方法

python - 在模型级别将 Django DateField 验证为一周中的特定日期的最佳做法是什么?

python - tensorflow:如何喂养 numpy.ndarray?

python - 列出 : 与::的使用