| 1 |  |  | import logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import attr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import numpy as np | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import somoclu | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from sklearn.cluster import KMeans | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | logger = logging.getLogger(__name__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | def infer_map(nb_cols, nb_rows, dataset, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     """Infer a self-organizing map from dataset.\n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     initialcodebook = None, kerneltype = 0, maptype = 'planar', gridtype = 'rectangular', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     compactsupport = False, neighborhood = 'gaussian', std_coeff = 0.5, initialization = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     if not hasattr(dataset, 'feature_vectors'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         raise NoFeatureVectorsError("Attempted to train a Som model, " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |                                     "but did not find feature vectors in the dataset.") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     som = somoclu.Somoclu(nb_cols, nb_rows, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     som.train(data=np.array(dataset.feature_vectors, dtype=np.float32)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     return som | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | @attr.s(slots=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class SomTrainer: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     infer_map: callable = attr.ib() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     @staticmethod | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     def from_callable(): | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         return SomTrainer(infer_map) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | @attr.s | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | class SelfOrganizingMap: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     som = attr.ib(init=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     dataset_name = attr.ib(init=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     def height(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return self.som._n_rows | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     def width(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         return self.som._n_columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     def type(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         return self.som._map_type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     def grid_type(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return self.som._grid_type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     def __getattr__(self, item): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if item in ('n_rows', 'n_columns', 'initialization', 'map_type', 'grid_type'): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             item = f'_{item}' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return getattr(self.som, item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     def get_map_id(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         _ = '_'.join(getattr(self, attribute) for attribute in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                      ['dataset_name', 'n_rows', 'n_columns', 'initialization', 'map_type', 'grid_type']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if self.som.clusters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             return f'{_}_cl{self.nb_clusters}' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return _ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     def nb_clusters(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return np.max(self.som.clusters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     def neurons_coordinates(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         raise NotImplementedError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         # # iterate through the array of shape [nb_datapoints, 2]. Each row is the coordinates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         # for i, arr in enumerate(self.som.bmus): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         #     # of the neuron the datapoint gets attributed to (closest distance) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         #     attributed_cluster = self.som.clusters[arr[0], arr[1]]  # >= 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         #     id2members[attributed_cluster].add(dataset[i].id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     def datapoint_coordinates(self, index): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         """Get the best-matching unit (bmu) coordinates of the datapoint indexed by the input pointer.\n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         Bmu is simply the neuron on the som grid that is closest to the projected-into-2D-space datapoint.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return self.som.bmus[index][0], self.som.bmus[index][1] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     def project(self, datapoint): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         """Compute the coordinates of a (potentially unseen) datapoint. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         It is assumed that the codebook has been computed already.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         raise NotImplementedError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     def cluster(self, nb_clusters, random_state=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         self.som.cluster(algorithm=KMeans(n_clusters=nb_clusters, random_state=random_state)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     def visual_umatrix(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         buffer = '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         # i.e. a clustering of 11 clusters with ids 0, 1, .., 10 has a max_len = 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         max_len = len(str(np.max(self.som.clusters))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         for j in range(self.som.umatrix.shape[0]): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             buffer += ' '.join(' ' * (max_len - len(str(i))) + str(i) for i in self.som.clusters[j, :]) + '\n' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return buffer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  | class NoFeatureVectorsError(Exception): pass | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  |