Changeset 7226:a925af283716 in orange


Ignore:
Timestamp:
02/02/11 19:17:56 (3 years ago)
Author:
lanz <lan.zagar@…>
Branch:
default
Convert:
e99a0d1bb1fb6444685890649191c2fcb4da2db4
Message:
 
Location:
orange
Files:
3 edited

Legend:

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

    r7191 r7226  
    1 from orange import \ 
     1import Orange.data 
     2import Orange.feature 
     3from Orange.core import \ 
    24        LookupLearner, \ 
    35         ClassifierByLookupTable, \ 
     
    68              ClassifierByLookupTable3, \ 
    79              ClassifierByExampleTable 
     10 
     11 
     12import orngMisc 
     13 
     14 
     15def lookupFromBound(attribute, bound): 
     16    if not len(bound): 
     17        raise TypeError, "no bound attributes" 
     18    elif len(bound)<=3: 
     19        return apply([ClassifierByLookupTable, ClassifierByLookupTable2, ClassifierByLookupTable3][len(bound)-1], [attribute] + list(bound)) 
     20    else: 
     21        return None 
     22 
     23     
     24def lookupFromFunction(attribute, bound, function): 
     25    """ 
     26    Constructs ClassifierByExampleTable or ClassifierByLookupTable mirroring the given function 
     27    """ 
     28    lookup = lookupFromBound(attribute, bound) 
     29    if lookup: 
     30        lookup.lookupTable = [Orange.data.Value(attribute, function(attributes)) for attributes in orngMisc.LimitedCounter([len(attr.values) for attr in bound])] 
     31        return lookup 
     32    else: 
     33        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)])) 
     36        return LookupLearner(examples) 
     37       
     38 
     39def lookupFromExamples(examples, weight = 0, learnerForUnknown = None): 
     40    if len(examples.domain.attributes) <= 3: 
     41        lookup = lookupFromBound(examples.domain.classVar, examples.domain.attributes) 
     42        lookupTable = lookup.lookupTable 
     43        for example in examples: 
     44            ind = lookup.getindex(example) 
     45            if not lookupTable[ind].isSpecial() and (lookupTable[ind] <> example.getclass()): 
     46                break 
     47            lookupTable[ind] = example.getclass() 
     48        else: 
     49            return lookup 
     50 
     51        # there are ambiguities; a backup plan is ClassifierByExampleTable, let it deal with them 
     52        return LookupLearner(examples, weight, learnerForUnknown = learnerForUnknown) 
     53 
     54    else: 
     55        return LookupLearner(examples, weight, learnerForUnknown = learnerForUnknown) 
     56         
     57         
     58def printLookupFunction(func): 
     59    if isinstance(func, Orange.feature.Feature): 
     60        if not func.getValueFrom: 
     61            raise TypeError, "attribute '%s' does not have an associated function" % func.name 
     62        else: 
     63            func = func.getValueFrom 
     64 
     65    outp = "" 
     66    if isinstance(func, ClassifierByExampleTable): 
     67    # XXX This needs some polishing :-) 
     68        for i in func.sortedExamples: 
     69            outp += "%s\n" % i 
     70    else: 
     71        boundset = func.boundset() 
     72        for a in boundset: 
     73            outp += "%s\t" % a.name 
     74        outp += "%s\n" % func.classVar.name 
     75        outp += "------\t" * (len(boundset)+1) + "\n" 
     76         
     77        lc = 0 
     78        if len(boundset)==1: 
     79            cnt = orngMisc.LimitedCounter([len(x.values)+1 for x in boundset]) 
     80        else: 
     81            cnt = orngMisc.LimitedCounter([len(x.values) for x in boundset]) 
     82        for ex in cnt: 
     83            for i in range(len(ex)): 
     84                if ex[i]<len(boundset[i].values): 
     85                    outp += "%s\t" % boundset[i].values[ex[i]] 
     86                else: 
     87                    outp += "?\t", 
     88            outp += "%s\n" % func.classVar.values[int(func.lookupTable[lc])] 
     89            lc += 1 
     90    return outp 
  • orange/Orange/classification/majority.py

    r7208 r7226  
    1414======== 
    1515 
    16 .. class:: Orange.classify.MajorityLearner 
     16.. class:: MajorityLearner 
    1717 
    1818    MajorityLearner will most often be used as is, without setting any 
     
    3333============== 
    3434 
    35 .. class:: Orange.classify.ConstantClassifier 
     35.. class:: ConstantClassifier 
    3636 
    3737    ConstantClassifier always classifies to the same class and reports 
  • orange/doc/Orange/rst/orange.classification.majority.rst

    r7215 r7226  
    1 ======================== 
    2 orange.classify.majority 
    3 ======================== 
     1============================== 
     2orange.classification.majority 
     3============================== 
    44 
    55.. automodule:: Orange.classification.majority 
Note: See TracChangeset for help on using the changeset viewer.