13

Sometimes the printed numpy array is provide to share the data such as this post. So far, I converted that manually. But the array in the post is too big to convert by hand.

I want to convert a string representation of a numpy array back to an array. (Thanks, @LevLevitsky. I reference your expression.)

I tried this code

import numpy as np

print np.array([[0, 1], [2, 3]])
#[[0 1]
# [2 3]]

# the output is
output = '''[[0 1]
 [2 3]]'''

import re
pat_ignore = re.compile(r'[\[\]]')
numbers = pat_ignore.sub('', output)
print np.array([map(float, line.split()) for line in numbers.splitlines()])
[[ 0.  1.]
 [ 2.  3.]]

However, this could not retain the data type. Also if ndim > 3, It does not work properly.

[[[0 1]
  [2 3]]]

is interpreted as

[[ 0.  1.]
 [ 2.  3.]]
5
  • I don't understand the question. You want to see the entire numpy array printed out on the screen, regardless of the size? You want to make sure the data type is preserved? Commented May 28, 2014 at 14:41
  • 1
    @mauve The OP wants to convert a string representation of a numpy array back to an array, AFAICT. Commented May 28, 2014 at 14:42
  • 1
    Well, you have to add some logic that counts the number of open parens in the beginning and derives array dimensions from there. And then depending on the dimensions, you would need to do some additional parsing to make sure you can handle arrays with higher dimensions. Commented May 28, 2014 at 14:52
  • 2
    This answer shows a similar approach, with re.sub and then ast.literal_eval. Commented May 28, 2014 at 14:54
  • For people sharing printed numpy array, please either use pretty print (from pprint import pprint) to get printed numpy array or at least convert it to a list like np.arange(4).reshape((2,2)).tolist(). Commented May 28, 2014 at 17:39

3 Answers 3

14

You can use re to treat the string and then create the array using eval():

 import re
 from ast import literal_eval

 import numpy as np

 a = """[[[ 0 1]
          [ 2 3]]]"""
 a = re.sub(r"([^[])\s+([^]])", r"\1, \2", a)
 a = np.array(literal_eval(a))
Sign up to request clarification or add additional context in comments.

7 Comments

Obligatory note: eval is unsafe and should be avoided. Use ast.literal_eval instead.
If numpy uses float point numbers, it (sometimes?) inserts a space between the leading [ and the first number. With this code snippet, this space will be converted to a comma, which will make the value invalid.
@max do you have a solution for this problem?
@max I added a better regex for this case
Works but uses extreme memory size for 400 MB of printed text (144x72x3x1024 float numbers). Had to parse the outer layer manually and then use your code on 144x72x3 matrices only.
|
0

The other reply works great, but some shortcuts can be taken if the values are numeric. Furthermore, you may have an array with many dimension and even many orders. Given npstr, your str(np.array):

import re, json
import numpy as np

# 1. replace those spaces and newlines with commas.
# the regex could be '\s+', but numpy does not add spaces.
t1 = re.sub('\s',',',npstr)
# 2. covert to list
t2 = json.loads(t1)
# 3. convert to array
a = np.array(t2)

In a single line (bad form sure, but good for copypasting):

a = np.array(json.loads(re.sub('\s',',',npstr)))

1 Comment

Possibly because numpy is adding more whitespace padding than it used to, I had to change the regex to (?<=\d|])\s+(?=\d|\[) (the extra padding might be because I am using long floats with varying length, and numpy aligns the decimal point as well as the numbers).
0

Here's a solution that worked for me for multi-dimensional arrays with ints, floats and scientific notation:

import re
import numpy as np
import json

def np_repr_to_arr(s):
  s_with_commas = re.sub(r'([\d\]])\s+(-?[\d\[])', r'\1, \2', s.strip())
  return np.array(json.loads(s_with_commas))

it inserts commas only between square brackets and between numbers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.