Changeset 10315:633089e8109a in orange
 Timestamp:
 02/20/12 10:37:16 (2 years ago)
 Branch:
 default
 rebase_source:
 7c373f18ece2bc2fb9bdfd082c814f1858edef83
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Orange/projection/som.py
r10103 r10315 9 9 single: projection; selforganizing map (SOM) 10 10 11 An implementation of 12 ` selforganizing map <http://en.wikipedia.org/wiki/Selforganizing_map>`_ algorithm (SOM).13 SOMis an unsupervised learning algorithm that infers low,11 12 `Selforganizing map <http://en.wikipedia.org/wiki/Selforganizing_map>`_ 13 (SOM) is an unsupervised learning algorithm that infers low, 14 14 typically twodimensional discretized representation of the input 15 15 space, called a map. The map preserves topological properties of the … … 32 32 33 33 34 .. autodata:: NeighbourhoodGaussian35 36 .. autodata:: HexagonalTopology37 38 .. autodata:: RectangularTopology39 40 .. autodata:: InitializeLinear41 42 .. autodata:: InitializeRandom43 44 .. autodata:: NeighbourhoodGaussian45 46 .. autodata:: NeighbourhoodBubble47 48 .. autodata:: NeighbourhoodEpanechicov49 50 34 .. autoclass:: SOMLearner 51 :members:52 53 .. autoclass:: Solver54 35 :members: 55 36 56 37 .. autoclass:: SOMMap 57 38 :members: 58 39 40 Topology 41  42 43 .. autodata:: HexagonalTopology 44 45 .. autodata:: RectangularTopology 46 47 48 Map initialization 49  50 51 .. autodata:: InitializeLinear 52 53 .. autodata:: InitializeRandom 54 55 Node neighbourhood 56  57 58 .. autodata:: NeighbourhoodGaussian 59 60 .. autodata:: NeighbourhoodBubble 61 62 .. autodata:: NeighbourhoodEpanechicov 63 64 59 65 ============================================= 60 66 Supervised Learning with SelfOrganizing Maps … … 90 96 ================== 91 97 92 Class :obj:`Map` stores the selforganizing map composed of :obj:`Node` objects. The code below 93 (:download:`somnode.py <code/somnode.py>`) shows an example how to access the information stored in the 94 node of the map: 98 The actual map optimization algorithm is implemented by :class:`Solver` 99 class which is used by both the :class:`SOMLearner` and the 100 :class:`SOMSupervisedLearner`. 101 102 .. autoclass:: Solver 103 :members: 104 105 Class :obj:`Map` stores the selforganizing map composed of :obj:`Node` 106 objects. The code below (:download:`somnode.py <code/somnode.py>`) 107 shows an example how to access the information stored in the node of the 108 map: 95 109 96 110 .. literalinclude:: code/somnode.py … … 107 121 ======== 108 122 109 The following code (:download:`sommapping.py <code/sommapping.py>`) infers selforganizing 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: 123 The following code (:download:`sommapping.py <code/sommapping.py>`) 124 infers selforganizing map from Iris data set. The map is rather small, 125 and consists of only 9 cells. We optimize the network, and then report 126 how many data instances were mapped into each cell. The second part 127 of the code reports on data instances from one of the corner cells: 112 128 113 129 .. literalinclude:: code/sommapping.py … … 134 150 [5.5, 2.4, 3.7, 1.0, 'Irisversicolor'] 135 151 [5.0, 2.3, 3.3, 1.0, 'Irisversicolor'] 152 136 153 """ 137 154 … … 223 240 224 241 self.qerror = [] 225 self.bmu_cache = {}226 242 if self.batch_train: 227 243 self.train_batch(progress_callback) … … 350 366 351 367 class SOMLearner(orange.Learner): 352 """An implementation of selforganizing 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 357 372 projection space, :obj:`Solver` for training, and returns a 358 373 trained map with information on projection of the training … … 372 387 :param neighbourhood: cell neighborhood type id 373 388 :type neighbourhood: :obj:`NeighbourhoodGaussian`, 374 obj:`NeighbourhoodBubble`, orobj:`NeighbourhoodEpanechicov`389 :obj:`NeighbourhoodBubble`, or :obj:`NeighbourhoodEpanechicov` 375 390 376 391 :param batch_train: perform batch training? … … 482 497 483 498 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 """ 484 504 bmu = self.get_best_matching_node(instance) 485 505 return bmu.classifier(instance, what) … … 656 676 """ 657 677 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) 659 679 dimension = data.shape[1] 660 680 else: … … 674 694 munits = len(list(self)) 675 695 me = ma.mean(data, 0) 676 A = numpy.zeros((dim ,dim))696 A = numpy.zeros((dim, dim)) 677 697 678 698 for i in range(dim): … … 716 736 717 737 def get_u_matrix(som): 718 dim1 =som.map_shape[0]*21719 dim2 =som.map_shape[1]*21720 721 a =numpy.zeros((dim1, dim2))738 dim1 = som.map_shape[0]*21 739 dim2 = som.map_shape[1]*21 740 741 a = numpy.zeros((dim1, dim2)) 722 742 if som.topology == HexagonalTopology: 723 743 return __fill_hex(a, som) … … 733 753 def __fill_hex(array, som): 734 754 xDim, yDim = som.map_shape 735 ## for n in som.nodes:736 ## d[tuple(n.pos)]=n737 755 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*21) and y>=0 and y<(yDim*21) 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*21 and y>=0 and y<yDim*21 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] 761 760 for i in range(0, xDim*2, 2): 762 761 for j in range(0, yDim*2, 2): 763 762 for ddx, ddy in zip(dx, dy): 764 763 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] 769 769 for i in range(0, xDim*2, 2): 770 770 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 776 def __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) 773 796 return array 774 797
Note: See TracChangeset
for help on using the changeset viewer.