Ry Covington
Ry Covington

Reputation: 1

Converting a string of lists of lists into a Numpy array

I'm still quite new to Pandas and Numpy. I'm trying to convert a string that contains a list of lists into an array of shape (150, 150, 3).

Right now, I'm importing a Pandas dataframe from a CSV. The dataframe has 3 columns: imageName, data, label. When I run df['data'][0] I get a string that looks like this:

'[[[110 122 100]\n  [120 132 110]\n  [119 131 110]\n  ...\n  [105  89  90]\n  [117 104 105]\n  [ 56  45  46]]\n\n [[116 127 106]\n  [123 135 114]\n  [117 131 110]\n  ...\n  [ 99  84  83]\n  [103  88  90]\n  [108  97  97]]\n\n [[112 127 106]\n  [121 136 114]\n  [116 130 112]\n  ...\n  [102  88  86]\n  [109  95  96]\n  [116 105 105]]\n\n ...\n\n [[145 158 148]\n  [125 134 124]\n  [110 112 103]\n  ...\n  [105  84  86]\n  [106  85  87]\n  [113  92  94]]\n\n [[144 167 154]\n  [135 152 142]\n  [122 132 124]\n  ...\n  [104  83  85]\n  [106  85  87]\n  [109  88  90]]\n\n [[138 170 158]\n  [143 170 160]\n  [138 158 149]\n  ...\n  [105  84  86]\n  [105  84  86]\n  [105  84  86]]]'

Each set of numbers is a pixel (values for R,G,B). Basically, I'm trying to reconstruct an image array. I'm trying to turn each of these strings into an array of shape (150, 150, 3) so that they would look like:

[[[110 122 100]
  [120 132 110]
  [119 131 110] ... you get the idea. 

I've tried .strip() and .split() to get rid of the \n characters, but it still doesn't solve the problem. I've also tried to import ast and use ast.literal_eval()- as suggested here - but it doesn't work either. I get a strange error message:

File "<unknown>", line 1
  [[[110 122 100]
         ^
SyntaxError: invalid syntax

Any help would tremendously appreciated. Thanks for your time and consideration.

All the best, ry.co

Upvotes: 0

Views: 90

Answers (1)

user2357112
user2357112

Reputation: 280898

Your data is already lost. Those ... parts are where NumPy threw away your data because you did something like print(some_array) instead of saving it with something like numpy.save. You need to regenerate your data and save it properly this time.

Upvotes: 2

Related Questions