问题描述
在 numpy 中有没有办法创建一个布尔数组,每个条目只使用 1 位?
Is there a way in numpy to create an array of booleans that uses just 1 bit for each entry?
标准的 np.bool 类型是 1 字节,但是这样我使用了 8 倍的所需内存.
The standard np.bool type is 1 byte, but this way I use 8 times the required memory.
在 Google 我发现 C++ 有 std::vector<bool>.
On Google I found that C++ has std::vector<bool>.
推荐答案
你不妨看看 bitstring(文档这里).
You might like to take a look at bitstring (documentation here).
如果你从一个文件创建一个 ConstBitArray 或 ConstBitStream 那么它将使用 mmap 而不会将它加载到内存中.在这种情况下,它不会是可变的,因此如果您想进行更改,则必须将其加载到内存中.
If you create a ConstBitArray or ConstBitStream from a file then it will use mmap and not load it into memory. In this case it won't be mutable so if you want to make changes it will have to be loaded in memory.
例如创建而不加载到内存中:
For example to create without loading into memory:
>>> a = bitstring.ConstBitArray(filename='your_file')
或
>>> b = bitstring.ConstBitStream(a_file_object)
这篇关于具有 1 位条目的 numpy 布尔数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)