4

I have stringified ndarray passed from server to the client-side, the example of that array could be seen below

import numpy as np

str_array = "[[0.1233 0.333 4.1111] [0.1233 0.333 4.1111] [0.1233 0.333 4.1111]]"
arr = np.fromstring(str_array, dtype=np.float32, sep = ' ')

print(arr)

when I run that code, it would raise an error message:

  File "example.py", line 89, in <module>
    arr = np.fromstring(str_array, dtype=np.float32)
ValueError: string size must be a multiple of element size

I want my stringified array to become a ndarray again. How can I solve this?

1
  • That stringified ndarray is not designed for parsing like this. Don't build this process into your code. Commented Mar 19, 2020 at 10:50

3 Answers 3

3

Use numpy.matrix and than reshape

>>> np.matrix(str_array).reshape(-1,3)
matrix([[0.1233, 0.333 , 4.1111],
        [0.1233, 0.333 , 4.1111],
        [0.1233, 0.333 , 4.1111]])

Or to get ndarray use attribute matrix.A

>>> np.matrix(str_array).reshape(-1,3).A
array([[0.1233, 0.333 , 4.1111],
       [0.1233, 0.333 , 4.1111],
       [0.1233, 0.333 , 4.1111]])
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not looking to diss a 'competing' answer, but I note that the documentation you link to says "It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future."
2

I note that the documentation says that np.fromstring() "Return a new 1-D array initialized from raw binary or text data in string."

If you know the dimensions of your array, one simple workaround for the fact your data is 2D would simply to be:

str_array = str_array.replace("]", "")
str_array = str_array.replace("[", "")
np.fromstring(str_array, sep=' ').reshape(3,3)

Which does yield:

array([[0.1233, 0.333 , 4.1111],
       [0.1233, 0.333 , 4.1111],
       [0.1233, 0.333 , 4.1111]])

Comments

1

Here is a recursive solution. First, use regex, because the native re does not include recursive regular expressions.

import regex
import numpy as np

Then, utilize that you can easily split the brackets if you exclude the outermost brackets (first and last place in your string). Next, construct arrays recursively, only using the last instance without brackets to start the numpying process:

def str_to_np_ndarray(bracketed_string:str, sep:str) -> np.ndarray:
    bracketed_string = bracketed_string[1:-1]
    if "[" in bracketed_string:
        split = regex.findall(r'\[(?:[^][]|(?R))*\]', bracketed_string)
        return np.array([str_to_np_ndarray(bracketed_string, sep) for bracketed_string in split])
    else:
        return np.fromstring(bracketed_string, sep=sep)

Finally:

str_to_np_ndarray("[[[[ 6 2 2]\n [ 4 11 2]]\n [[ 6 2 2]\n [ 4 11 2]]]\n [[[ 6 2 2]\n [ 4 1 12]]\n [[ 0 2 1]\n [ 4 1 1]]]]", " ")

should yield:

array([[[[ 6.,  2.,  2.],
     [ 4., 11.,  2.]],

    [[ 6.,  2.,  2.],
     [ 4., 11.,  2.]]],


   [[[ 6.,  2.,  2.],
     [ 4.,  1., 12.]],

    [[ 0.,  2.,  1.],
     [ 4.,  1.,  1.]]]])

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.