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)
from yellowbrick.cluster import SilhouetteVisualizer from yellowbrick.datasets import load_nfl from sklearn.cluster import KMeans # Load a clustering dataset X, y = load_nfl() # Specify the features to use for clustering features = ['Rec', 'Yds', 'TD', 'Fmb', 'Ctch_Rate'] X = X.query('Tgt >= 20')[features] # Instantiate the clustering model and visualizer model = KMeans(5, random_state=42) visualizer = SilhouetteVisualizer(model, colors='yellowbrick') visualizer.fit(X) # Fit the data to the visualizer visualizer.show() # Finalize and render the figure