0

I have 2 numpy arrays:

1st array Contains a matrix of 3 elements : Class name, ID, coordinates

['Mobile Phone', '000ad20b5e452b24','0.0196.800512617.6939.200512']

2nd array contains a matrix of 3 elements: Class name, ID, image array:

['Mobile Phone', '000ad20b5e452b24',
        array([[[183, 205, 210],
                [181, 203, 208],
                [186, 206, 211],
                ...,
                [202, 216, 222],
                [201, 213, 219],
                [202, 214, 220]],

               [[178, 200, 205],
                [177, 199, 204],
                [179, 199, 204],
                ...,
                [186, 200, 206],
                [189, 201, 207],
                [194, 206, 212]],

               [[174, 196, 201],
                [173, 195, 200],
                [174, 193, 200],
                ...,
                [170, 184, 190],
                [172, 184, 190],
                [177, 189, 195]],

               ...,

               [[217, 226, 235],
                [216, 225, 234],
                [213, 222, 231],
                ...,
                [ 88,  97, 110],
                [ 96, 105, 118],
                [100, 109, 122]],

               [[202, 209, 218],
                [193, 200, 209],
                [181, 190, 199],
                ...,
                [124, 128, 139],
                [134, 138, 149],
                [139, 143, 154]],

               [[183, 190, 199],
                [168, 175, 184],
                [152, 161, 170],
                ...,
                [147, 149, 159],
                [160, 162, 173],
                [167, 169, 180]]]

The first array could have duplicate IDs, but the second one doesnt.

For each row in the first array i want to check if the second array has the same id and class name and append or get the image array.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
King
  • 451
  • 6
  • 17
  • Might be easier to treat this as a merge operation https://stackoverflow.com/questions/53645882/pandas-merging-101 – Chris Dec 29 '21 at 15:04
  • I was thinking the same, but as I am going to build a convolutional neural network, and I need to keep the image array shape, with RGB, and im afraid that by doing this i will loose the array when reconverting from pandas – King Dec 29 '21 at 15:15
  • 1
    It won't be a problem at all, the numpy array will be preserved – Chris Dec 29 '21 at 15:43
  • thats great, thanks! il do that.if you want to put your answer so i can mark it as resolved! – King Dec 29 '21 at 15:55

1 Answers1

1

Here is a simplified example, with a 1d array. Only one of of the classes will match, the other example is not found in the image array. The inner join will capture the desired match, and you can see by inspecting the data type of the image data that it is still an ndarray.

import numpy as np
classes = np.array([['Mobile Phone', '000ad20b5e452b24','0.0196.800512617.6939.200512'],
                  ['Mobile Phone', '000ad20b5e99999','0.0196.800512617.6939.200512']])

image_data = np.array([['Mobile Phone', '000ad20b5e452b24', np.array([183, 205, 210])],
                       ['Mobile Phone', '000ad20b5e444444', np.array([183, 205, 210])]])
              
    
c = pd.DataFrame(classes, columns=['class','id','coordinates'])
i = pd.DataFrame(image_data, columns=['class','id','image'])

output = c.merge(i, on=['class','id'], how='inner')

print(output)

print(type(output['image'].iloc[0]))

Output

    class           id                  coordinates                     image
0   Mobile Phone    000ad20b5e452b24    0.0196.800512617.6939.200512    [183, 205, 210]

<class 'numpy.ndarray'>
Chris
  • 15,819
  • 3
  • 24
  • 37