1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import numpy as np import matplotlib.pyplot as plt from sklearn.cluster import KMeans from sklearn.manifold import TSNE
data = np.load('test.npy')
features = data[:, 0, :]
num_clusters = 3 kmeans = KMeans(n_clusters=num_clusters, random_state=0) kmeans.fit(features) clusters = kmeans.labels_
tsne = TSNE(n_components=2, random_state=10) feature_tsne = tsne.fit_transform(features)
plt.figure(figsize=(10, 8)) scatter = plt.scatter(feature_tsne[:, 0], feature_tsne[:, 1], c=clusters, cmap='viridis', marker='o')
legend1 = plt.legend(*scatter.legend_elements(), title="Clusters") plt.gca().add_artist(legend1)
plt.title('t-SNE Visualization of Clusters') plt.xlabel('t-SNE Component 1') plt.ylabel('t-SNE Component 2') plt.grid() plt.show()
|