Changeset 11647:dfa6d31c2fc2 in orange


Ignore:
Timestamp:
08/02/13 16:10:29 (9 months ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Message:

Preserve the domain's meta attributes and class_vars.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • Orange/feature/selection.py

    r11646 r11647  
    33from operator import itemgetter 
    44 
     5import Orange.data 
    56import Orange.core as orange 
    67 
    78from Orange.feature.scoring import score_all 
     9 
     10 
     11def _select_features_subset(data, features): 
     12    """Select the `features` from the `data`. 
     13 
     14    .. note:: 
     15        The `features` must be a subset of the `data.domain.features`. 
     16 
     17    """ 
     18    def as_descriptor(arg): 
     19        """Ensure `arg` is an descriptor from `data.domain`""" 
     20        return data.domain[arg] 
     21 
     22    features = map(as_descriptor, features) 
     23    domain = Orange.data.Domain(features, data.domain.class_var, 
     24                                class_vars=data.domain.class_vars) 
     25    domain.add_metas(data.domain.get_metas()) 
     26    return Orange.data.Table(domain, data) 
    827 
    928 
     
    5372    :type n: int 
    5473    :rtype: :obj:`Orange.data.Table` 
    55     """ 
    56     return data.select(top_rated(scores, n) + [data.domain.classVar.name]) 
     74 
     75    """ 
     76    features = top_rated(scores, n) 
     77    return _select_features_subset(data, features) 
    5778 
    5879selectBestNAtts = select 
     
    7596    :rtype: :obj:`Orange.data.Table` 
    7697    """ 
    77     return data.select(above_threshold(scores, threshold) + \ 
    78                        [data.domain.classVar.name]) 
     98    features = above_threshold(scores, threshold) 
     99    return _select_features_subset(data, features) 
    79100 
    80101selectAttsAboveThresh = select_above_threshold 
  • Orange/testing/unit/tests/test_feature_selection.py

    r11646 r11647  
    5151        self.assertEqual(above, [a.name for a in new_data.domain.attributes]) 
    5252        self.assertEqual(new_data.domain.class_var, self.data.domain.class_var) 
    53          
    54          
    55          
    56          
    57          
    58      
     53 
     54    def test_select_features_subset(self): 
     55        data = Orange.data.Table("lenses") 
     56 
     57        d1 = selection._select_features_subset(data, []) 
     58        self.assertSequenceEqual(d1.domain.features, []) 
     59        self.assertIs(d1.domain.class_var, data.domain.class_var) 
     60 
     61        d1 = selection._select_features_subset(data, [data.domain[0]]) 
     62        self.assertSequenceEqual(d1.domain.features, [data.domain[0]]) 
     63        self.assertIs(d1.domain.class_var, data.domain.class_var) 
     64 
     65        domain = Orange.data.Domain(data.domain.features[:2], 
     66                                    data.domain.class_var, 
     67                                    class_vars=[data.domain.features[2]]) 
     68        domain.add_metas({-1, data.domain.features[3]}) 
     69        data = Orange.data.Table(domain, data) 
     70 
     71        d1 = selection._select_features_subset(data, [data.domain[0]]) 
     72        self.assertSequenceEqual(d1.domain.features, [data.domain[0]]) 
     73        self.assertIs(d1.domain.class_var, data.domain.class_var) 
     74        self.assertSequenceEqual(d1.domain.class_vars, data.domain.class_vars) 
     75        self.assertEqual(d1.domain.get_metas(), data.domain.get_metas()) 
     76 
    5977if __name__ == "__main__": 
    6078    unittest.main() 
    61      
  • docs/reference/rst/Orange.feature.selection.rst

    r11405 r11647  
    2626The module also includes a learner that incorporates feature subset 
    2727selection. 
     28 
     29 
     30.. versionadded:: 2.7.1 
     31   `select`, `select_above_threshold` and `select_relief` now preserve 
     32   the domain's meta attributes and `class_vars`. 
    2833 
    2934-------------------------------------- 
Note: See TracChangeset for help on using the changeset viewer.