将 numpy.array 编码和解码为 base64。为什么需要使用 numpy.frombuffer?

兰德森

请考虑以下代码:

import base64
import numpy as np

array = np.array([1,2]).astype('float32')

arrayencode64 = base64.b64encode(array)

arraydecode64 = base64.b64decode(arrayencode64)

ARRAY = np.frombuffer(arraydecode64, dtype='float32')

print(array)
print()
print(arrayencode64)
print()
print(arraydecode64)
print()
for el in arraydecode64:
    print(el)
print()
print(ARRAY)

问题 1:我想知道为什么在应用base64.b64decode 后,我没有检索到原始对象?我的意思是,为什么需要使用指定类型的np.frombuffer读取它

因为,我期望在应用base64.b64decode之后立即获得目标对象(绑定在数组变量中的numpy.array)

问题 2:当我for 循环打印变量arraydecode64 时我得到了一个数字序列。它们究竟是什么意思?

谢谢。

多米尼克·加米尔

根据文档base64.b64decode所做的只是获取一个字符串并返回一个字节对象。np.frombuffer然后获取这些字节并创建数组。base64不知道如何numpy表示字节,也不知道数组是什么。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

NumPy-frombuffer和fromstring有什么区别?

如何在base64字符串和numpy数组之间进行编码和解码?

将 javascript 文件或 blob 编码和解码为 base64

numpy.memmap到Image.frombuffer-无需复制

numpy fromstring已弃用,请改用frombuffer

numpy.frombuffer ValueError:缓冲区小于请求的大小

使用numpy将数据分组为类

numpy.array.tolist() 将 numpy.datetime64 转换为 int

如何使用 librosa 将 .ogg opus 解码为 int16 NumPy 数组?

在将图像编码和解码到base64中时,java内存不足异常

将.EXE编码/解码为Base64

用于复杂numpy数组的Json编码器和解码器

PHP 将文本编码和解码为 ASCII 字符

将布尔numpy数组的行编码为字节

numpy frombuffer-AttributeError:“ str”对象没有属性“ __buffer__”

使用buildozer将numpy库打包为Kivy与numpy库时出错

使用pandas,numpy或其他将numpy数组连接为两个数组

大量使用numpy.array

将包含numpy数组的元组展平为numpy数组

如何使用GWT将短字符串编码/解码为Base64?

使用 jackson 和 spring-boot 将 base64 编码的 JSON 解码为 POJO

numpy数组-使用整形将多列堆叠为一

使用值数组将numpy分组为多个子数组

无法使用dtype = np.float64将pandas.Series转换为numpy.array

将文件中的一行读取为 numpy.array 的一行脚本

如果可以轻松解码,为什么要使用 base64 编码字符串

如何将对象编码和解码为 Base64?

将numpy.array a中的每个元素与numpy.array b中的每个元素相乘

将 numpy.array a 与 numpy.array b 的前导维度相乘