Exemplo n.º 1
0
 def test_train_hexagonal(self):
     data = pd.read_csv('../data/test_data.csv').drop(['Class'], axis=1)
     som = StandardSOM((50, 50), 5, "hexagonal")
     som.train(data)
     self.assertTrue(som.trained)
     self.assertIsNotNone(som.codebook)
     self.assertIsNotNone(som.bmu_indices)
     self.assertIsNotNone(som.bmu_distances)
     self.assertIsNotNone(som.get_first_bmus())
     self.assertIsNotNone(som.get_second_bmus())
Exemplo n.º 2
0
 def test_rectangular(self):
     data = pd.read_csv('../../../data/test_data.csv').drop(['Class'],
                                                            axis=1)
     som = StandardSOM((50, 50), 5)
     som.train(data)
     self.assertTrue(som.trained)
     self.assertIsNotNone(som.codebook)
     self.assertIsNotNone(som.bmu_indices)
     self.assertIsNotNone(som.bmu_distances)
     self.assertIsNotNone(som.get_first_bmus())
     self.assertIsNotNone(som.get_second_bmus())
     topographic_error(som)
Exemplo n.º 3
0
def __standard_som_hit_histogram(som: StandardSOM, cmap: str):
    """
    Plot the hit histogram for a standard SOM. Plot depends on the topology of the neighborhood (hexagonal or
    rectangular).

    Parameters:
    -----------
    som: StandardSOM
        The SOM for which the hit histogram should be plotted.
    cmap: str
        The matplotlib color map for the map.

    Returns:
    --------
    None
    """
    if som.codebook is not None and som.trained:
        first_bmus = som.get_first_bmus()
        # init hit result array
        hits = np.zeros(len(som.positions))
        # aggregate for each position with hits
        agg = {k: len(v) for k, v in first_bmus.items()}
        # fill up values at the resp. positions
        hits[list(agg.keys())] = list(agg.values())
        # define normalizer for matplotlib colors
        normalized = Normalize(vmin=np.min(hits), vmax=np.max(hits))
        # plot
        fig, ax = plt.subplots(1)
        ax.set_aspect("equal")
        plt.axis('off')
        cmap = cm.get_cmap(cmap)
        if som.topology == "rectangular":
            # axis limits
            ax.set_xlim(-1, som.map_size[1])
            ax.set_ylim(-1, som.map_size[0])
            for pos, hit in zip(som.positions, hits):
                # noinspection PyTypeChecker
                rect = RegularPolygon((pos[1], pos[0]),
                                      numVertices=4,
                                      radius=np.sqrt(0.5),
                                      orientation=np.radians(45),
                                      edgecolor='k',
                                      facecolor=cmap(normalized(hit)),
                                      alpha=0.2)
                ax.add_patch(rect)

        else:
            # hexagonal topology
            # Horizontal cartesian coords
            hcoord = [c[0] for c in som.positions]
            # Vertical cartersian coords
            vcoord = [
                2. * np.sin(np.radians(60)) * (c[1] - c[2]) / 3.
                for c in som.positions
            ]
            # axis limits
            ax.set_xlim(np.min(hcoord) - 2, np.max(hcoord) + 2)
            ax.set_ylim(np.min(vcoord) - 2, np.max(vcoord) + 2)
            for x, y, hit in zip(hcoord, vcoord, hits):
                # noinspection PyTypeChecker
                rect = RegularPolygon((x, y),
                                      numVertices=6,
                                      radius=2. / 3.,
                                      orientation=np.radians(30),
                                      edgecolor='k',
                                      facecolor=cmap(normalized(hit)),
                                      alpha=0.2)
                ax.add_patch(rect)

        # add colorbar
        plt.colorbar(cm.ScalarMappable(norm=normalized, cmap=cmap), ax=ax)
        # show
        plt.show()