简体   繁体   English

存储数字在 -255 到 255 之间的 numpy 数组的最小方法?

[英]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.

相关问题 规范从-1,1到0,255的图像的numpy数组 - Normalising numpy array of images from -1, 1 to 0,255 使用NumPy将ubyte [0,255]数组转换为float数组[-0.5,+0.5]的最快方法 - Fastest way to convert ubyte [0, 255] array to float array [-0.5, +0.5] with NumPy 显示 numpy 数组由 rgb (255,255,255) 作为图像组成,为什么它输出纯黑色而不是纯白色? - Show numpy array consists of rgb (255,255,255) as image, why it output pure black instead of pure white? 将 numpy 数组从 -0.1 - 0.2 缩放到 0-255 - Scale a numpy array with from -0.1 - 0.2 to 0-255 尝试将 numpy 图像数组除以 255 时出现空白 Memory 错误 - Blank Memory error when trying to divide the numpy image array by 255 numpy.astype(np.uint8) 如何转换浮点数组? -1.2997805 变成 255 - how does numpy.astype(np.uint8) convert a float array? -1.2997805 became 255 Python OpenCV/NumPy:为什么 np.uint8([255]) + np.uint8([255]) 给出的结果是 254 而不是 255? - Python OpenCV/NumPy: why np.uint8([255]) + np.uint8([255]) is giving result 254 instead of 255? 如何将数组值0和255转换为对应的0和1数组 - How to convert array values 0 and 255 to corresponding 0 and 1 array 零数组减 1 给出 255 数组? - array of zeros minus 1 gives array of 255? 如果此数组中的值可以介于0到255之间,则如何在Python中生成2d数组的所有可能组合 - How to generate all possible combinations of a 2d array in Python, if a value in this array can be between 0 and 255
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM