Changeset 10676:d89dc0429bf6 in orange


Ignore:
Timestamp:
03/28/12 14:55:38 (2 years ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Message:

Added normalization parameter to LinearSVMLearner. Changed how and when DomainContinuizer is used.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Orange/classification/svm/__init__.py

    r10665 r10676  
    722722        self.learner = SVMLearnerSparse(**kwargs) 
    723723 
    724 def default_preprocessor(): 
    725     # Construct and return a default preprocessor for use by 
    726     # Orange.core.LinearLearner learner. 
    727     impute = preprocess.Impute() 
    728     cont = preprocess.Continuize(multinomialTreatment= 
    729                                    preprocess.DomainContinuizer.AsOrdinal) 
    730     preproc = preprocess.PreprocessorList(preprocessors= 
    731                                             [impute, cont]) 
    732     return preproc 
    733  
     724 
     725""" 
     726LIBLINEAR learners interface 
     727""" 
    734728class LinearSVMLearner(Orange.core.LinearLearner): 
    735729    """Train a linear SVM model.""" 
     
    743737    __new__ = _orange__new__(base=Orange.core.LinearLearner) 
    744738 
    745     def __init__(self, solver_type=L2R_L2LOSS_DUAL, C=1.0, eps=0.01, **kwargs): 
    746         """ 
    747         :param solver_type: One of the following class constants: ``LR2_L2LOSS_DUAL``, ``L2R_L2LOSS``, ``LR2_L1LOSS_DUAL``, ``L2R_L1LOSS`` or ``L1R_L2LOSS`` 
     739    def __init__(self, solver_type=L2R_L2LOSS_DUAL, C=1.0, eps=0.01,  
     740                 normalization=True, **kwargs): 
     741        """ 
     742        :param solver_type: One of the following class constants:  
     743            ``LR2_L2LOSS_DUAL``, ``L2R_L2LOSS``,  
     744            ``LR2_L1LOSS_DUAL``, ``L2R_L1LOSS`` or  
     745            ``L1R_L2LOSS`` 
    748746         
    749747        :param C: Regularization parameter (default 1.0) 
     
    752750        :param eps: Stopping criteria (default 0.01) 
    753751        :type eps: float 
    754           
     752         
     753        :param normalization: Normalize the input data prior to learning 
     754            (default True) 
     755        :type normalization: bool 
     756         
    755757        """ 
    756758        self.solver_type = solver_type 
    757759        self.eps = eps 
    758760        self.C = C 
     761        self.normalization = normalization 
     762 
    759763        for name, val in kwargs.items(): 
    760764            setattr(self, name, val) 
    761765        if self.solver_type not in [self.L2R_L2LOSS_DUAL, self.L2R_L2LOSS, 
    762766                self.L2R_L1LOSS_DUAL, self.L2R_L1LOSS_DUAL, self.L1R_L2LOSS]: 
    763             pass 
    764 #            raise ValueError("Invalid solver_type parameter.") 
    765  
    766         self.preproc = default_preprocessor() 
    767  
    768     def __call__(self, instances, weight_id=None): 
    769         instances = self.preproc(instances) 
    770         classifier = super(LinearSVMLearner, self).__call__(instances, weight_id) 
    771         return classifier 
     767            import warnings 
     768            warnings.warn("""\ 
     769Deprecated 'solver_type', use  
     770'Orange.classification.logreg.LibLinearLogRegLearner' 
     771to build a logistic regression model using LIBLINEAR. 
     772""", 
     773                DeprecationWarning) 
     774 
     775    def __call__(self, data, weight_id=None): 
     776        if data.domain.has_discrete_attributes() or self.normalization: 
     777            dc = Orange.data.continuization.DomainContinuizer() 
     778            dc.multinomial_treatment = dc.NValues 
     779            dc.class_treatment = dc.Ignore 
     780            dc.continuous_treatment = \ 
     781                    dc.NormalizeBySpan if self.normalization else dc.Leave 
     782            c_domain = dc(data) 
     783            data = data.translate(c_domain) 
     784 
     785        return super(LinearSVMLearner, self).__call__(data, weight_id) 
    772786 
    773787LinearLearner = LinearSVMLearner 
Note: See TracChangeset for help on using the changeset viewer.