为什么将numpy.dot输出到memmap不起作用? [英] Why does outputing numpy.dot to memmap does not work?
问题描述
如果我这样做:
a = np.ones((10,1))
b = np.ones((10,1))
c = np.memmap('zeros.mat', dtype=np.float64, mode='w+', shape=(10,10), order='C')
a.dot(b.T, out=c)
我得到了:
ValueError:输出数组不可接受(必须具有正确的类型, 尺寸,并成为C数组)
ValueError: output array is not acceptable (must have the right type, nr dimensions, and be a C-Array)
我检查了错误消息中的所有条件,这些条件似乎很合适:
I check all conditions from the error message and they seem to fit:
>>> print(a.dtype == b.dtype == c.dtype)
>>> print(np.dot(a, b.T).shape == c.shape)
>>> print(c.flags['C_CONTIGUOUS'])
True
True
True
当我将c替换为:
c = np.zeros((10,10))
有效.
我在做什么错了?
推荐答案
它不仅需要与dtype相匹配;还可以与dtype相匹配.如type(c)
一样,它还必须具有正确的 type . c
是numpy.memmap
实例,而不是numpy.ndarray
,因此检查失败.
It doesn't just have to match the dtype; it also has to have the right type, as in type(c)
. c
is a numpy.memmap
instance, not a numpy.ndarray
, so that check fails.
如 numpy.memmap
文档,您可以改为使用mmap.mmap
映射文件并创建由mmap支持的numpy.ndarray
作为其缓冲区.您可以查看 numpy.memmap
实现看看这样做可能涉及什么.
As recommended in the numpy.memmap
docs, you could instead use mmap.mmap
to map the file and create a numpy.ndarray
backed by the mmap as its buffer. You can look at the numpy.memmap
implementation to see what might be involved in doing that.
这篇关于为什么将numpy.dot输出到memmap不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!