Home > Enterprise >  Numpy.tostring and back to an array
Numpy.tostring and back to an array

Time:12-09

I'm trying to convert a numpy array to a string representation of the array and then back to the array itself. below is the data from using frame_string = numpy.array2string(frame, separator=',') When I try to convert it back to a numpy array with numpy.fromstring(frame_string, dtype=int, sep=',') I end up with an empty [] What am I doing wrong? been banging my head against a wall with this issue for a while now. Any help is appreciated!

"""[[[24,25,22],
  [24,24,21],
  [24,24,21],
  ...,
  [36,24,30],
  [36,24,30],
  [35,23,29]],

 [[26,26,24],
  [25,25,23],
  [26,24,23],
  ...,
  [37,25,31],
  [37,25,31],
  [37,25,31]],

 [[28,25,25],
  [29,26,26],
  [30,27,27],
  ...,
  [38,26,32],
  [39,27,33],
  [39,27,33]],

 ...,

 [[22,23,18],
  [22,23,18],
  [24,22,18],
  ...,
  [21,20,24],
  [21,20,24],
  [20,19,23]],

 [[22,23,17],
  [23,23,17],
  [25,22,18],
  ...,
  [21,20,24],
  [21,19,25],
  [20,18,24]],

 [[23,23,17],
  [24,23,17],
  [26,22,18],
  ...,
  [21,20,24],
  [21,19,25],
  [20,18,24]]]"""

CodePudding user response:

numpy.fromstring is not the inverse of numpy.array2string. NumPy does not provide any inverse of numpy.array2string.

In fact, it is impossible to recover your original array from the string shown. By default, numpy.array2string will summarize large arrays, replacing large chunks of their content with .... We can see that this happened with your array. That data is lost. The documented way to avoid this would have been to pass threshold=sys.maxsize to the array2string call.

  • Related