Changeset 3696:9591e3c46e17 in orange


Ignore:
Timestamp:
05/27/07 18:46:09 (7 years ago)
Author:
Gregor <Gregor@…>
Branch:
default
Convert:
51126b17643d4641d72467542b347d5bcff67a57
Message:
  • added mergeAttrValues
File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/orngEvalAttr.py

    r3671 r3696  
    9090 
    9191 
     92def mergeAttrValues(data, attrList, attrMeasure, removeUnusedValues = 1): 
     93    import orngCI 
     94    #data = data.select([data.domain[attr] for attr in attrList] + [data.domain.classVar]) 
     95    newData = data.select(attrList + [data.domain.classVar]) 
     96    newAttr = orngCI.FeatureByCartesianProduct(newData, attrList)[0] 
     97    dist = orange.Distribution(newAttr, newData) 
     98    activeValues = [] 
     99    for i in range(len(newAttr.values)): 
     100        if dist[newAttr.values[i]] > 0: activeValues.append(i) 
     101    currScore = attrMeasure(newAttr, newData) 
     102    while 1: 
     103        bestScore, bestMerge = currScore, None 
     104        for i1, ind1 in enumerate(activeValues): 
     105            oldInd1 = newAttr.getValueFrom.lookupTable[ind1] 
     106            for ind2 in activeValues[:i1]: 
     107                newAttr.getValueFrom.lookupTable[ind1] = ind2 
     108                score = attrMeasure(newAttr, newData) 
     109                if score >= bestScore: 
     110                    bestScore, bestMerge = score, (ind1, ind2) 
     111                newAttr.getValueFrom.lookupTable[ind1] = oldInd1 
     112 
     113        if bestMerge: 
     114            ind1, ind2 = bestMerge 
     115            currScore = bestScore 
     116            for i, l in enumerate(newAttr.getValueFrom.lookupTable): 
     117                if not l.isSpecial() and int(l) == ind1: 
     118                    newAttr.getValueFrom.lookupTable[i] = ind2 
     119            newAttr.values[ind2] = newAttr.values[ind2] + "+" + newAttr.values[ind1] 
     120            del activeValues[activeValues.index(ind1)] 
     121        else: 
     122            break 
     123 
     124    if not removeUnusedValues: 
     125        return newAttr 
     126 
     127    reducedAttr = orange.EnumVariable(newAttr.name, values = [newAttr.values[i] for i in activeValues]) 
     128    reducedAttr.getValueFrom = newAttr.getValueFrom 
     129    reducedAttr.getValueFrom.classVar = reducedAttr 
     130    return reducedAttr 
     131 
     132 
     133 
    92134##if __name__=="__main__": 
    93135##    data = orange.ExampleTable(r"E:\Development\Orange Datasets\UCI\zoo.tab") 
     
    97139##    #import orngCI 
    98140##    #newFeature, quality = orngCI.FeatureByIM(data, ["milk", "airborne"], binary = 0, measure = MeasureAttribute_MDL()) 
    99      
     141 
Note: See TracChangeset for help on using the changeset viewer.