Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_colormap_silhouette(self):
"""
Test no exceptions for modifying the colormap in a silhouette visualizer
"""
# 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, colormap="gnuplot"
)
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.finalize()
self.assert_images_similar(visualizer, remove_legend=True)
except Exception as e:
self.fail("error during silhouette: {}".format(e))
def test_integrated_kmeans_silhouette(self):
"""
Test no exceptions for kmeans silhouette visualizer on blobs dataset
"""
# 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(KMeans(random_state=0), ax=ax)
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_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))
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)
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)
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_quick_method(self):
"""
Test the quick method producing a valid visualization
"""
X, y = make_blobs(
n_samples=1000, n_features=12, centers=8, shuffle=False, random_state=0
)
model = MiniBatchKMeans(3, random_state=343)
oz = silhouette_visualizer(model, X, show=False)
assert isinstance(oz, SilhouetteVisualizer)
self.assert_images_similar(oz)
show : bool, default: True
If True, calls ``show()``, which in turn calls ``plt.show()`` however
you cannot call ``plt.savefig`` from this signature, nor
``clear_figure``. If False, simply calls ``finalize()``
kwargs : dict
Keyword arguments that are passed to the base class and may influence
the visualization as defined in other Visualizers.
Returns
-------
viz : SilhouetteVisualizer
The silhouette visualizer, fitted and finalized.
"""
oz = SilhouetteVisualizer(
model, ax=ax, colors=colors, is_fitted=is_fitted, **kwargs
)
oz.fit(X, y)
if show:
oz.show()
else:
oz.finalize()
return oz
def __init__(self, model, ax=None, colors=None, is_fitted="auto", **kwargs):
# Initialize the visualizer bases
super(SilhouetteVisualizer, self).__init__(model, ax=ax, **kwargs)
# Visual Properties
# Use colors if it is given, otherwise attempt to use colormap which
# which will override colors. If neither is found, default to None.
# The colormap may yet still be found in resolve_colors
self.colors = colors
if "colormap" in kwargs:
self.colors = kwargs["colormap"]