Numpy从二进制字符串转换为浮点数组

时间:2020-12-13 21:41:30

I have a feature vector of length 16 stored in mysql as a BLOB and I'm fetching that BLOB in python. It is fetched as a binary string. I'm currently converting it to a array first using:

我有一个长度为16的特征向量存储在mysql中作为BLOB,我在python中获取BLOB。它被取为二进制字符串。我目前正在使用以下方法将其转换为数组:

list(map(lambda x: list(map(float, x['encoded_vals'].split(b','))), 
    visual_features))

My dict looks something like this:

我的字典看起来像这样:

{img_id: 1, encoded_vals: b'0.99451257448,0.8541256468...'} # Till 16 values

Is there a way to convert this string to numpy array without using list(map)?

有没有办法将此字符串转换为numpy数组而不使用list(map)?

1 个解决方案

#1


1  

You can use:

您可以使用:

import numpy as np
np.fromstring(text, sep=',')

There is no need to split it by b',' you can provide it through sep. It works as it is with bytes, but to be more precise, you should use text.decode('ascii').

没有必要将它拆分为b','你可以通过sep提供它。它与字节一样工作,但更确切地说,你应该使用text.decode('ascii')。

#1


1  

You can use:

您可以使用:

import numpy as np
np.fromstring(text, sep=',')

There is no need to split it by b',' you can provide it through sep. It works as it is with bytes, but to be more precise, you should use text.decode('ascii').

没有必要将它拆分为b','你可以通过sep提供它。它与字节一样工作,但更确切地说,你应该使用text.decode('ascii')。