convert string representation of array to numpy array in python
Question:
I can convert a string representation of a list to a list with ast.literal_eval
. Is there an equivalent for a numpy array?
x = arange(4)
xs = str(x)
xs
'[0 1 2 3]'
# how do I convert xs back to an array
Using ast.literal_eval(xs)
raises a SyntaxError
. I can do the string parsing if I need to, but I thought there might be a better solution.
Answers:
For 1D arrays, Numpy has a function called fromstring
, so it can be done very efficiently without extra libraries.
Briefly you can parse your string like this:
s = '[0 1 2 3]'
a = np.fromstring(s[1:-1], dtype=np.int, sep=' ')
print(a) # [0 1 2 3]
For nD arrays, one can use .replace()
to remove the brackets and .reshape()
to reshape to desired shape, or use Merlin’s solution.
Try this:
xs = '[0 1 2 3]'
import re, ast
ls = re.sub('s+', ',', xs)
a = np.array(ast.literal_eval(ls))
a # -> array([0, 1, 2, 3])
if elements of lists are 2D float. ast.literal_eval() cannot handle a lot very complex list of list of nested list.
Therefore, it is better to parse list of list as dict and dump the string.
while loading a saved dump, ast.literal_eval() handles dict as strings in a better way. convert the string to dict and then dict to list of list
k = np.array([[[0.09898942, 0.22804536],[0.06109612, 0.19022354],[0.93369348, 0.53521671],[0.64630094, 0.28553219]],[[0.94503154, 0.82639528],[0.07503319, 0.80149062],[0.1234832 , 0.44657691],[0.7781163 , 0.63538195]]])
d = dict(enumerate(k.flatten(), 1))
d = str(d) ## dump as string (pickle and other packages parse the dump as bytes)
m = ast.literal_eval(d) ### convert the dict as str to dict
m = np.fromiter(m.values(), dtype=float) ## convert m to nparray
Use np.matrix
to convert a string to a numpy matrix. Then, use np.asarray
to convert the matrix to a numpy array with the same shape.
>>> s = "[1,2]; [3,4]"
>>> a = np.asarray(np.matrix(s))
>>> a
array([[1, 2],
[3, 4]])
In contrast to the accepted answer, this works also for two dimensional arrays.
I can convert a string representation of a list to a list with ast.literal_eval
. Is there an equivalent for a numpy array?
x = arange(4)
xs = str(x)
xs
'[0 1 2 3]'
# how do I convert xs back to an array
Using ast.literal_eval(xs)
raises a SyntaxError
. I can do the string parsing if I need to, but I thought there might be a better solution.
For 1D arrays, Numpy has a function called fromstring
, so it can be done very efficiently without extra libraries.
Briefly you can parse your string like this:
s = '[0 1 2 3]'
a = np.fromstring(s[1:-1], dtype=np.int, sep=' ')
print(a) # [0 1 2 3]
For nD arrays, one can use .replace()
to remove the brackets and .reshape()
to reshape to desired shape, or use Merlin’s solution.
Try this:
xs = '[0 1 2 3]'
import re, ast
ls = re.sub('s+', ',', xs)
a = np.array(ast.literal_eval(ls))
a # -> array([0, 1, 2, 3])
if elements of lists are 2D float. ast.literal_eval() cannot handle a lot very complex list of list of nested list.
Therefore, it is better to parse list of list as dict and dump the string.
while loading a saved dump, ast.literal_eval() handles dict as strings in a better way. convert the string to dict and then dict to list of list
k = np.array([[[0.09898942, 0.22804536],[0.06109612, 0.19022354],[0.93369348, 0.53521671],[0.64630094, 0.28553219]],[[0.94503154, 0.82639528],[0.07503319, 0.80149062],[0.1234832 , 0.44657691],[0.7781163 , 0.63538195]]])
d = dict(enumerate(k.flatten(), 1))
d = str(d) ## dump as string (pickle and other packages parse the dump as bytes)
m = ast.literal_eval(d) ### convert the dict as str to dict
m = np.fromiter(m.values(), dtype=float) ## convert m to nparray
Use np.matrix
to convert a string to a numpy matrix. Then, use np.asarray
to convert the matrix to a numpy array with the same shape.
>>> s = "[1,2]; [3,4]"
>>> a = np.asarray(np.matrix(s))
>>> a
array([[1, 2],
[3, 4]])
In contrast to the accepted answer, this works also for two dimensional arrays.