0

I was hoping someone could help me get a the silhouette visualizer to work/show, without having to pass the model.

Under their documentation the following must be supplied:

# 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

and I was hoping to not have to pass the model, but skip to the draw function, where I only need to pass the labels and data for the visualization to run.

Here is what I tried:

from sklearn.metrics import silhouette_score as sc
from yellowbrick.cluster import SilhouetteVisualizer

k = 2
data = bc_sv
labels = bc_best_labels
title = f"bc_{k}_silhouette_method_plots.png"

score = sc(data, labels, metric='euclidean')
print('Silhouetter Score: %.3f' % score)

visualizer = SilhouetteVisualizer.draw(data, labels)
visualizer.finalize()
visualizer.ax.set_xlabel('Silhouette Coefficient Values', fontsize=20)
visualizer.ax.set_ylabel('Cluster Labels', fontsize=20)
visualizer.fig.tight_layout() 
visualizer.fig.show()

but I get the error:

color_kwargs = {"n_colors": self.n_clusters_} AttributeError: 'numpy.ndarray' object has no attribute 'n_clusters_'

And this is probably because I cannot simply call the draw function from the visualizer, so I need some help trying to work around that.

Plewis
  • 119
  • 10

0 Answers0