コード例 #1
0
 def test_train_learning_rate_less_than_zero_should_raise_value_error(self):
     data = pd.read_csv('../data/test_data.csv').drop(['Class'], axis=1)
     som = StandardSOM((50, 50), 5)
     with self.assertRaises(ValueError):
         som.train(data, alpha=-0.01)
     self.assertFalse(som.trained)
     self.assertIsNone(som.codebook)
コード例 #2
0
ファイル: _quantization.py プロジェクト: nikdra/sos-som
 def test_hexagonal(self):
     data = pd.read_csv('../../data/test_data.csv').drop(['Class'], axis=1)
     som = StandardSOM((50, 50), 5, "hexagonal")
     som.train(data)
     qe_m(som)
     mqe_m(som)
     mqe(som)
     mmqe(som)
コード例 #3
0
ファイル: _quantization.py プロジェクト: nikdra/sos-som
 def test_rectangular(self):
     data = pd.read_csv('../../data/test_data.csv').drop(['Class'], axis=1)
     som = StandardSOM((50, 50), 5)
     som.train(data)
     qe_m(som)
     mqe_m(som)
     mqe(som)
     mmqe(som)
コード例 #4
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())
コード例 #5
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)
コード例 #6
0
 def test_hexagonal(self):
     data = pd.read_csv('../../data/test_data.csv').drop(['Class'], axis=1)
     som = StandardSOM((50, 50), 5, "hexagonal")
     som.train(data)
     topographic_error(som)
コード例 #7
0
ファイル: _density.py プロジェクト: nikdra/sos-som
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()
コード例 #8
0
 def test_train_iterations_less_than_zero_should_raise_value_error(self):
     data = pd.read_csv('../data/test_data.csv').drop(['Class'], axis=1)
     som = StandardSOM((50, 50), 5)
     with self.assertRaises(ValueError):
         som.train(data, iterations=-1)
コード例 #9
0
 def test_train_data_none_should_raise_value_error(self):
     som = StandardSOM((50, 50), 5)
     with self.assertRaises(ValueError):
         som.train(None)
コード例 #10
0
 def test_distance_measure_not_supported_should_raise_value_error(self):
     with self.assertRaises(ValueError):
         StandardSOM((1, 1), 1, distance_measure="test")
コード例 #11
0
 def test_neighborhood_type_not_supported_should_raise_value_error(self):
     with self.assertRaises(ValueError):
         StandardSOM((1, 1), 1, neighborhood_type="test")
コード例 #12
0
 def test_map_size_not_int_tuple_should_raise_value_error(self):
     with self.assertRaises(ValueError):
         StandardSOM((1.0, 1.0), 1)
コード例 #13
0
 def test_neighborhood_radius_equal_zero_should_raise_value_error(self):
     with self.assertRaises(ValueError):
         StandardSOM((1, 1), 0)
コード例 #14
0
 def test_init(self):
     StandardSOM((1, 1), 1)