Changeset 7243:65e1bd9be7da in orange


Ignore:
Timestamp:
02/02/11 21:16:03 (3 years ago)
Author:
lanz <lan.zagar@…>
Branch:
default
Convert:
375d6dd23937c3a0dd9a38be0093ca5ed74d8f0a
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/Orange/classification/lookup.py

    r7226 r7243  
     1""" 
     2 
     3.. index:: lookup 
     4 
     5Lookup classifiers predict classes by looking into stored lists of 
     6cases. There are two kinds of such classifiers in Orange. The simpler 
     7and fastest ClassifierByLookupTable use up to three discrete attributes 
     8and have a stored mapping from values of those attributes to class 
     9value. The more complex classifiers stores an ExampleTable and predicts 
     10the class by matching the example to examples in the table. 
     11 
     12The natural habitat of these classifiers is feature construction: 
     13they usually reside in getValueFrom fields of constructed attributes 
     14to facilitate their automatic computation. For instance, the following 
     15script shows how to translate the Monk 1 dataset features into a more 
     16useful subset that will only include the attributes a, b, e, and 
     17attributes that will tell whether a and b are equal and whether e is 1 
     18(don't bother the details, they follow later). 
     19 
     20`classifierByLookupTable.py`_ (uses: `monks-1.tab`_): 
     21 
     22.. literalinclude:: code/classifierByLookupTable.py 
     23 
     24We can check the correctness of the script by printing out several 
     25random examples from data2. 
     26 
     27 
     28.. _classifierByLookupTable.py: code/classifierByLookupTable.py 
     29.. _monks-1.tab: code/monks-1.tab 
     30 
     31""" 
     32 
    133import Orange.data 
    234import Orange.feature 
     
    1648    if not len(bound): 
    1749        raise TypeError, "no bound attributes" 
    18     elif len(bound)<=3: 
    19         return apply([ClassifierByLookupTable, ClassifierByLookupTable2, ClassifierByLookupTable3][len(bound)-1], [attribute] + list(bound)) 
     50    elif len(bound) <= 3: 
     51        return apply([ClassifierByLookupTable, ClassifierByLookupTable2, 
     52                      ClassifierByLookupTable3][len(bound) - 1], 
     53                     [attribute] + list(bound)) 
    2054    else: 
    2155        return None 
     
    2357     
    2458def lookupFromFunction(attribute, bound, function): 
    25     """ 
    26     Constructs ClassifierByExampleTable or ClassifierByLookupTable mirroring the given function 
     59    """Constructs ClassifierByExampleTable or ClassifierByLookupTable 
     60    mirroring the given function 
     61     
    2762    """ 
    2863    lookup = lookupFromBound(attribute, bound) 
    2964    if lookup: 
    30         lookup.lookupTable = [Orange.data.Value(attribute, function(attributes)) for attributes in orngMisc.LimitedCounter([len(attr.values) for attr in bound])] 
     65        lookup.lookupTable = [Orange.data.Value(attribute, function(attributes)) 
     66                              for attributes in orngMisc.LimitedCounter( 
     67                                  [len(attr.values) for attr in bound])] 
    3168        return lookup 
    3269    else: 
    3370        examples = Orange.data.Table(Orange.data.Domain(bound, attribute)) 
    34         for attributes in orngMisc.LimitedCounter([len(attr.values) for attr in dom.attributes]): 
    35             examples.append(Orange.data.Example(dom, attributes + [function(attributes)])) 
     71        for attributes in orngMisc.LimitedCounter([len(attr.values) 
     72                                                   for attr in dom.attributes]): 
     73            examples.append(Orange.data.Example(dom, attributes + 
     74                                                [function(attributes)])) 
    3675        return LookupLearner(examples) 
    3776       
     
    3978def lookupFromExamples(examples, weight = 0, learnerForUnknown = None): 
    4079    if len(examples.domain.attributes) <= 3: 
    41         lookup = lookupFromBound(examples.domain.classVar, examples.domain.attributes) 
     80        lookup = lookupFromBound(examples.domain.classVar, 
     81                                 examples.domain.attributes) 
    4282        lookupTable = lookup.lookupTable 
    4383        for example in examples: 
    4484            ind = lookup.getindex(example) 
    45             if not lookupTable[ind].isSpecial() and (lookupTable[ind] <> example.getclass()): 
     85            if not lookupTable[ind].isSpecial() and (lookupTable[ind] <> 
     86                                                     example.getclass()): 
    4687                break 
    4788            lookupTable[ind] = example.getclass() 
     
    4990            return lookup 
    5091 
    51         # there are ambiguities; a backup plan is ClassifierByExampleTable, let it deal with them 
    52         return LookupLearner(examples, weight, learnerForUnknown = learnerForUnknown) 
     92        # there are ambiguities; a backup plan is 
     93        # ClassifierByExampleTable, let it deal with them 
     94        return LookupLearner(examples, weight, 
     95                             learnerForUnknown=learnerForUnknown) 
    5396 
    5497    else: 
    55         return LookupLearner(examples, weight, learnerForUnknown = learnerForUnknown) 
     98        return LookupLearner(examples, weight, 
     99                             learnerForUnknown=learnerForUnknown) 
    56100         
    57101         
Note: See TracChangeset for help on using the changeset viewer.