简体   繁体   English

将字符串ndarray转换为ndarray

[英]Convert a string of ndarray to ndarray

I have a string of ndarray. 我有一串ndarray。 I want to convert it back to ndarray. 我想将其转换回ndarray。 I tried newval = np.fromstring(val, dtype=float) . 我尝试过newval = np.fromstring(val, dtype=float) But it gives ValueError: string size must be a multiple of element size 但是它给出了ValueError: string size must be a multiple of element size

Also I tried newval = ast.literal_eval(val) . 我也尝试过newval = ast.literal_eval(val) This gives 这给

File "<unknown>", line 1
[-1.45181984e-01  1.51671678e-01  1.59053639e-01 -1.02861412e-01
                               ^
SyntaxError: invalid syntax

String of ndarray ndarray的字符串

 '[-1.45181984e-01  1.51671678e-01  1.59053639e-01 -1.02861412e-01
 -9.70948339e-02 -1.75551832e-01 -7.24434480e-02  1.19182713e-01
 -4.54084426e-02 -9.23779532e-02  8.87222588e-02  1.05331177e-02
 -1.31792471e-01  3.50326337e-02 -6.58577830e-02  1.02670217e+00
 -5.29987812e-02  2.09167395e-02 -1.19845152e-01  2.30511073e-02
  2.89404951e-02  4.17387672e-02 -2.08203331e-01  2.34342851e-02]'

How can I convert this back to ndarray? 如何将其转换回ndarray?

To expand upon my comment: 扩展我的评论:

If you're trying to parse a human-readable string representation of a NumPy array you've acquired from somewhere, you're already doing something you shouldn't. 如果您试图解析从某处获取的NumPy数组的可读字符串表示形式,那么您已经在做不应该做的事情。

Instead use numpy.save() and numpy.load() to persist NumPy arrays in an efficient binary format. 而是使用numpy.save()numpy.load()将NumPy数组持久numpy.load()有效的二进制格式。

Maybe use .savetxt() if you need human readability at the expense of precision and processing speed... but never consider str(arr) to be something you can ever parse again. 如果您需要人类可读性,而又以精度和处理速度为代价,则可以使用.savetxt() ...,但是永远不要认为str(arr)是您可以再次解析的东西。

However, to answer your question, if you're absolutely desperate and don't have a way to get the array into a better format... 但是,要回答您的问题,如果您绝对绝望并且没有办法将数组转换为更好的格式...

>>> data = '''
... [-1.45181984e-01  1.51671678e-01  1.59053639e-01 -1.02861412e-01
...  -9.70948339e-02 -1.75551832e-01 -7.24434480e-02  1.19182713e-01
...  -4.54084426e-02 -9.23779532e-02  8.87222588e-02  1.05331177e-02
...  -1.31792471e-01  3.50326337e-02 -6.58577830e-02  1.02670217e+00
...  -5.29987812e-02  2.09167395e-02 -1.19845152e-01  2.30511073e-02
...   2.89404951e-02  4.17387672e-02 -2.08203331e-01  2.34342851e-02]
... '''.strip()
>>> list_of_floats = [float(x) for x in data.strip('[]').split(None)]
[-0.145181984, 0.151671678, 0.159053639, -0.102861412, -0.0970948339, -0.175551832, -0.072443448, 0.119182713, -0.0454084426, -0.0923779532, 0.0887222588, 0.0105331177, -0.131792471, 0.0350326337, -0.065857783, 1.02670217, -0.0529987812, 0.0209167395, -0.119845152, 0.0230511073, 0.0289404951, 0.0417387672, -0.208203331, 0.0234342851]

EDIT: For the case OP mentioned in the comments, 编辑:对于案例中提到的OP,

I am storing these arrays in LevelDB as key value pairs. 我将这些数组作为键值对存储在LevelDB中。 The arrays are fasttext vectors. 数组是快速文本向量。 In levelDB vector (value) for each ngram (key) are stored. 在levelDB中,存储了每个ngram(关键字)的向量(值)。 Is what you mentioned above applicable here? 您上面提到的内容在这里适用吗?

Yes – you'd use BytesIO from the io module to emulate an in-memory "file" NumPy can write into, then put that buffer into LevelDB, and reverse the process (read from LevelDB into an empty BytesIO and pass it to NumPy) to read: 是的–您将使用io模块中的BytesIO来模拟NumPy可以写入的内存中“文件”,然后将该缓冲区放入LevelDB中,并逆转该过程(从LevelDB中读取到一个空的BytesIO中并将其传递给NumPy)读书:

bio = io.BytesIO()
np.save(bio, my_array)
ldb.put('my-key', bio.getvalue())
# ...
bio = io.BytesIO(ldb.get('my-key'))
my_array = np.load(bio)

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

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