-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_2d_visualization.py
160 lines (127 loc) · 7.1 KB
/
test_2d_visualization.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from GaussianMM import GaussianMM
from KMeans import KMeans
from MeanShift import MeanShift
from Agglomerative import Agglomerative
import numpy as np
import matplotlib.pyplot as plt
from sklearn import cluster, mixture
# Import datasets.
filename = 'data_2d_test/cluster_data_text/cluster_data_dataA_X.txt'
data_a_x = np.genfromtxt(filename, delimiter='\t')
data_a_x = np.transpose(data_a_x)
data_a_x = data_a_x[~np.isnan(data_a_x).any(axis=1)]
filename = 'data_2d_test/cluster_data_text/cluster_data_dataB_X.txt'
data_b_x = np.genfromtxt(filename, delimiter='\t')
data_b_x = np.transpose(data_b_x)
data_b_x = data_b_x[~np.isnan(data_b_x).any(axis=1)]
filename = 'data_2d_test/cluster_data_text/cluster_data_dataC_X.txt'
data_c_x = np.genfromtxt(filename, delimiter='\t')
data_c_x = np.transpose(data_c_x)
data_c_x = data_c_x[~np.isnan(data_c_x).any(axis=1)]
def plot_clusters(samples, labels, title):
"""
Plot the results of 2D data point clustering analysis.
"""
filtered_label0 = samples[labels == 0]
filtered_label1 = samples[labels == 1]
filtered_label2 = samples[labels == 2]
filtered_label3 = samples[labels == 3]
filtered_labels = samples[labels >= 4]
plt.scatter(filtered_label0[:, 0] , filtered_label0[:, 1] , color = 'red')
plt.scatter(filtered_label1[:, 0] , filtered_label1[:, 1] , color = 'green')
plt.scatter(filtered_label2[:, 0] , filtered_label2[:, 1] , color = 'blue')
plt.scatter(filtered_label3[:, 0] , filtered_label3[:, 1] , color = 'yellow')
plt.scatter(filtered_labels[:, 0] , filtered_labels[:, 1] , color = 'grey')
plt.xlabel("x_1")
plt.ylabel("x_2")
plt.title(title)
plt.savefig(title.replace(".", "_").replace(" ", "_").replace(",", "")
.replace("=", "").replace("(", "").replace(")", "").lower())
plt.close()
# # 2d dataset a.
meanshift = MeanShift(bandwidth=2.1, max_iter=200, tol=1e-4)
cluster_labels, cluster_centers = meanshift.fit(data_a_x)
plot_clusters(data_a_x, cluster_labels, "blobs, mean-shift (my implementation)")
kmeans = KMeans(k=4, tol=1e-4, max_iter=200, n_init=500)
cluster_labels, cluster_centers = kmeans.fit(data_a_x)
plot_clusters(data_a_x, cluster_labels, "blobs, k-means (my implementation)")
gmm = GaussianMM(k=4, max_iter=200, n_init=500, regul=1e-6)
cluster_labels, cluster_centers, cluster_weights = gmm.fit(data_a_x)
plot_clusters(data_a_x, cluster_labels, "blobs, EM-GMM (my implementation)")
agglomerative = Agglomerative(k=4, linkage="average")
cluster_centers, cluster_labels = agglomerative.fit(data_a_x)
plot_clusters(data_a_x, cluster_labels, "blobs, agglomerative (my implementation)")
kmeans = cluster.KMeans(
n_clusters=4, random_state=0, n_init=30, init="k-means++"
).fit(data_a_x)
plot_clusters(data_a_x, kmeans.labels_, "blobs, k-means (scikit-learn)")
gmm = mixture.GaussianMixture(
n_components=4, covariance_type="full", random_state=0, n_init=300
).fit(data_a_x)
plot_clusters(data_a_x, gmm.predict(data_a_x), "blobs, EM-GMM (scikit-learn)")
bandwidth = cluster.estimate_bandwidth(data_a_x, quantile=0.25, random_state=0)
meanshift = cluster.MeanShift(bandwidth=bandwidth).fit(data_a_x)
bandwidth = str(np.round(bandwidth, 2))
plot_clusters(data_a_x, meanshift.labels_, f"blobs, mean-shift, bandwidth = {bandwidth} (scikit-learn)")
agglomerative = cluster.AgglomerativeClustering(
n_clusters=4, affinity='euclidean', compute_full_tree='auto', linkage='average'
).fit(data_a_x)
plot_clusters(data_a_x, agglomerative.labels_, "blobs, agglomerative (scikit-learn)")
# # 2d dataset b.
meanshift = MeanShift(bandwidth=1.65, max_iter=200, tol=1e-4)
cluster_labels, cluster_centers = meanshift.fit(data_b_x)
plot_clusters(data_b_x, cluster_labels, "sticks, mean-shift (my implementation)")
kmeans = KMeans(k=4, tol=1e-4, max_iter=200, n_init=500)
cluster_labels, cluster_centers = kmeans.fit(data_b_x)
plot_clusters(data_b_x, cluster_labels, "sticks, k-means (my implementation)")
gmm = GaussianMM(k=4, max_iter=200, n_init=500, regul=1e-6)
cluster_labels, cluster_centers, cluster_weights = gmm.fit(data_b_x)
plot_clusters(data_b_x, cluster_labels, "sticks, EM-GMM (my implementation)")
agglomerative = Agglomerative(k=4, linkage="average")
cluster_centers, cluster_labels = agglomerative.fit(data_b_x)
plot_clusters(data_b_x, cluster_labels, "sticks, agglomerative (my implementation)")
kmeans = cluster.KMeans(
n_clusters=4, random_state=0, n_init=30, init="k-means++"
).fit(data_b_x)
plot_clusters(data_b_x, kmeans.labels_, "sticks, k-means (scikit-learn)")
gmm = mixture.GaussianMixture(
n_components=4, covariance_type="full", random_state=0, n_init=300
).fit(data_b_x)
plot_clusters(data_b_x, gmm.predict(data_b_x), "sticks, EM-GMM (scikit-learn)")
bandwidth = cluster.estimate_bandwidth(data_b_x, quantile=0.25, random_state=0)
meanshift = cluster.MeanShift(bandwidth=bandwidth).fit(data_b_x)
bandwidth = str(np.round(bandwidth, 2))
plot_clusters(data_b_x, meanshift.labels_, f"sticks, mean-shift, bandwidth = {bandwidth} (scikit-learn)")
agglomerative = cluster.AgglomerativeClustering(
n_clusters=4, affinity='euclidean', compute_full_tree='auto', linkage='average'
).fit(data_b_x)
plot_clusters(data_b_x, agglomerative.labels_, "sticks, agglomerative (scikit-learn)")
# # 2d dataset c.
meanshift = MeanShift(bandwidth=2.0, max_iter=200, tol=1e-4)
cluster_labels, cluster_centers = meanshift.fit(data_c_x)
plot_clusters(data_c_x, cluster_labels, "moons and stars, mean-shift (my implementation)")
kmeans = KMeans(k=4, tol=1e-4, max_iter=200, n_init=500)
cluster_labels, cluster_centers = kmeans.fit(data_c_x)
plot_clusters(data_c_x, cluster_labels, "moons and stars, k-means (my implementation)")
gmm = GaussianMM(k=4, max_iter=200, n_init=500, regul=1e-6)
cluster_labels, cluster_centers, cluster_weights = gmm.fit(data_c_x)
plot_clusters(data_c_x, cluster_labels, "moons and stars, EM-GMM (my implementation)")
agglomerative = Agglomerative(k=4, linkage="average")
cluster_centers, cluster_labels = agglomerative.fit(data_c_x)
plot_clusters(data_c_x, cluster_labels, "moons and stars, agglomerative (my implementation)")
kmeans = cluster.KMeans(
n_clusters=4, random_state=0, n_init=30, init="k-means++"
).fit(data_c_x)
plot_clusters(data_c_x, kmeans.labels_, "moons and stars, k-means (scikit-learn)")
gmm = mixture.GaussianMixture(
n_components=4, covariance_type="full", random_state=0, n_init=300
).fit(data_c_x)
plot_clusters(data_c_x, gmm.predict(data_c_x), "moons and stars, EM-GMM (scikit-learn)")
bandwidth = cluster.estimate_bandwidth(data_c_x, quantile=0.25, random_state=0)
meanshift = cluster.MeanShift(bandwidth=bandwidth).fit(data_c_x)
bandwidth = str(np.round(bandwidth, 2))
plot_clusters(data_c_x, meanshift.labels_, f"moons and stars, mean-shift, bandwidth = {bandwidth} (scikit-learn)")
agglomerative = cluster.AgglomerativeClustering(
n_clusters=4, affinity='euclidean', compute_full_tree='auto', linkage='average'
).fit(data_c_x)
plot_clusters(data_c_x, agglomerative.labels_, "moons and stars, agglomerative (scikit-learn)")