具有 1 位条目的 numpy 布尔数组 [英] numpy boolean array with 1 bit entries

查看:31
本文介绍了具有 1 位条目的 numpy 布尔数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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).

如果你从一个文件创建一个 ConstBitArrayConstBitStream 那么它将使用 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 布尔数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆