[英]Smallest way to store numpy array with numbers between -255 and 255?
What is the way to store this array that takes up the least amount of memory?这个占用内存最少的数组的存储方式是什么? uint8 doesn't work since some values are negative and int8 doesn't work since some values are above 127. int16 works, but I would rather have it take up less space. uint8 不起作用,因为某些值是负数,而 int8 不起作用,因为某些值高于 127。int16 起作用,但我宁愿让它占用更少的空间。
Should I not have it as a numpy array and just store it as a regular python list?我不应该把它作为一个 numpy 数组而只是将它存储为一个常规的 python 列表吗?
This is the array (i'm only including the first few lines, if you want the entire array let me know)这是数组(我只包括前几行,如果您想要整个数组,请告诉我)
array([[[ 218, 219, 223],
[ 0, 0, 0],
[ 2, 2, 2],
[ 1, 1, 1],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ -3, -3, -3],
[ -1, -1, -1],
[ 0, 0, 0],
[ -1, -1, -1],
[ 0, 0, 0]]], dtype=int16)
I tried我试过
import numpy as np
a = np.array([[[ 218, 219, 223],
[ 0, 0, 0],
[ 2, 2, 2],
[ 1, 1, 1],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ -3, -3, -3],
[ -1, -1, -1],
[ 0, 0, 0],
[ -1, -1, -1],
[ 0, 0, 0]]], dtype=np.int16)
signs = a<0.astype(np.bool)
a=a.astype(np.uint8)
But found it is actually worse than the original(206 bytes) vs. 167 for signs and 167 for the uint8 array.但发现它实际上比原始(206 字节)差,而符号为 167,uint8 数组为 167。
The boolean seems to be taking as much as the uint8 - after looking into it I found that packbits will finally get you somewhere -布尔值似乎与 uint8 一样多 - 在调查之后我发现 packbits 最终会让你到达某个地方 -
signs2 = np.packbits(signs, axis=None)
although at 106 bytes for the packed bytes, the uint8+signs still loses out to the original int16.尽管打包字节为 106 个字节,但 uint8+signs 仍然输给了原始的 int16。 Those sizes are as reported by sys.getsizeof();这些大小由 sys.getsizeof() 报告; if you use len(x.tostring()) you will find 78 bytes for the original array, 39 for the unsigned 8bit array, 39 for the boolean signs, and 5 for the packed signs.如果你使用 len(x.tostring()) 你会发现原始数组有 78 个字节,无符号 8 位数组有 39 个字节,布尔符号有 39 个字节,压缩符号有 5 个字节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.