|
37 | 37 | heterogeneity,
|
38 | 38 | k
|
39 | 39 | )
|
40 | | - 5. Transfers Dataframe into excel format it must have feature called |
| 40 | + 5. Plot the labeled 3D data points with centroids. |
| 41 | + plot_kmeans( |
| 42 | + X, |
| 43 | + centroids, |
| 44 | + cluster_assignment |
| 45 | + ) |
| 46 | + 6. Transfers Dataframe into excel format it must have feature called |
41 | 47 | 'Clust' with k means clustering numbers in it.
|
42 | 48 | """
|
43 | 49 |
|
@@ -126,6 +132,19 @@ def plot_heterogeneity(heterogeneity, k):
|
126 | 132 | plt.show()
|
127 | 133 |
|
128 | 134 |
|
| 135 | +def plot_kmeans(data, centroids, cluster_assignment): |
| 136 | + ax = plt.axes(projection="3d") |
| 137 | + ax.scatter(data[:, 0], data[:, 1], data[:, 2], c=cluster_assignment, cmap="viridis") |
| 138 | + ax.scatter( |
| 139 | + centroids[:, 0], centroids[:, 1], centroids[:, 2], c="red", s=100, marker="x" |
| 140 | + ) |
| 141 | + ax.set_xlabel("X") |
| 142 | + ax.set_ylabel("Y") |
| 143 | + ax.set_zlabel("Z") |
| 144 | + ax.set_title("3D K-Means Clustering Visualization") |
| 145 | + plt.show() |
| 146 | + |
| 147 | + |
129 | 148 | def kmeans(
|
130 | 149 | data, k, initial_centroids, maxiter=500, record_heterogeneity=None, verbose=False
|
131 | 150 | ):
|
@@ -193,6 +212,7 @@ def kmeans(
|
193 | 212 | verbose=True,
|
194 | 213 | )
|
195 | 214 | plot_heterogeneity(heterogeneity, k)
|
| 215 | + plot_kmeans(dataset["data"], centroids, cluster_assignment) |
196 | 216 |
|
197 | 217 |
|
198 | 218 | def report_generator(
|
|
0 commit comments