def robust_example(plot=False): X, y = load_robust() clusters = 5 k = KMeans(K=clusters, max_iters=50, init="++") k.fit(X) k.predict() if plot: k.plot()
def kmeans_example(plot=False): X, y = make_blobs(centers=4, n_samples=500, n_features=2, shuffle=True, random_state=42) clusters = len(np.unique(y)) k = KMeans(K=clusters, max_iters=150, init='++') k.fit(X) k.predict() if plot: k.plot()
def iris_example(plot=False): X, y = load_iris() clusters = len(np.unique(y)) k = KMeans(K=clusters, max_iters=50, init="++") k.fit(X, y) k.predict() data = np.zeros([k.n_samples, 2]) # Dimension reducing # Sepal width*length data[:, 0] = k.X[:, 0] * k.X[:, 1] # Petal width*length data[:, 1] = k.X[:, 2] * k.X[:, 3] if plot: k.plot(data)
def KMeans_and_GMM(K): COLOR = 'bgrcmyk' X, y = make_clusters(skew=True, n_samples=1500, centers=K) _, axes = plt.subplots(1, 3) # Ground Truth axes[0].scatter(X[:, 0], X[:, 1], c=[COLOR[int(assignment)] for assignment in y]) axes[0].set_title("Ground Truth") # KMeans kmeans = KMeans(K=K, init='++') kmeans.fit(X) kmeans.predict() axes[1].set_title("KMeans") kmeans.plot(ax=axes[1], holdon=True) # Gaussian Mixture gmm = GaussianMixture(K=K, init='kmeans') gmm.fit(X) axes[2].set_title("Gaussian Mixture") gmm.plot(ax=axes[2])