-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_clusters.py
73 lines (52 loc) · 1.84 KB
/
plot_clusters.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
import sys
import numpy
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import adjusted_rand_score
from matplotlib.colors import ListedColormap
def read_dataset(path):
data = numpy.loadtxt(open(path, "rb"), delimiter=",", skiprows=1)
return data[:,:-1], data[:,-1]
def plot_dataset2D(X, y, y_true, score=None, labels=False):
"""Plot first two features of dataset.
Parameters
----------
X :
datapoints (n x d)
y :
labels (n)
"""
cb_cmap = ListedColormap(sns.color_palette('colorblind'))
# construct cmap
flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]
my_cmap = ListedColormap(sns.color_palette(flatui).as_hex())
new_cmap = sns.light_palette("Navy", as_cmap=True)
fig = plt.figure()
ax = fig.add_subplot(111)
if score is not None:
ax.set_title(f'Adjusted Rand Index : {score:.4f}')
ax.scatter(X[:, 0], X[:, 1], marker="o", c=y, s=20, edgecolor="k", cmap=my_cmap)
if labels:
for i in range(X.shape[0]):
ax.annotate(f"{int(y_true[i])}:{int(y[i])}", (X[i, 0]+.1, X[i, 1]+.1))
ax.legend(y)
# plt.axis('equal')
plt.axis('square')
plt.show()
plt.savefig(sys.argv[1][:-4] + '.png')
def main():
if len(sys.argv) < 2:
print('Usage: plot_clusters.py <prediction_dataset_path>')
return
elif len(sys.argv) == 2:
print('To plot with similarity score use: plot_clusters.py <prediction_dataset_path> <true_dataset_path>')
X_pred, y_pred = read_dataset(sys.argv[1])
score = None
y_true = None
if(len(sys.argv) > 2):
X_true, y_true = read_dataset(sys.argv[2])
# Compute similarity score
score = adjusted_rand_score(y_pred, y_true)
plot_dataset2D(X_pred, y_pred, y_true, score)
if __name__ == "__main__":
main()