Changeset 6973:d64403c104f5 in orange


Ignore:
Timestamp:
09/09/10 15:07:03 (4 years ago)
Author:
ales_erjavec <ales.erjavec@…>
Branch:
default
Convert:
809d005fbc499965c1a8710cd7e0f8a9307a4b9b
Message:
  • added MeasureAttribute_SVMWeights class to orngSVM
  • added Linear SVM weights measure to OWPreprocess Feature selection
Location:
orange
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • orange/OrangeWidgets/Data/OWPreprocess.py

    r6965 r6973  
    1212import orange 
    1313import orngWrap 
     14import orngSVM 
    1415 
    1516import sys, os 
     
    1718 
    1819from orange import Preprocessor_discretize, Preprocessor_imputeByLearner, Preprocessor_dropMissing 
    19  
    20 import copy_reg 
    2120 
    2221def _gettype(obj): 
     
    154153        domain.addmetas(data.domain.getmetas()) 
    155154        return orange.ExampleTable(domain, data) 
     155     
     156class Preprocessor_RFE(Preprocessor_featureSelection): 
     157    """ A preprocessor that runs RFE(Recursive feature elimination) using linear SVM derived attribute weights. 
     158    Arguments: 
     159        - `limit`: The number of selected fetures (default 10) 
     160         
     161    """ 
     162    __new__ = _orange__new(Preprocessor_featureSelection) 
     163    __reduce__ = _orange__reduce 
     164    def __init__(self, limit=10): 
     165        self.limit = limit 
     166         
     167    def __call__(self, data, weightId=None): 
     168        from orngSVM import RFE 
     169        rfe = RFE() 
     170        return rfe(data, self.limit) 
    156171     
    157172def selectNRandom(examples, N=10): 
     
    364379                ("Gain ratio", orange.MeasureAttribute_gainRatio), 
    365380                ("Gini Gain", orange.MeasureAttribute_gini), 
    366                 ("Log Odds Ratio", orange.MeasureAttribute_logOddsRatio)] 
     381                ("Log Odds Ratio", orange.MeasureAttribute_logOddsRatio), 
     382                ("Linear SVM weights", orngSVM.MeasureAttribute_SVMWeights)] 
     383     
    367384    FILTERS = [Preprocessor_featureSelection.bestN, 
    368385               Preprocessor_featureSelection.bestP] 
     
    504521               orange.MeasureAttribute_gini: "Gini", 
    505522               orange.MeasureAttribute_logOddsRatio: "Log Odds", 
     523               orngSVM.MeasureAttribute_SVMWeights: "Linear SVM weights", 
    506524               type(lambda : None): _funcName} 
    507525     
  • orange/orngSVM.py

    r6883 r6973  
    319319    """Computes a BOW kernel function (sum_i(example1[i]*example2[i])) using the examples meta attributes (need to be floats)""" 
    320320    def __call__(self, example1, example2): 
    321         s=Set(example1.getmetas().keys()).intersection(Set(example2.getmetas().keys())) 
     321        s=set(example1.getmetas().keys()).intersection(set(example2.getmetas().keys())) 
    322322        sum=0 
    323323        for key in s: 
    324324            sum+=float(example2[key])*float(example1[key]) 
    325325        return sum 
     326     
     327class MeasureAttribute_SVMWeights(orange.MeasureAttribute): 
     328    """ Measure attribute relevance by training an multi-class linear SVM classifier on provided 
     329    examples and using a squared sum of weights (of each binary classifier) as the returned measure. 
     330     
     331    Attributes: 
     332        - `learner`: Learner used for weight esstimation (default LinearLearner(solver_type=orngSVM.LinearLearner.L2Loss_SVM_Dual)) 
     333         
     334    Example:: 
     335        >>> measure = MeasureAttribute_SVMWeights() 
     336        >>> for attr in data.domain.attributes: 
     337        ...   print "%15s: %.3f" % (attr.name, measure(attr, data))   
     338    """ 
     339    def __new__(cls, attr=None, examples=None, weightId=None, **kwargs): 
     340        self = orange.MeasureAttribute.__new__(cls, **kwargs) 
     341        if examples is not None and attr is not None: 
     342            self.__init__(**kwargs) 
     343            return self.__call__(attr, examples, weightId) 
     344        else: 
     345            return self 
     346         
     347    def __reduce__(self): 
     348        return MeasureAttribute_SVMWeights, (), {"learner": self.learner} 
     349     
     350    def __init__(self, learner=None, **kwargs): 
     351        self.learner = LinearLearner(solver_type=LinearLearner.L2Loss_SVM_Dual) if learner is None else learner 
     352        self._cached_examples = None 
     353         
     354    def __call__(self, attr, examples, weightId=None): 
     355        if examples is self._cached_examples: 
     356            weights = self._cached_weights 
     357        else: 
     358            classifier = self.learner(examples, weightId) 
     359            self._cached_examples = examples 
     360            import numpy 
     361            weights = numpy.array(classifier.weights) 
     362            weights = numpy.sum(weights ** 2, axis=0) 
     363            weights = dict(zip(examples.domain.attributes, weights)) 
     364            self._cached_weights = weights 
     365        return weights.get(attr, 0.0) 
    326366 
    327367class RFE(object): 
Note: See TracChangeset for help on using the changeset viewer.