简体   繁体   English

如何在numpy中创建一个布尔数组

[英]How to create a Boolean array in numpy

I have to deal with a large data (300 by 4 by 1400000 integer array) which is around我必须处理一个大数据(300 x 4 x 1400000 整数数组),它大约在

4byte * 300 * 4 * 1400000 = 6.72GB

However, this array only contains 0 or 1. So if I could use a boolean array, then the size would shrink to 6.25% of the original data.然而,这个数组只包含 0 或 1。所以如果我可以使用一个布尔数组,那么大小将缩小到原始数据的 6.25%。

6.72GB / 4bytes / 8bits/bytes * 2 = 420Mbits 

Is there any way to make use of boolean arrays in numpy?有没有办法在 numpy 中使用布尔数组?

EDIT: I don't know why he deleted his answer but this did exactly what I wanted.编辑:我不知道他为什么删除了他的答案,但这正是我想要的。

arr = np.ones((300,2,1400000), dtype = np.bool)

This lead to 12.5% compression.这导致 12.5% 的压缩。

>>> arr = np.ones((300,2,1400000), dtype = np.bool)
>>> arr.nbytes
840000000

>>> arr = np.ones((300,2,1400000))
>>> arr.nbytes
6720000000

840000000/6720000000 = 12.5%

While there is a way to manipulate 'bit fields' in numpy these don't offer the conveniences of proper numpy arrays on a bit level.虽然有一种方法可以在numpy 中操作“位字段”,但它们并没有在位级别上提供适当的 numpy 数组的便利。

That said, numpy does have "logical" or "boolean" arrays, ie arrays with dtype bool.也就是说,numpy 确实有“逻辑”或“布尔”数组,即带有 dtype bool 的数组。 These take only one byte per element and are proper arrays.这些每个元素只需要一个字节,并且是正确的数组。 When your array is created by a "logical array operation" like, say, b = (a > 0) it ( b ) will be automatically of bool type.当您的数组由“逻辑数组操作”创建时,例如b = (a > 0)它 ( b ) 将自动为 bool 类型。 You can obtain boolean arrays by the standard numpy ways a.astype(bool), array(..., dtype=bool) etc.您可以通过标准的 numpy 方式 a.astype(bool), array(..., dtype=bool) 等获取布尔数组。

这将使您减少 75%,每个项目四个字节到每个项目一个字节。

bool_array = np.logical_and(int_array, True)

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

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