Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def elbow():
X, _ = make_blobs(centers=8, n_features=12, shuffle=True)
oz = KElbowVisualizer(KMeans(), k=(4, 12), ax=newfig())
oz.fit(X)
savefig(oz, "elbow")
def k_elbow(ax=None):
X, y = make_blobs(centers=12, n_samples=1000, n_features=16, shuffle=True)
viz = KElbowVisualizer(KMeans(), k=(4, 12), ax=ax, locate_elbow=False)
viz.fit(X)
viz.finalize()
return viz
def find_optk():
from yellowbrick.cluster import KElbowVisualizer
model = KMeans(**kwargs)
visualizer = KElbowVisualizer(model, k=(4, 12))
visualizer.fit(self.train_data)
visualizer.show()
print(f"Optimal number of clusters is {visualizer.elbow_value_}.")
return visualizer.elbow_value_
def draw_elbow(path="images/elbow.png"):
# Generate synthetic dataset with 8 blobs
X, y = make_blobs(
centers=8, n_features=12, n_samples=1000,
shuffle=True, random_state=42
)
# Create a new figure to draw the clustering visualizer on
_, ax = plt.subplots()
# Instantiate the clustering model and visualizer
model = KMeans()
visualizer = KElbowVisualizer(model, ax=ax, k=(4,12))
visualizer.fit(X) # Fit the data to the visualizer
visualizer.poof(outpath=path) # Draw/show/poof the data
def clustering(fname="clustering.png"):
# Create side-by-side axes grid
_, axes = plt.subplots(ncols=2, figsize=(18,6))
X, y = make_blobs(centers=7)
# Add K-Elbow to the left
oz = KElbowVisualizer(MiniBatchKMeans(), k=(3,12), ax=axes[0])
oz.fit(X, y)
oz.finalize()
# Add SilhouetteVisualizer to the right
oz = SilhouetteVisualizer(Birch(n_clusters=5), ax=axes[1])
oz.fit(X, y)
oz.finalize()
# Save figure
path = os.path.join(FIGURES, fname)
plt.tight_layout()
plt.savefig(path)
def draw_calinski_harabaz(path="images/calinski_harabaz.png"):
# Generate synthetic dataset with 8 blobs
X, y = make_blobs(
centers=8, n_features=12, n_samples=1000,
shuffle=True, random_state=42
)
# Create a new figure to draw the clustering visualizer on
_, ax = plt.subplots()
# Instantiate the clustering model and visualizer
model = KMeans()
visualizer = KElbowVisualizer(
model, ax=ax, k=(4,12),
metric='calinski_harabaz', timings=False
)
visualizer.fit(X) # Fit the data to the visualizer
visualizer.poof(outpath=path) # Draw/show/poof the data