code: def plot_kmeans_interactive(min_clusters=1, max_clusters=6):
      from IPython.html.widgets import interact
      from sklearn.metrics.pairwise import euclidean_distances
      from sklearn.datasets.samples_generator import make_blobs
  
      with warnings.catch_warnings():
          warnings.filterwarnings("ignore")
  
          X, y = make_blobs(
              n_samples=300, centers=4, random_state=0, cluster_std=0.60
          )
  
          def _kmeans_step(frame=0, n_clusters=4):
              rng = np.random.RandomState(2)
              labels = np.zeros(X.shape[0])
              centers = rng.randn(n_clusters, 2)
  
              nsteps = frame // 3
  
              for i in range(nsteps + 1):
                  old_centers = centers
                  if i < nsteps or frame % 3 > 0:
                      dist = euclidean_distances(X, centers)
                      labels = dist.argmin(1)
  
                  if i < nsteps or frame % 3 > 1:
                      centers = np.array(
                          [
                              X[labels == j].mean(0)
                              for j in range(n_clusters)
                          ]
                      )
                      nans = np.isnan(centers)
                      centers[nans] = old_centers[nans]
  
              # plot the data and cluster centers
              plt.scatter(
                  X[:, 0],
                  X[:, 1],
                  c=labels,
                  s=50,
                  cmap="rainbow",
                  vmin=0,
                  vmax=n_clusters - 1,
              )
              plt.scatter(
                  old_centers[:, 0],
                  old_centers[:, 1],
                  marker="o",
                  c=np.arange(n_clusters),
                  s=200,
                  cmap="rainbow",
              )
              plt.scatter(
                  old_centers[:, 0],
                  old_centers[:, 1],
                  marker="o",
                  c="black",
                  s=50,
              )
  
              # plot new centers if third frame
              if frame % 3 == 2:
                  for i in range(n_clusters):
                      plt.annotate(
                          "",
                          centers[i],
                          old_centers[i],
                          arrowprops=dict(arrowstyle="->", linewidth=1),
                      )
                  plt.scatter(
                      centers[:, 0],
                      centers[:, 1],
                      marker="o",
                      c=np.arange(n_clusters),
                      s=200,
                      cmap="rainbow",
                  )
                  plt.scatter(
                      centers[:, 0],
                      centers[:, 1],
                      marker="o",
                      c="black",
                      s=50,
                  )
  
              plt.xlim(-4, 4)
              plt.ylim(-2, 10)
  
              if frame % 3 == 1:
                  plt.text(
                      3.8,
                      9.5,
                      "1. Reassign points to nearest centroid",
                      ha="right",
                      va="top",
                      size=14,
                  )
              elif frame % 3 == 2:
                  plt.text(
                      3.8,
                      9.5,
                      "2. Update centroids to cluster means",
                      ha="right",
                      va="top",
                      size=14,
                  )
  
      return interact(
          _kmeans_step,
          frame=[0, 50],
          n_clusters=[min_clusters, max_clusters],
      )
  
file_path: data-science-ipython-notebooks/scikit-learn/fig_code/figures.py
imports: import numpy
  import matplotlib.pyplot
  import warnings
label_desc: Use image node_id 5 for calling a global function with example usage: plot_kmeans_interactive(min_clusters, max_clusters) and returns: interact