I have the following array:
pattern = array([['[0, 0, 1, 0, 0]'],
['[0, 1, 1, 1, 1]'],
['[0, 1, 1, 1, 0]'],
['[0, 0, 1, 1, 1]'],
['[0, 0, 0, 1, 1]'],
['[0, 0, 1, 0, 1]'],
['[0, 0, 0, 0, 1]'],
['[1, 0, 1, 0, 0]'],
['[0, 1, 0, 1, 1]'],
['[0, 0, 1, 1, 0]'],
['[1, 1, 1, 1, 1]'],
['[1, 1, 1, 1, 0]']], dtype='<U15')
and I want to get it in non-string format as the following:
import numpy
my_array = numpy.array([[0, 0, 1, 0, 0],
[0, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 1, 1, 1],
[0, 0, 0, 1, 1],
[0, 0, 1, 0, 1],
[0, 0, 0, 0, 1],
[1, 0, 1, 0, 0],
[0, 1, 0, 1, 1],
[0, 0, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 0]
])
Any idea on how to do it non-manually?
CodePudding user response:
You can use the ast.literal_eval() function from the ast module to evaluate the strings in the array as Python literals and convert them to lists. Then you can use the numpy.array() function to create a NumPy array from the lists.
Here's an example of how you can do this:
import ast
import numpy
pattern = [['[0, 0, 1, 0, 0]'],
['[0, 1, 1, 1, 1]'],
['[0, 1, 1, 1, 0]'],
['[0, 0, 1, 1, 1]'],
['[0, 0, 0, 1, 1]'],
['[0, 0, 1, 0, 1]'],
['[0, 0, 0, 0, 1]'],
['[1, 0, 1, 0, 0]'],
['[0, 1, 0, 1, 1]'],
['[0, 0, 1, 1, 0]'],
['[1, 1, 1, 1, 1]'],
['[1, 1, 1, 1, 0]']
]
# Evaluate the strings as Python literals and convert them to lists
pattern = [ast.literal_eval(x[0]) for x in pattern]
# Create a NumPy array from the lists
pattern = numpy.array(pattern)
print(pattern)
This will output the following NumPy array:
array([[0, 0, 1, 0, 0],
[0, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 1, 1, 1],
[0, 0, 0, 1, 1],
[0, 0, 1, 0, 1],
[0, 0, 0, 0, 1],
[1, 0, 1, 0, 0],
[0, 1, 0, 1, 1],
[0, 0, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 0]])
CodePudding user response:
Using numpy string operations to strip brackets ([]
), splitting on comma and recast into an array with int
dtype is possible:
np.array(np.char.split(np.char.strip(pattern[:, 0], '[]'), ', ').tolist(), 'int')
but a list comprehension where you do the same things using python string methods is much easier to read (and faster as well) imo.
np.array([row[0][1:-1].split(', ') for row in pattern], dtype='int')
# array([[0, 0, 1, 0, 0],
# [0, 1, 1, 1, 1],
# [0, 1, 1, 1, 0],
# [0, 0, 1, 1, 1],
# [0, 0, 0, 1, 1],
# [0, 0, 1, 0, 1],
# [0, 0, 0, 0, 1],
# [1, 0, 1, 0, 0],
# [0, 1, 0, 1, 1],
# [0, 0, 1, 1, 0],
# [1, 1, 1, 1, 1],
# [1, 1, 1, 1, 0]])