简体   繁体   English

如何将 numpy.array2string 形成的字符串转换回数组?

[英]How to convert string formed by numpy.array2string back to array?

I have a string of numpy array which is converted by using numpy.array2string我有一个使用numpy.array2string转换的 numpy 数组字符串

Now, I want back my numpy array.现在,我想要回我的 numpy 数组。

Any suggestions for how I can achieve it?关于如何实现它的任何建议?

My Code:我的代码:

img = Image.open('test.png')
array = np.array(img)
print(array.shape)
array_string = np.array2string(array, precision=2, separator=',',suppress_small=True)

PS My array is a 3D array not 1D PS 我的阵列是 3D 阵列而不是 1D

numpy.array2string() gives output string as : '[1, 2]' so you need to remove the braces to get to the elements just separated by some separator. numpy.array2string()给出输出字符串为: '[1, 2]'所以你需要删除大括号才能到达刚刚被一些分隔符分隔的元素。

Here is a small example to extract the list elements from the string by removing the braces and then using np.fromstring() .这是一个通过移除大括号然后使用np.fromstring()从字符串中提取列表元素的小示例。 As you have used ',' as the separator when creating the string, I am using the same to delimit the string for conversion.由于您在创建字符串时使用了','作为分隔符,因此我使用相同的字符来分隔要转换的字符串。

import numpy as np
x = '[1, 2]'
x = x.replace('[','')
x = x.replace(']','')
a = np.fromstring(x, dtype=int, sep=",")
print(a)

#Output: [1 2]

Update: I just tried this and it worked for me:更新:我刚试过这个,它对我有用:

import numpy as np
from PIL import Image

img = Image.open('2.jpg')

arr = np.array(img)

# get shape and type
array_shape = arr.shape
array_data_type = arr.dtype.name
# converting to string
array_string = arr.tostring()

# converting back to numpy array
new_arr = np.frombuffer(array_string, dtype=array_data_type).reshape(array_shape)
print(new_arr)

For converting numpy array to string, I used arr.tostring() instead of arr.array2string() .为了将 numpy 数组转换为字符串,我使用了arr.tostring()而不是arr.array2string() After that converting back to numpy array works with np.frombuffer() .之后转换回 numpy 数组与np.frombuffer()

This is kind of a hack, but may be the simplest solution.这是一种hack,但可能是最简单的解决方案。

import numpy as np

array = np.array([[[1,2,3,4]]]) # create a 3D array
array_string = np.array2string(array, precision=2, separator=',', suppress_small=True)
print(array_string) #=> [[[1,2,3,4]]]

# Getting the array back to numpy
new_array = eval('np.array(' + array_string + ')')

Since the string representation of the array matches the argument we pass to build such array, using eval successfully creates the same array.由于数组的字符串表示与我们传递来构建此类数组的参数相匹配,因此使用 eval 成功创建了相同的数组。

Probably is best if you enclose this in a try except in case the string format isn't valid.如果您将其包含在try则可能是最好的, except字符串格式无效。

import numpy as np

def fromStringArrayToFloatArray(stringArray):
  array = [float(s) for s in stringArray[1:-1].split(' ')]
  return np.array(array)

x = np.array([1.1, 2.2, 3.3, 4.4])
y = np.array2string(x)
z = fromStringArrayToFloatArray(y)
x == z

You can use a list comprehension to split your array into different strings and then, convert them to float (or whatever)您可以使用列表理解将数组拆分为不同的字符串,然后将它们转换为浮点数(或其他)

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

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