Home > Software engineering >  How to sort numpy array according to the to the two element indexes and group the same type elements
How to sort numpy array according to the to the two element indexes and group the same type elements

Time:11-28

I'm looking for a solution that would help me do the following.

Suppose I have to generate HTML code using hand-drawn design. I got element type, x, y coordination width, and height as NumPy array. I need to sort these arrays according to the y coordination if the two elements are in the same value then sort according to the y value. Then I need to group the elements in the same type.

I created array like this :

import numpy
s = numpy.array([
          #element type, x,y,width,height
          ["hyperlink", 5, 150, 25, 10],
          ["paragraph", 20, 60, 10, 10],
          ["image", 85, 150, 25, 10],
          ["radio", 20, 60, 10, 10],
          ["radio", 85, 150, 25, 10],
          ["button", 20, 60, 10, 10],
          ["text_field", 20, 40, 25, 10],  
          ["label", 10, 10, 20, 10]])

print(s)
lists = s.tolist()
print([{"element_type ": x[0] ,  "x": x[1], "y": x[2], "width":x[3], "height" :x[4]} for i, x in enumerate(lists)])

I'd like to sort it such that my points are ordered by y-coordinate, and then by x in cases where their coordinate is the same.

Then I need to group the elements such as two radio buttons as in the above example. I expected output as follows.

{
'element_type ': 'hyperlink',
 'group_id' :"1",
 'x': '5', 
 'y': '150', 
 'width': '25', 
 'height': '10'
},
    
{
'element_type ': 'image',
'group_id' :"3", 
'x': '85', 
'y': '150', 
'width': '25', 
'height': '10'
}, 

{'element_type ': 'radio', 
'group_id' :"4",
'x': '20', 
'y': '60', 
'width': '10', 
'height': '10'
}, 

{
'element_type ': 'radio',
'group_id' :"4", 
'x': '85', 
'y': '150', 
'width': '25', 
'height': '10'
}, 

 {
'element_type ': 'text_field', 
'group_id' :"5",
'x': '20', 
'y': '40', 
'width': '25', 
'height': '10'
}, 

{'element_type ': 'label', 
**group_id :"5",** 
'x': '10', 
'y': '10', 
'width': '20', 
'height': '10'
}]

Can I get an idea for this? I used python.

CodePudding user response:

numpy is not designed for groupby operations. Except the case you force it. pandas is a better option here.

import pandas as pd
df = pd.DataFrame(s, columns = ['element_type', 'x', 'y', 'width', 'height'])
df['group_id'] = df.groupby(['x', 'y', 'width', 'height']).ngroup()
>>> df.to_dict('records')

[{'element type': 'hyperlink',
  'x': '5',
  'y': '150',
  'width': '25',
  'height': '10',
  'group_id': 3},
 ...,
 {'element type': 'label',
  'x': '10',
  'y': '10',
  'width': '20',
  'height': '10',
  'group_id': 0}]

CodePudding user response:

I thought grouping was tied to the sorting, so here goes.

  • You can use numpy.lexsort method to sort by y first and then x.

  • Then use pandas.groupby to group by element_type and assign group numbers.

  • Finally convert back to a dictionary.

    import numpy as np
    import pandas as pd
    s = s[np.lexsort((s[:,2], s[:,1]))]
    df = pd.DataFrame(s, columns = ['element_type', 'x', 'y', 'width', 'height'])
    df['group_id'] = (df.groupby(['element_type']).ngroup()   1).astype(str)
    output = df.to_dict("records")
    
  • Related