Example #1
0
    def test_integrated_mini_batch_kmeans_silhouette(self):
        """
        Test no exceptions for mini-batch kmeans silhouette visualizer
        """
        # NOTE see #182: cannot use occupancy dataset because of memory usage

        # Generate a blobs data set
        X, y = make_blobs(n_samples=1000,
                          n_features=12,
                          centers=8,
                          shuffle=False,
                          random_state=0)

        try:
            fig = plt.figure()
            ax = fig.add_subplot()

            visualizer = SilhouetteVisualizer(MiniBatchKMeans(random_state=0),
                                              ax=ax)
            visualizer.fit(X)
            visualizer.poof()

            self.assert_images_similar(visualizer)
        except Exception as e:
            self.fail("error during silhouette: {}".format(e))
Example #2
0
    def test_colormap_as_colors_silhouette(self):
        """
        Test no exceptions for modifying the colors in a silhouette visualizer
        by using a matplotlib colormap as colors
        """
        # Generate a blobs data set
        X, y = make_blobs(n_samples=1000,
                          n_features=12,
                          centers=8,
                          shuffle=False,
                          random_state=0)

        try:
            fig = plt.figure()
            ax = fig.add_subplot()

            visualizer = SilhouetteVisualizer(MiniBatchKMeans(random_state=0),
                                              ax=ax,
                                              colors="cool")
            visualizer.fit(X)
            visualizer.finalize()

            tol = (3.2 if sys.platform == "win32" else 0.01
                   )  # Fails on AppVeyor with RMS 3.143
            self.assert_images_similar(visualizer, remove_legend=True, tol=tol)
        except Exception as e:
            self.fail("error during silhouette: {}".format(e))
Example #3
0
    def test_colors_silhouette(self):
        """
        Test no exceptions for modifying the colors in a silhouette visualizer
        with a list of color names
        """
        # Generate a blobs data set
        X, y = make_blobs(n_samples=1000,
                          n_features=12,
                          centers=8,
                          shuffle=False,
                          random_state=0)

        try:
            fig = plt.figure()
            ax = fig.add_subplot()

            visualizer = SilhouetteVisualizer(
                MiniBatchKMeans(random_state=0),
                ax=ax,
                colors=["red", "green", "blue", "indigo", "cyan", "lavender"],
            )
            visualizer.fit(X)
            visualizer.finalize()

            self.assert_images_similar(visualizer, remove_legend=True)
        except Exception as e:
            self.fail("error during silhouette: {}".format(e))
    def test_integrated_mini_batch_kmeans_silhouette(self):
        """
        Test no exceptions for mini-batch kmeans silhouette visualizer
        """
        # NOTE see #182: cannot use occupancy dataset because of memory usage

        # Generate a blobs data set
        X, y = make_blobs(
            n_samples=1000, n_features=12, centers=8, shuffle=False, random_state=0
        )

        try:
            fig = plt.figure()
            ax = fig.add_subplot()

            visualizer = SilhouetteVisualizer(MiniBatchKMeans(random_state=0), ax=ax)
            visualizer.fit(X)
            visualizer.poof()

            self.assert_images_similar(visualizer)
        except Exception as e:
            self.fail("error during silhouette: {}".format(e))
    def test_integrated_yb_colormap(self):
        """
        Assert silhouette plot colormap can be set with a yellowbrick palette
        """
        # Generate a blobs data set
        X, y = make_blobs(n_samples=1000,
                          n_features=12,
                          centers=8,
                          shuffle=False,
                          random_state=0)
        visualizer = SilhouetteVisualizer(KMeans(random_state=0),
                                          colormap="neural_paint")
        visualizer.fit(X)
        visualizer.finalize()

        tol = (3.2 if sys.platform == "win32" else 0.01
               )  # Fails on AppVeyor with RMS 3.143
        self.assert_images_similar(visualizer, remove_legend=True, tol=tol)
    def test_integrated_mini_batch_kmeans_silhouette(self):
        """
        Test no exceptions for mini-batch kmeans silhouette visualizer

        See #182: cannot use occupancy dataset because of memory usage
        """

        # Generate a blobs data set
        X, y = make_blobs(
            n_samples=1000,
            n_features=12,
            centers=8,
            shuffle=True,
        )

        try:
            visualizer = SilhouetteVisualizer(MiniBatchKMeans())
            visualizer.fit(X)
            visualizer.poof()
        except Exception as e:
            self.fail("error during silhouette: {}".format(e))
Example #7
0
    def test_with_fitted(self):
        """
        Test that visualizer properly handles an already-fitted model
        """
        X, y = load_nfl(return_dataset=True).to_numpy()

        model = MiniBatchKMeans().fit(X, y)

        with mock.patch.object(model, "fit") as mockfit:
            oz = SilhouetteVisualizer(model)
            oz.fit(X, y)
            mockfit.assert_not_called()

        with mock.patch.object(model, "fit") as mockfit:
            oz = SilhouetteVisualizer(model, is_fitted=True)
            oz.fit(X, y)
            mockfit.assert_not_called()

        with mock.patch.object(model, "fit") as mockfit:
            oz = SilhouetteVisualizer(model, is_fitted=False)
            oz.fit(X, y)
            mockfit.assert_called_once_with(X, y)