简体   繁体   中英

Why does outputing numpy.dot to memmap does not work?

If I do:

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)

I am getting:

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

When I replace c with:

c = np.zeros((10,10))

it works.

What am I doing wrong?

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.

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.

From RKI's comment, use of numpy.asarray works directly, eg:

a = np.ones((10,1))
b = np.ones((10,1))

c_memmap = np.memmap('zeros.mat', dtype=np.float64, mode='w+', shape=(10,10), order='C')
c = numpy.asarray(c_memmap)

a.dot(b.T, out=c)

c_memmap.flush()
#etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM