def plot(Xs, labels, K, clusters): labelsNo = np.max(labels) markers = [] # get the different markers while len(markers) < labelsNo: markers.extend(list(matplotlib.markers.MarkerStyle.filled_markers)) colors = plt.cm.rainbow(np.linspace(0, 1, K+1)) if Xs.shape[1] == 2: x = Xs[:,0] y = Xs[:,1] for (_x, _y, _c, _l) in zip(x, y, clusters, labels): plt.scatter(_x, _y, s=200, c=colors[_c], marker=markers[_l]) plt.show() elif Xs.shape[1] == 3: x = Xs[:,0] y = Xs[:,1] z = Xs[:,2] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') for (_x, _y, _z, _c, _l) in zip(x, y, z, clusters, labels): ax.scatter(_x, _y, _z, s=200, c=colors[_c], marker=markers[_l]) plt.show() else: for i in range(N1): print(i, ": ", clusters[i], " ~ ", labels[i])
def plotClusters(Xs, labels, centroids, clusters): """ Plot the data with the true labels alongside the centroids and the predicted cluster. If the elements from the dataset are not 2 or 3 dimensional then print the index, predicted cluster and true label. Args: Xs (numpy array): dataset labels (numpy array): real/true labels centroids (numpy array): positions for the centroids clusters (numpy array): predicted labels """ labelsNo = np.max(labels) K = centroids.shape[0] markers = [] while len(markers) < labelsNo: markers.extend(list(matplotlib.markers.MarkerStyle.filled_markers)) colors = plt.cm.rainbow(np.linspace(0, 1, K + 1)) if Xs.shape[1] == 2: x = Xs[:, 0] y = Xs[:, 1] for (_x, _y, _c, _l) in zip(x, y, clusters, labels): plt.scatter(_x, _y, s=500, c=[colors[_c]], marker=markers[_l]) plt.scatter(centroids[:, 0], centroids[:, 1], s=800, c=[colors[K]], marker=markers[labelsNo]) plt.show() elif Xs.shape[1] == 3: x = Xs[:, 0] y = Xs[:, 1] z = Xs[:, 2] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') for (_x, _y, _z, _c, _l) in zip(x, y, z, clusters, labels): ax.scatter(_x, _y, _z, s=200, c=[colors[_c]], marker=markers[_l]) ax.scatter(centroids[:, 0], centroids[:, 1], centroids[:, 2], s=400, c=[colors[K]], marker=markers[labelsNo]) plt.show() else: for i in range(Xs.shape[0]): print(f"{i} : {clusters[i]} ~ {labels[i]}")