简体   繁体   English

有没有办法将 numpy unicode 数组加载到 memmap 中?

[英]Is there a way to load a numpy unicode array into a memmap?

I am trying to create an array of dtype='U' and saving that using numpy.save() , however, when trying to load the saved file into a numpy.memmap I get an error related to the size not being a multiple of 'U3'我正在尝试创建一个numpy.save() dtype='U'数组并使用numpy.save()保存它,但是,当尝试将保存的文件加载到numpy.memmap我收到一个与大小不是倍数相关的错误'U3'

I am working with python 3.5.2 .我正在使用python 3.5.2 I have tried the following code where I am creating an empty array and another array with 3 entries, all with length of 3 letters and then save the array into file1.npy file.我尝试了以下代码,其中我创建了一个空数组和另一个包含 3 个条目的数组,所有条目的长度均为 3 个字母,然后将数组保存到file1.npy文件中。

import numpy as np
arr = np.empty((1, 0), dtype='U')
arr2 = np.array(['111', '222', '333'], dtype='U')
arr = np.concatenate((arr, arr2), axis = None)
print(arr)
np.save('file1', arr)

rArr = np.memmap('file1.npy', dtype='U3', mode='r')

However, when I try to load the file into a numpy.memmap I get the the following error ValueError: Size of available data is not a multiple of the data-type size.但是,当我尝试将文件加载到numpy.memmap ,出现以下错误ValueError: Size of available data is not a multiple of the data-type size.

Is there a way to load the data into a numpy.memmap using strings?有没有办法使用字符串将数据加载到numpy.memmap中? I feel I am missing something simple.我觉得我错过了一些简单的东西。

The files used by numpy.memmap are raw binary files, not NPY-format files. numpy.memmap使用的文件是原始二进制文件,而不是NPY 格式的文件。 If you want to read a memory-mapped NPY file, use numpy.load with the argument mmap_mode='r' (or whatever other value is appropriate).如果要读取内存映射 NPY 文件,请使用numpy.load和参数mmap_mode='r' (或任何其他合适的值)。

After creating 'file1.npy' like you did, here's how it can be memory-mapped with numpy.load :像您一样创建“file1.npy”后,以下是如何使用numpy.load对其进行内存映射:

In [16]: a = np.load('file1.npy', mmap_mode='r')                                                                       

In [17]: a                                                                                                             
Out[17]: memmap(['111', '222', '333'], dtype='<U3')

Looks like np.load is your friend here.看起来np.load是你的朋友。

Doc 文件

Issue问题

The following snippet works for me:以下代码段对我有用:

rArr = np.load('file1.npy', mmap_mode='r')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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