Changeset 10315:633089e8109a in orange


Ignore:
Timestamp:
02/20/12 10:37:16 (2 years ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
rebase_source:
7c373f18ece2bc2fb9bdfd082c814f1858edef83
Message:

Documentation and code cleanup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Orange/projection/som.py

    r10103 r10315  
    99   single: projection; self-organizing map (SOM) 
    1010 
    11 An implementation of  
    12 `self-organizing map <http://en.wikipedia.org/wiki/Self-organizing_map>`_ algorithm (SOM).  
    13 SOM is an unsupervised learning  algorithm that infers low,  
     11 
     12`Self-organizing map <http://en.wikipedia.org/wiki/Self-organizing_map>`_ 
     13(SOM) is an unsupervised learning  algorithm that infers low,  
    1414typically two-dimensional discretized representation of the input 
    1515space, called a map. The map preserves topological properties of the 
     
    3232 
    3333 
    34 .. autodata:: NeighbourhoodGaussian 
    35  
    36 .. autodata:: HexagonalTopology 
    37  
    38 .. autodata:: RectangularTopology 
    39  
    40 .. autodata:: InitializeLinear 
    41  
    42 .. autodata:: InitializeRandom 
    43  
    44 .. autodata:: NeighbourhoodGaussian  
    45  
    46 .. autodata:: NeighbourhoodBubble 
    47  
    48 .. autodata:: NeighbourhoodEpanechicov  
    49  
    5034.. autoclass:: SOMLearner 
    51    :members: 
    52  
    53 .. autoclass:: Solver 
    5435   :members: 
    5536    
    5637.. autoclass:: SOMMap 
    5738   :members: 
    58  
     39    
     40Topology 
     41-------- 
     42 
     43.. autodata:: HexagonalTopology 
     44 
     45.. autodata:: RectangularTopology 
     46 
     47 
     48Map initialization 
     49------------------ 
     50 
     51.. autodata:: InitializeLinear 
     52 
     53.. autodata:: InitializeRandom 
     54 
     55Node neighbourhood 
     56------------------ 
     57 
     58.. autodata:: NeighbourhoodGaussian  
     59 
     60.. autodata:: NeighbourhoodBubble 
     61 
     62.. autodata:: NeighbourhoodEpanechicov  
     63 
     64    
    5965============================================= 
    6066Supervised Learning with Self-Organizing Maps 
     
    9096================== 
    9197 
    92 Class :obj:`Map` stores the self-organizing map composed of :obj:`Node` objects. The code below 
    93 (:download:`som-node.py <code/som-node.py>`) shows an example how to access the information stored in the 
    94 node of the map: 
     98The actual map optimization algorithm is implemented by :class:`Solver` 
     99class which is used by both the :class:`SOMLearner` and the 
     100:class:`SOMSupervisedLearner`. 
     101 
     102.. autoclass:: Solver 
     103   :members: 
     104    
     105Class :obj:`Map` stores the self-organizing map composed of :obj:`Node` 
     106objects. The code below (:download:`som-node.py <code/som-node.py>`)  
     107shows an example how to access the information stored in the node of the  
     108map: 
    95109 
    96110.. literalinclude:: code/som-node.py 
     
    107121======== 
    108122 
    109 The following code  (:download:`som-mapping.py <code/som-mapping.py>`) infers self-organizing map from Iris data set. The map is rather small, and consists 
    110 of only 9 cells. We optimize the network, and then report how many data instances were mapped 
    111 into each cell. The second part of the code reports on data instances from one of the corner cells: 
     123The following code  (:download:`som-mapping.py <code/som-mapping.py>`) 
     124infers self-organizing map from Iris data set. The map is rather small, 
     125and consists of only 9 cells. We optimize the network, and then report 
     126how many data instances were mapped into each cell. The second part  
     127of the code reports on data instances from one of the corner cells: 
    112128 
    113129.. literalinclude:: code/som-mapping.py 
     
    134150    [5.5, 2.4, 3.7, 1.0, 'Iris-versicolor'] 
    135151    [5.0, 2.3, 3.3, 1.0, 'Iris-versicolor'] 
     152     
    136153""" 
    137154 
     
    223240 
    224241        self.qerror = [] 
    225         self.bmu_cache = {} 
    226242        if self.batch_train: 
    227243            self.train_batch(progress_callback) 
     
    350366 
    351367class SOMLearner(orange.Learner): 
    352     """An implementation of self-organizing map. Considers  
    353     an input data set, projects the data instances onto a map,  
    354     and returns a result in the form of a classifier holding  
    355     projection information together with an algorithm to project 
    356     new data instances. Uses :obj:`Map` for representation of  
     368    """Considers an input data set, projects the data instances 
     369    onto a map, and returns a result in the form of a classifier 
     370    holding  projection information together with an algorithm to 
     371    project new data instances. Uses :obj:`Map` for representation of  
    357372    projection space, :obj:`Solver` for training, and returns a  
    358373    trained map with information on projection of the training 
     
    372387    :param neighbourhood: cell neighborhood type id 
    373388    :type neighbourhood: :obj:`NeighbourhoodGaussian`,  
    374         obj:`NeighbourhoodBubble`, or obj:`NeighbourhoodEpanechicov` 
     389        :obj:`NeighbourhoodBubble`, or :obj:`NeighbourhoodEpanechicov` 
    375390         
    376391    :param batch_train: perform batch training? 
     
    482497         
    483498    def __call__(self, instance, what=orange.GetValue): 
     499        """Map `instance` onto the best matching node and predict 
     500        its class using the majority/mean of the training data in 
     501        that node.  
     502          
     503        """ 
    484504        bmu = self.get_best_matching_node(instance) 
    485505        return bmu.classifier(instance, what) 
     
    656676        """ 
    657677        if data is not None: 
    658             min, max = ma.min(data, 0), ma.max(data, 0); 
     678            min, max = ma.min(data, 0), ma.max(data, 0) 
    659679            dimension = data.shape[1] 
    660680        else: 
     
    674694        munits = len(list(self)) 
    675695        me = ma.mean(data, 0) 
    676         A = numpy.zeros((dim ,dim)) 
     696        A = numpy.zeros((dim, dim)) 
    677697 
    678698        for i in range(dim): 
     
    716736 
    717737def get_u_matrix(som): 
    718     dim1=som.map_shape[0]*2-1 
    719     dim2=som.map_shape[1]*2-1 
    720  
    721     a=numpy.zeros((dim1, dim2)) 
     738    dim1 = som.map_shape[0]*2-1 
     739    dim2 = som.map_shape[1]*2-1 
     740 
     741    a = numpy.zeros((dim1, dim2)) 
    722742    if som.topology == HexagonalTopology: 
    723743        return __fill_hex(a, som) 
     
    733753def __fill_hex(array, som): 
    734754    xDim, yDim = som.map_shape 
    735 ##    for n in som.nodes: 
    736 ##        d[tuple(n.pos)]=n 
    737755    d = dict([((i, j), som[i, j]) for i in range(xDim) for j in range(yDim)]) 
    738     check=lambda x,y:x>=0 and x<(xDim*2-1) and y>=0 and y<(yDim*2-1) 
    739     dx=[1,0,-1] 
    740     dy=[0,1, 1] 
    741     for i in range(0, xDim*2,2): 
    742         for j in range(0, yDim*2,2): 
    743             for ddx, ddy in zip(dx, dy): 
    744                 if check(i+ddx, j+ddy): 
    745 ##                    array[i+ddx][j+ddy]=d[(i/2, j/2)].getDistance(d[(i/2+ddx, j/2+ddy)].referenceExample) 
    746                     array[i+ddx][j+ddy] = numpy.sqrt(ma.sum((d[(i/2, j/2)].vector - d[(i/2+ddx, j/2+ddy)].vector)**2)) 
    747     dx=[1,-1,0,-1, 0, 1] 
    748     dy=[0, 0,1, 1,-1,-1] 
    749     for i in range(0, xDim*2, 2): 
    750         for j in range(0, yDim*2, 2): 
    751             l=[array[i+ddx, j+ddy] for ddx, ddy in zip(dx, dy) if check(i+ddx, j+ddy)] 
    752             array[i][j]=sum(l)/len(l) 
    753     return array 
    754  
    755 def __fill_rect(array, som): 
    756     xDim, yDim = som.map_shape 
    757     d = dict([((i, j), som[i, j]) for i in range(xDim) for j in range(yDim)]) 
    758     check=lambda x,y:x>=0 and x<xDim*2-1 and y>=0 and y<yDim*2-1 
    759     dx=[1, 0, 1] 
    760     dy=[0, 1, 1] 
     756    check = lambda x, y: x >= 0 and x < (xDim * 2 - 1) and \ 
     757                            y >= 0 and y < (yDim * 2 - 1) 
     758    dx = [1, 0, -1] 
     759    dy = [0, 1, 1] 
    761760    for i in range(0, xDim*2, 2): 
    762761        for j in range(0, yDim*2, 2): 
    763762            for ddx, ddy in zip(dx, dy): 
    764763                if check(i+ddx, j+ddy): 
    765 ##                    array[i+ddx][j+ddy]=d[(i/2, j/2)].getDistance(d[(i/2+ddx, j/2+ddy)].referenceExample) 
    766                     array[i+ddx][j+ddy] = numpy.sqrt(ma.sum((d[(i/2, j/2)].vector - d[(i/2+ddx, j/2+ddy)].vector)**2)) 
    767     dx=[1,-1, 0,0,1,-1,-1, 1] 
    768     dy=[0, 0,-1,1,1,-1, 1,-1] 
     764                    array[i+ddx][j+ddy] = \ 
     765                        numpy.sqrt(ma.sum((d[(i/2, j/2)].vector - \ 
     766                                           d[(i/2+ddx, j/2+ddy)].vector)**2)) 
     767    dx = [1, -1, 0, -1, 0, 1] 
     768    dy = [0, 0, 1, 1, -1, -1] 
    769769    for i in range(0, xDim*2, 2): 
    770770        for j in range(0, yDim*2, 2): 
    771             l=[array[i+ddx,j+ddy] for ddx,ddy in zip(dx,dy) if check(i+ddx, j+ddy)] 
    772             array[i][j]=sum(l)/len(l) 
     771            l = [array[i+ddx, j+ddy] for ddx, ddy in zip(dx, dy) \ 
     772                 if check(i+ddx, j+ddy)] 
     773            array[i][j] = sum(l)/len(l) 
     774    return array 
     775 
     776def __fill_rect(array, som): 
     777    xDim, yDim = som.map_shape 
     778    d = dict([((i, j), som[i, j]) for i in range(xDim) for j in range(yDim)]) 
     779    check = lambda x, y: x >= 0 and x < xDim*2 - 1 and y >= 0 and y < yDim*2 - 1 
     780    dx = [1, 0, 1] 
     781    dy = [0, 1, 1] 
     782    for i in range(0, xDim*2, 2): 
     783        for j in range(0, yDim*2, 2): 
     784            for ddx, ddy in zip(dx, dy): 
     785                if check(i+ddx, j+ddy): 
     786                    array[i+ddx][j+ddy] = \ 
     787                        numpy.sqrt(ma.sum((d[(i/2, j/2)].vector - \ 
     788                                           d[(i/2+ddx, j/2+ddy)].vector)**2)) 
     789    dx = [1, -1, 0, 0, 1, -1, -1, 1] 
     790    dy = [0, 0, -1, 1, 1, -1, 1, -1] 
     791    for i in range(0, xDim*2, 2): 
     792        for j in range(0, yDim*2, 2): 
     793            l = [array[i+ddx, j+ddy] for ddx, ddy in zip(dx, dy) \ 
     794                 if check(i+ddx, j+ddy)] 
     795            array[i][j] = sum(l)/len(l) 
    773796    return array 
    774797 
Note: See TracChangeset for help on using the changeset viewer.