next up previous contents
Next: Section Summary Up: Integrated Methods Previous: labelUsing   Contents

sortDatasetByLabel

While a SortedView by itself may be sufficient for some purposes, there is often the situation where it is of interest to order the data by some external characteristic. It may be for readability (order by classification), information (order by probability), or some other, user-conceived purpose.

To facilitate this, the Labeling class contains a sortDatasetByLabel() method. This is special in the sense that it will only work on a SortedView and will return an error if applied to any other view type. For an example, let's sort our previous dataset to even rows first, odd rows next.

>>> classes.sortDatasetByLabel()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "~/checkout/code/python/compClust/mlx/Labeling.py", 
  line 99, in sortDatasetByLabel
    raise TypeError
TypeError
>>> sort_view = SortedView(ds)
>>> even_odd = Labeling(ds)
>>> even_odd.addLabelToRows(1,[0,2,4,6,8])
>>> even_odd.addLabelToRows(2,[1,3,5,7])
>>> sort_lab = sort_view.labelUsing(even_odd)
>>> sort_view.getData()
[[ 0.93310058, 0.59187013,]
 [ 0.62153167, 0.28014728,]
 [ 0.66348588, 0.06767605,]
 [ 0.19404136, 0.3603994 ,]
 [ 0.39297128, 0.4130477 ,]
 [ 0.3661778 , 0.89579421,]
 [ 0.32203168, 0.02883008,]
 [ 0.53443021, 0.62014562,]
 [ 0.79724813, 0.12202285,]]
>>> sort_lab.getRowLabels()
[1, 2, 1, 2, 1, 2, 1, 2, 1]
>>> sort_lab.sortDatasetByLabel()
>>> sort_view.getData()
[[ 0.93310058, 0.59187013,]
 [ 0.66348588, 0.06767605,]
 [ 0.39297128, 0.4130477 ,]
 [ 0.32203168, 0.02883008,]
 [ 0.79724813, 0.12202285,]
 [ 0.62153167, 0.28014728,]
 [ 0.19404136, 0.3603994 ,]
 [ 0.3661778 , 0.89579421,]
 [ 0.53443021, 0.62014562,]]
>>> sort_lab.getRowLabels()      
[1, 1, 1, 1, 1, 2, 2, 2, 2]

It's not just integer labels which can be sorted. Any python object which is capable of being sorted (which is also any object which can be used as a label) can be used to sort a Dataset. You can even mix different python objects and sort on them, though the results may be strange. i.e. Is 0.2 < (1,2,3)?



Lucas Scharenbroich 2003-08-27