Changeset 10775:c720c17c2f3f in orange


Ignore:
Timestamp:
04/10/12 15:00:31 (2 years ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
Message:

Added base_features method to EarthClassifier.

Location:
Orange
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Orange/regression/earth.py

    r10542 r10775  
    346346        bx = base_matrix(data, self.best_set, self.dirs, self.cuts) 
    347347        return bx 
    348      
     348 
     349    def base_features(self): 
     350        """Return a list of features for the included Earth terms. 
     351        The attributes can be used in Orange's domain translation 
     352        (i.e. they define the proper ``get_value_from`` functions). 
     353         
     354        """ 
     355        terms = [] 
     356        dirs = self.dirs[self.best_set] 
     357        cuts = self.cuts[self.best_set] 
     358 
     359        for dir, cut in zip(dirs[1:], cuts[1:]): # Drop the intercept (first)  
     360            hinge = [_format_knot(self, attr.name, dir1, cut1) \ 
     361                     for (attr, dir1, cut1) in \ 
     362                     zip(self.domain.attributes, dir, cut) \ 
     363                     if dir1 != 0.0] 
     364            term_name = " * ".join(hinge) 
     365            term = Orange.feature.Continuous(term_name) 
     366            term.get_value_from = term_computer(term, self.domain, dir, cut) 
     367            terms.append(term) 
     368        return terms 
     369 
    349370    def predict(self, instance): 
    350371        """ Predict the response value(s) 
     
    398419    def __str__(self): 
    399420        return self.to_string() 
    400      
    401421 
    402422""" 
     
    461481    """ 
    462482    return  rss / (n * (1. - n_effective_params / n) ** 2) 
     483 
     484class term_computer(Orange.core.ClassifierFD): 
     485    """An utility class for computing basis terms. Can be used as 
     486    a :obj:`~Orange.feature.Descriptior.get_value_from` member. 
     487    """ 
     488    def __init__(self, term_var=None, domain=None, dir=None, cut=None): 
     489        self.class_var = term_var 
     490        self.domain = domain 
     491        self.dir = dir 
     492        self.cut = cut 
     493 
     494    def __call__(self, instance, return_what=Orange.core.GetValue): 
     495        instance = Orange.data.Instance(self.domain, instance) 
     496        attributes = self.domain.attributes 
     497        sum = 0.0 
     498        for val, dir1, cut1 in zip(instance, self.dir, self.cut): 
     499            if dir1 != 0.0 and dir1 != 2 and not val.isSpecial(): 
     500                sum += max(dir1 * (float(val) - cut1), 0.0) 
     501        return self.class_var(sum) 
    463502 
    464503""" 
  • Orange/testing/unit/tests/test_earth.py

    r10768 r10775  
    2323        str = self.classifier.to_string() 
    2424        evimp = self.classifier.evimp() 
     25        # Test base_features (make sure the domain translation works)  
     26        basis_features = self.classifier.base_features() 
     27        basis_domain = Orange.data.Domain(basis_features, None) 
     28        basis_matrix = Orange.data.Table(basis_domain, dataset) 
    2529 
    2630    @test_on_data 
Note: See TracChangeset for help on using the changeset viewer.